微信小程序选项卡切换(滑动切换,点击切换)

效果如下:可点击切换,滑动切换



代码如下
这个可以在项目用

index.wxml

html 复制代码
<view class='topTabSwiper'>
    <view class='tab  {{currentData == 0 ? "tabBorer" : ""}}'  data-current = "0" bindtap='checkCurrent'>选项一</view>
    <view class='tab  {{currentData == 1 ? "tabBorer" : ""}}'  data-current = "1" bindtap='checkCurrent'>选项二</view>
</view>
<swiper current="{{currentData}}" class='swiper' style="height:600px;" duration="300" bindchange="bindchange">
  <swiper-item><view class='swiper_con'>TAB1</view></swiper-item> 
  <swiper-item><view class='swiper_con'>TAB2</view></swiper-item>
</swiper>

index.wxss

css 复制代码
.tab {
  
  width: 50%;
  text-align: center;
  padding: 10rpx 0;
}

.topTabSwiper {
  display: flex;
  justify-content: space-between;
}
.tabBorer {
  border-bottom: 1px solid #f00;
  color: #f00;
}

.swiper {
  width: 100%;
}

.swiper_con {
  text-align: center;
  width: 100%;
  height: 100%;
  padding: 80rpx 0;
}

index.js

js 复制代码
//获取当前滑块的index
  bindchange:function(e){
    const that  = this;
    that.setData({
      currentData: e.detail.current
    })
  },
  //点击切换,滑块index赋值
  checkCurrent:function(e){
    const that = this;

    if (that.data.currentData === e.target.dataset.current){
        return false;
    }else{

      that.setData({
        currentData: e.target.dataset.current
      })
    }
  },
相关推荐
海兰11 小时前
【文字三国志:第六篇】天命重构,UI组件设计细节
人工智能·ui·语言模型·小程序
草根站起来14 小时前
微信小程序request net:ERR_CERT_DATE_INVALID
微信小程序·小程序
小北的AI科技分享16 小时前
广州小程序平台推荐:2026年本地商家数字化选型深度测评
小程序·广州小程序平台
MageGojo17 小时前
10 种主题随机诗词:一个 API 解决小程序的诗词内容源
python·小程序·古诗词·api 接入
青山科技分享17 小时前
2026北京小程序平台推荐——本地商家数字化选型全维度解析
小程序·小程序平台推荐
肖有米XTKF86461 天前
肖有米团队开发:青蓝山泉送水模式系统
小程序·团队开发·零售·csdn开发云
double_eggm1 天前
微信小程序7
微信小程序·小程序
程序鉴定师1 天前
上海小程序开发的坚实保障与行业优势解析
大数据·小程序
double_eggm2 天前
微信小程序8
微信小程序·小程序
MageGojo2 天前
小程序每日一谜怎么做:riddle 接口接入示例
windows·小程序·apache·谜语