微信小程序 swiper 点击切换,左右滑动,自动滑动

时间:2024-03-13 11:13:45

.wxml

<view class="swiper-tab">
  <view class="swiper-tab-list {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swichNav">红色</view>
  <view class="swiper-tab-list {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swichNav">绿色</view>
  <view class="swiper-tab-list {{currentTab==2 ? 'on' : ''}}" data-current="2" bindtap="swichNav">黄色</view>
</view>
<view class="swiper-tab-temp"></view>
<view>
  <swiper current="{{currentTab}}" autoplay="{{true}}" class="swiper-box" duration="300" style="height:600px" bindchange="bindChange">
    <swiper-item style="background-color:red">
      红色内容
    </swiper-item>
    <swiper-item style="background-color:green">
      绿色内容
    </swiper-item>
    <swiper-item style="background-color:yellow">
      黑色内容
    </swiper-item>
  </swiper>
</view>


.wxss

.swiper-tab {
  width: 100%;
  border-bottom: 1px solid #777;
  text-align: center;
  line-height: 40px;
  background-color: #fff;
}


.swiper-tab-list {
  font-size: 13px;
  display: inline-block;
  width: 33.3%;
  color: #777;
}
.on {
  color: #FFF;
  background-color: #528dc3;
  border-top:5rpx solid #528dc3;
  border-bottom:5rpx solid #528dc3;
}


.swiper-box {
  display: block;
  height: 100%;
  width: 100%;
  overflow: hidden;
}


.swiper-box view {
  text-align: center;
}


.js

Page({
  data: {
    currentTab: '',
  },
  /*** 滑动切换tab***/
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },
  /*** 点击tab切换***/
  swichNav: function (e) {
    var that = this;
    that.setData({
      currentTab: e.target.dataset.current
    });
  },
  /*
    onshow
   */
})

官方文档:https://mp.weixin.qq.com/debug/wxadoc/dev/component/swiper.html

效果如图

微信小程序 swiper 点击切换,左右滑动,自动滑动


本文纯属原创