goodsSwiper.js 2.34 KB
Component({

  properties: {
    data: {
      type: Array,
      observer(n) {
        if (n && n.length) {
          this.setData({
            list: n
          })
          this.startSwiper()
        }
      }
    },
    conType:Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    list: [],
  },

  /**
   * 组件的方法列表
   */
  methods: {

    fulfill() {
      if (this.timer) clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        let {
          list
        } = this.data
        list.splice(0, 1)
        this.setData({
          animation: false
        }, () => {
          wx.nextTick(() => {
            this.setData({
              list,
              zoom: false
            })
          })
        })
      }, 400)
    },

    clickGoods(auto) {
      let {
        animation,
        list
      } = this.data,
        [item] = list
      if (animation) return
      this.stop = auto == true ? false : true
      list.push(item)
      this.setData({
        animation: true,
        list
      }, () => {
        setTimeout(() => {
          this.setData({
            zoom: true
          })
          this.fulfill()
          if (this.stop) {
            this.stop = false
            this.startSwiper()
          }
        }, 200)
      })
    },

    startSwiper() {
      if (this.swiperTimer) clearTimeout(this.swiperTimer)
      this.swiperTimer = setTimeout(() => {
        if (this.stop) {
          return clearTimeout(this.swiperTimer)
        }
        this.clickGoods(true)
        this.startSwiper()
      }, 3e3)
    },

    start(e) {
      let cell = e.changedTouches[0]
      this.startX = cell.clientX
      this.startY = cell.clientY
    },
    // move(e) {
    //   let {
    //     startX,
    //     startY,
    //     data: {
    //       list
    //     }
    //   } = this
    //   let {
    //     index
    //   } = e.currentTarget.dataset
    //   let {
    //     clientX,
    //     clientY
    //   } = e.changedTouches[0]
    //   let X = clientX - startX
    //   let Y = clientY - startY
    //   let angle = (360 * Math.atan(Y / X)) / (2 * Math.PI)
    //   let result = clientX < startX
    //   if (Math.abs(angle) > 30) return
    //   if (!result) return
    //   list[index].style = `transform: translateX(-${startX - clientX}px)`
    //   this.setData({
    //     list
    //   })

    // }
  }
})