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

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



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

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
      })
    }
  },
相关推荐
jingqingdai39 小时前
微信小程序 Canvas 2D 踩坑指南:如何优雅地导出高清长图?(附 AI 辅助实录)
人工智能·微信小程序·小程序
2503_9284115610 小时前
AI 行业正在重塑每个人的工作方式,而你需要一个更轻松的入口
人工智能·小程序
qq_4335021816 小时前
微信小程序更新机制踩坑记录:updateInfo 为什么总是读到旧数据?
微信小程序·小程序·notepad++
优雅的大白鹅1 天前
创建uniapp小程序
小程序·uni-app
liangdabiao1 天前
开源AI拼豆大升级 - 一键部署cloudflare page - 全免费 web和小程序
前端·人工智能·小程序
px不是xp1 天前
DeepSeek API集成:让小程序拥有AI大脑
javascript·人工智能·小程序
小汪说干货1 天前
2026年4月最新|公众号文章插入文档附件3种技术方案
javascript·小程序
QQ22792391021 天前
Java springboot基于微信小程序的智慧旅游导游系统景点门票酒店预订(源码+文档+运行视频+讲解视频)
java·spring boot·微信小程序·maven·vuejs
小汪说干货2 天前
2026年4月优化版|公众号文档格式附件插入3种方案(推荐“附链”小程序)
小程序·微信公众平台
万岳科技系统开发2 天前
商城系统搭建自建平台与入驻第三方平台对比分析
数据库·小程序·架构