小程序swiper带切换按钮

小程序自带swiper组件,

display-multiple-items 同时显示的滑块数量
current: 当前所在滑块的 index
duration:切换需要花的时间 一般为1秒
bindchange:current发生改变触发
bindanimationfinish:动画结束时触发
circular 是否采用衔接滑动

      <view class="swiperCont">
        <view class="activeBtn1" bindtap="toLeft"></view>
        <view class="activeBtn2" bindtap="toRight"></view>

        <swiper current="{{currentIndex}}" circular="true" duration="1000" bindchange="changeIndex" bindanimationfinish="changeFinish" display-multiple-items="4">
          <swiper-item wx:for="{{[1, 2, 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]}}" wx:key="index" bindtap="openPop" data-type="choose" data-ind="index">
            <view class="swiper_cont">
              <image src="../../ossweb-img/icon_treasureBox.png" mode="scaleToFill" class="slide-image" />
            </view>
            <text>道具名xxxxx道具名xxxxx{{index}}</text>
          </swiper-item>
        </swiper>

      </view>
// index.js
Page({
  data: {
    currentIndex: 0, // 页面swiper的current索引
    index: 0,
    flag: true,
  },

  onLoad(options) {
  },
  onShow() {
  },
  onHide() {
  },
  toLeft: function (e) {
    console.log(this.data.flag)
    if (!this.data.flag) { // 动画未完成不执行
      return
    } else {
      // 修改按钮切换不可用状态
      this.setData({
        flag: false
      })
      let index = this.data.index
      if (index > 0) {
        this.setData({
          currentIndex: index - 1
        })
      } else {
        this.setData({
          currentIndex: 0
        })
      }
    }
  },
  toRight: function (e) {
    console.log(this.data.flag)
    if (!this.data.flag) { // 动画未完成不执行
      return
    } else {
      // 修改按钮切换不可用状态
      this.setData({
        flag: false
      })
      let index = this.data.index
      if (index >= 16) {
        this.setData({
          currentIndex: 0
        })
      } else {
        this.setData({
          currentIndex: index + 1
        })
      }
    }
  },
  changeIndex: function (e) { // 切换过程绑定
    console.log("e.detail.current",e.detail.current);
    this.setData({
      index: e.detail.current,
      currentIndex: e.detail.current
    })
  },
  changeFinish: function (e) { // 动画完成
    // 修改按钮切换可用状态
    this.setData({
      flag: true
    })
  },
.swiperCont {
  width: 650rpx;
  height: 180rpx;
  margin: 10rpx auto 0;
  position: relative;
}

.activeBtn1 {
  width: 40rpx;
  height: 66rpx;
  box-sizing: border-box;
  background: url("/ossweb-img/btn_active.png") no-repeat;
  background-size: 100% 100%;
  position: absolute;
  top: 20%;
  left: -40rpx;
}

.activeBtn2 {
  width: 40rpx;
  height: 66rpx;
  box-sizing: border-box;
  background: url("/ossweb-img/btn_active.png") no-repeat;
  background-size: 100% 100%;
  transform: rotate(180deg);
  position: absolute;
  top: 20%;
  right: -40rpx;
}

.swiperCont swiper {
  height: 180rpx;
}

.swiperCont swiper-item {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
}

swiper-item .swiper_cont {
  width: 134rpx;
  height: 134rpx;
  box-sizing: border-box;
  background: url("/ossweb-img/propsBG.png") no-repeat;
  background-size: 100% 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

swiper-item .swiper_cont .slide-image {
  width: 90rpx;
  height: 90rpx;
}

swiper-item text {
  width: 134rpx;
  font-size: 20rpx;
  color: #122334;
  text-align: center;
}

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值