實(shí)現(xiàn)一個(gè)微信小程序組件:文字跑馬燈效果

項(xiàng)目未動(dòng),組件先行

廢話不多說,直接上代碼

  • marquee.json
{
  "component": true,
  "usingComponents": {}
}
  • marquee.wxml
<!--components/marquee.wxml-->
<view class='marquee_container'>
  <view class='marquee_text' style='{{ orientation }}:{{ marqueeDistance }}px;font-size: {{size}}px;'>
    {{ text }}
  </view>
</view>

  • marquee.wxss
/* components/marquee.wxss */
.marquee_container {
  position: relative;  
  width: 100%;
}

.marquee_text {
  position: absolute;
  white-space: nowrap;
  top: 0;
}
  • marquee
// components/marquee.js
Component({
  /**
   * 組件的屬性列表
   */
  properties: {
    text: {
      type: String,
      value: 'ILoveEwei'
    }
  },

  /**
   * 組件的初始數(shù)據(jù)
   */
  data: {
    marqueePace: 1,
    marqueeDistance: 0,
    size: 14,
    orientation: 'left',
    interval: 20
  },

  /**
   * 組件的方法列表
   */
  methods: {
    _scrolling: function() {
      var _this = this;
      var timer = setInterval(()=> {
        if(-_this.data.marqueeDistance < _this.data.length) {
          _this.setData({
            marqueeDistance: _this.data.marqueeDistance - _this.data.marqueePace
          })
        } else {
          clearInterval(timer);
          _this.setData({
            marqueeDistance: _this.data.windowWidth
          });
          _this._scrolling();
        }
      },_this.data.interval);
    }
  },

  created: function() {
    var _this = this;
    var length = _this.data.text.length * _this.data.size;
    var windowWidth = wx.getSystemInfoSync().windowWidth
    _this.setData({
      length: length,
      windowWidth: windowWidth
    });
    _this._scrolling();
  }
})

以上就是我定義在components文件夾的組件,剩下就是引用組件

  • demo.json
{
  "usingComponents": {
    "marquee": "../../components/marquee"
  }
}
  • demo.wxml
<marquee text="我愛你我愛你我愛你"></marquee>  

就這樣可以了

參考資料

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容