微信小程式元件 marquee實例分享

小云云
發布: 2018-01-06 10:48:00
原創
2958 人瀏覽過

本文主要介紹了微信小程式組件 marquee實例詳解的相關資料,需要的朋友可以參考下,希望能幫助到大家。

微信小程式元件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:&#39;hello world&#39;
}
登入後複製

而那個奇怪的--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";
// js
import marquee from &#39;../component/marquee/marquee.js&#39;;

var options = Object.assign(marquee, {
 data: {
  motto: &#39;Hello World&#39;,
  userInfo: {},
  marquee: { text: &#39;你好,中国!hello,world!&#39; }
 },
 onLoad: function () {
  // ...

  const str = this.data.marquee.text;
  const width = this.getWidth(str);
  console.log(&#39;width&#39;,width);
  this.setData({ [`${&#39;marquee&#39;}.width`]: width });
 }
});
Page(options);
登入後複製

相關推薦:

##html中關於marquee標籤的詳細介紹

總結marquee 標籤的使用實例

透過marquee標籤完成捲動效果的純html程式碼

###############################################################

以上是微信小程式元件 marquee實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!