午夜91福利视频,午夜成人在线观看,午夜在线视频免费观看,午夜福利短视频,精品午夜成人免费视频APP

小程序模板網

微信小程序組件之marquee

發布時間:2018-04-21 08:54 所屬欄目:小程序開發教程

1. marquee標簽

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

html的(de)(de)marquee是這樣使(shi)用(yong)的(de)(de)。

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

2. wxml

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

傳(chuan)入wxml的是個(ge)json對象

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

而那個奇怪(guai)的–marqueeWidth是給(gei)@keyframes傳(chuan)的變量。內聯(lian)設置(zhi)變量,css文件中(zhong)也(ye)可(ke)以獲取到該變量。

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;
  }
}

以(yi)上(shang)是組件(jian)的(de)封裝。

5. 使用

// wxml
"../component/marquee/marquee.wxml" />
// wxss
@import "../component/marquee/marquee.wxss";
// js
import 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);


易優小程序(企業版)+靈活api+前后代碼開源(yuan) 碼(ma)云倉庫:
本文地址://www.jinyoudianli.com/wxmini/doc/course/23814.html 復制鏈接 如需(xu)定制(zhi)請聯系易優(you)客服咨詢:

工作日 8:30-12:00 14:30-18:00
周(zhou)六及部分節假日提供值班服務

易小優(you)
轉(zhuan)人工 ×