Marquee.js 3.14 KB
// components/marquee/marquee.js
let timer = null
import {componentsMixin } from '../../mixins/index'
Component({
  behaviors:[componentsMixin],
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: String,
      observer: function (n) {
        if (n) this.begin()
      }
    },
    delay: {
      type: Number,
      value: 1000
    },
    fontSize: {
      type: Number,
      value: 12
    },
    speed: {
      type: Number,
      value: 5000
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    flag: true,
    show: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 获取宽度
     */
    begin() {
      let boxWidth = 0
      let valueWidth = 0
      wx.$util.getViewInfo('#marquee', res => {
        boxWidth = res.width
        this.setData({
          boxWidth
        })
        wx.$util.getViewInfo('#content', contentInfo => {
          valueWidth = contentInfo.width
          if (boxWidth - 20 <= valueWidth) {
            this.setData({
              valueWidth,
              isRun: true
            })
            this.run()
          }
        }, this)
      }, this)
    },
    run() {
      clearTimeout(timer)
      wx.$util.promise(resolve => {
        let {
          speed,
          delay,
          valueWidth
        } = this.data
        let animation = wx.createAnimation({
          duration: speed,
          timingFunction: 'linear',
          delay
        }).left(`-${valueWidth}px`).step()
        this.setData({
          animation: animation.export()
        })
        let time = Number(delay) + Number(speed) + 1000
        timer = setTimeout(() => {
          resolve()
        }, time)
      }).then(() => {
        this.setData({
          flag: false
        })
        this.again()
      })

    },
    again() {
      clearTimeout(timer)
      wx.$util.promise(resolve => {
        let {
          boxWidth
        } = this.data
        let animation = wx.createAnimation({
          duration: 1,
          timingFunction: 'linear'
        }).left(`${boxWidth}px`).step()
        this.setData({
          animation: animation.export()
        })
        timer = setTimeout(() => {
          resolve()
        }, 500)
      }).then(() => {
        this.setData({
          flag: true
        })
        this.reset()
      })

    },
    reset() {
      clearTimeout(timer)
      wx.$util.promise(resolve => {
        let {
          valueWidth,
          boxWidth,
          speed
        } = this.data
        let animation = wx.createAnimation({
          duration: speed * (boxWidth / valueWidth).toFixed(2),
          timingFunction: 'linear',
        }).left('0%').step()
        this.setData({
          animation: animation.export()
        })
        timer = setTimeout(() => {
          resolve()
        }, Number(speed) + 1000)
      }).then(() => {
        this.run()
      })
    }
  },
  pageLifetimes: {
    show: function () {
      this.setData({
        show: true
      })
      this.begin()
    },
    hide: function () {
      if (this.data.isRun) {
        this.reset()
        clearTimeout(timer)
        this.setData({
          show: false
        })
      }
    }
  }
})