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

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



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

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
      })
    }
  },
相关推荐
lhldsg5 小时前
社区健身场地规划实战指南:从器材配置到智能化管理经验分享
java·开发语言·经验分享·小程序
Haibakeji7 小时前
APP小程序定制开发项目频繁延期?前期规避方法与落地避坑指南
小程序·uni-app·软件需求
show4339 小时前
2026小程序端AI智能优化技术实践:转文字后自动纠错+润色+分段算法分析
人工智能·算法·小程序
小小谈电商10 小时前
小程序商城源码技术选型:启山智软 / CRMEB / Mall4j 三家深度拆解
小程序
2501_9160074710 小时前
申请 iOS 推送证书并配置 APNs 群发推送教程
android·ios·小程序·https·uni-app·iphone·webview
weixin_BYSJ19871 天前
springboot技能与工具共享小程序---附源码29657
java·javascript·spring boot·python·小程序·django·php
weixin_BYSJ19871 天前
flask民族服饰饰品商城小程序---附源码37399
java·javascript·spring boot·python·小程序·django·php
show4331 天前
2026小程序工具生态技术趋势:轻量化免费替代桌面软件的路径解析
小程序
码云之上2 天前
小程序 iOS 多输入框焦点乱序脱坑记
ios·微信小程序·taro
ON.LIN2 天前
云边去水印小程序详细搭建教程(附源码)
小程序