淘宝小程序无缝轮播

  <!-- 轮播 -->
  <view style="width:714rpx;height:850rpx;overflow:hidden;position:relative;">
    <swiper style="width:714rpx;height:850rpx;overflow:hidden;">
      <swiper-item>
        <scroll-view catchTouchStart="catchstart" catchTouchMove="catchmove" catchTouchEnd='catchend'>
          <view onTransitionEnd="onTransitionEnd" style="width:{{swiperArr.length*714}}rpx;height:850rpx;display:flex;transform:translateX(-{{current*714}}rpx);transition:all {{duration}}ms linear;">
            <view a:for="{{swiperArr}}" style="width:714rpx;height:850rpx;">
              <view style="width:640rpx;height:850rpx;margin:auto;background-size:100% 100%;background-image:url({{item.image}})">
              </view>
            </view>
          </view>
        </scroll-view>
      </swiper-item>
    </swiper>
    <!-- 小圆点 -->
    <view style="width:714rpx;height:16rpx;display:flex;position:absolute;bottom:20rpx;justify-content:center;">
      <view a:for="{{pointList}}" style="width:{{point == index?80:16}}rpx;height:16rpx;border-radius:{{point == index?'24rpx':'50%'}};background-color:#000;margin:0rpx 8rpx;">
      </view>
    </view>
  </view>

import { enhanceComponent } from 'tb-shop-enhance';

Component(enhanceComponent({
  data: {
    flag: true,
    point: 0,
    duration: 500,
  },

  onInit() {
    // mock数据需要修改client文件夹中的page文件
    // 正常运行过程中模块总是默认传入data参数
  },

  didMount() {
    // 加载成功后可以异步获取数据更新数据展示,例如请求接口等操作
    const { gdc = {}, mds = {}, modUtils } = this.props.data;

    this.setData({
      swiperArr: mds.moduleData.swiperArr,
    });


    let newArr = this.data.swiperArr.concat(this.data.swiperArr.concat(this.data.swiperArr))
    this.setData({
      pointList: this.data.swiperArr,
      current: this.data.swiperArr.length,
      swiperArr: newArr
    })

    if (this.data.autoPlay == 1) {
      this.timer = setInterval(() => {
        this.autoPlay()
      }, this.data.interval * 1000)
    } else {
      clearInterval(this.timer)
    }
  },

  methods: {
    catchmove() { },
    catchstart(e) {
      this.setData({
        start: e.changedTouches[0].clientX
      })

      clearInterval(this.timer)
    },
    catchend(e) {
      // let end = e.changedTouches[0].clientX
      this.setData({
        end: e.changedTouches[0].clientX
      })
      if (this.data.flag) {
        this.setData({
          flag: false
        })
        // 过渡时间
        if (this.data.duration == 0) {
          this.setData({
            duration: 500
          })
        }
        // 索引,小圆点
        if (this.data.start - this.data.end > 20) {
          this.setData({
            current: this.data.current + 1
          })
          if (this.data.point == this.data.swiperArr.length / 3 - 1) {
            this.setData({
              point: 0
            })
          } else {
            this.setData({
              point: this.data.point + 1
            })
          }
        } else if (this.data.end - this.data.start > 20) {
          this.setData({
            current: this.data.current - 1
          })
          if (this.data.point == 0) {
            this.setData({
              point: this.data.swiperArr.length / 3 - 1
            })
          } else {
            this.setData({
              point: this.data.point - 1
            })
          }
        }

        this.time = setTimeout(() => {
          this.setData({
            flag: true
          })
          clearTimeout(this.time)
        }, this.data.duration)
      }
      if (this.data.autoPlay == 1) {
        this.timer = setInterval(() => {
          this.autoPlay()
        }, this.data.interval * 1000)
      } else {
        clearInterval(this.timer)
      }
    },
    onTransitionEnd(e) {
      // console.log(e)
      if (this.data.current == 0) {
        this.setData({
          duration: 0,
          current: this.data.swiperArr.length / 3
        })
      } else if (this.data.current == this.data.swiperArr.length / 3 * 2) {
        this.setData({
          current: this.data.swiperArr.length / 3,
          duration: 0
        })
      }
    },
    autoPlay() {
      if (this.data.flag) {
        if (this.data.duration == 0) {
          this.setData({
            duration: 500
          })
        }
        this.setData({
          flag: false,
          current: this.data.current + 1
        })
        if (this.data.point == this.data.pointList.length - 1) {
          this.setData({
            point: 0
          })
        } else {
          this.setData({
            point: this.data.point + 1
          })
        }

        this.t = setTimeout(() => {
          this.setData({
            flag: true
          })
          clearTimeout(this.t)
        }, this.data.duration)
      }
    }
  },
}));
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值