国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

微信小程序組件 marquee實例詳解

2019-11-19 16:16:09
字體:
來源:轉載
供稿:網友

微信小程序組件 marquee實例詳解

1. marquee標簽

html是有marquee標簽的,可以實現跑馬燈效果,但小程序沒有,所以要實現。這里考慮使用css3的animation實現。

html的marquee是這樣使用的。

<marquee direction="left" behavior="scroll" scrollamount="1" scrolldelay="0" loop="-1" width="200" height="50" bgcolor="#0099FF" hspace="10" vspace="10">   hello world</marquee>

2. wxml

<view class="marquee_container" style="--marqueeWidth--:{{-marquee.width}}em">  <view class="marquee_text">{{marquee.text}}</view></view>

傳入wxml的是個json對象

marquee:{  width:12,  text:'hello world'}

而那個奇怪的--marqueeWidth是給@keyframes傳的變量。內聯設置變量,css文件中也可以獲取到該變量。

3. wxss

@keyframes around {  from {   margin-left: 100%;  }  to {   margin-left: var(--marqueeWidth--);// var接受傳入的變量  } }.marquee_container{ background-color: #0099FF; height: 1.2em; position: relative; width: 100%;}.marquee_container:hover{ animation-play-state: paused; // 不起作用}.marquee_text{ display: inline-block; white-space: nowrap; animation-name: around; animation-duration: 5s; animation-iteration-count: infinite; animation-timing-function:linear;}

4. js

export default { getWidth:(str)=>{  return [].reduce.call(str, (pre, cur, index, arr) => {   if (str.charCodeAt(index) > 255) {// charCode大于255是漢字    pre++;   } else {    pre += 0.5;   }   return pre;  }, 0); }, getDuration:(str)=>{// 保留,根據文字長度設置時間  return this.getWidth()/10; }}

以上是組件的封裝。

5. 使用

// wxml<include src="../component/marquee/marquee.wxml" />// wxss@import "../component/marquee/marquee.wxss";// jsimport marquee from '../component/marquee/marquee.js';var options = Object.assign(marquee, { data: {  motto: 'Hello World',  userInfo: {},  marquee: { text: '你好,中國!hello,world!' } }, onLoad: function () {  // ...  const str = this.data.marquee.text;  const width = this.getWidth(str);  console.log('width',width);  this.setData({ [`${'marquee'}.width`]: width }); }});Page(options);

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 澄江县| 仙桃市| 南郑县| 嘉荫县| 米林县| 扬中市| 区。| 宁安市| 郁南县| 紫金县| 保亭| 辰溪县| 广河县| 四川省| 德令哈市| 天等县| 连南| 精河县| 监利县| 太原市| 略阳县| 甘肃省| 河西区| 山丹县| 邯郸县| 庆云县| 开阳县| 封开县| 晋城| 嵩明县| 崇文区| 巨野县| 阳曲县| 商都县| 扎赉特旗| 贵阳市| 霍林郭勒市| 龙海市| 伊金霍洛旗| 万年县| 铜梁县|