【经验分享】如何使用scroll-view + swiper制作tabs导航条组件。

简介: 【经验分享】如何使用scroll-view + swiper制作tabs导航条组件。

业务场景中有较多的情况下会用到tabs切换组件,例如某某列表啦,滑动切换,实现起来挺简单,这边按照自己业务内的效果手动撸了个组件。

效果图如下。


组件抛出三个属性进行设置:

currentTab: 当前下标。

color: 文字颜色及导航条颜色

list: 循环tab的列表。支持传入badge字段进行redDot红点设置。


<scroll-view class="scroll-wrapper" scroll-x scroll-with-animation="true" scroll-into-view="item{{currentTab < 4 ? 0 : currentTab - 3}}">
  <view class="navigate-item" id="item{{index}}" a:for="{{list}}" a:key="index" data-index="{{index}}" onTap="handleClick">
    <view class="names {{currentTab === index ? 'active' : ''}}"  style="color: {{currentTab === index ? color : ''}}">{{item.name}}
      <text class="badge" a:if="{{item.badge}}">{{item.badge}}</text>
    </view>
    <view class="currtline {{currentTab === index ? 'active' : ''}}" style="background: {{currentTab === index ? color : ''}}"></view>
  </view>
</scroll-view>
<swiper indicator-dots="{{false}}" onChange=“handleSwiper” current="{{currentTab}}">
  <block a:for="{{list}}" a:key="*this">
    <swiper-item>
      <view class="swiper-item">第{{index + 1}}页</view>
    </swiper-item>
  </block>
</swiper>

至于组件JS

Component({
  props: {
    currentTab: 0, // 当前下标
    list: [], // 导航条列表, redDot配置需加入badge字段。如: tabList: [{ name: '有趣好玩', badge: 20}]
    color: '#00cc88' // 文字及导航条下标
  },
  methods: {
    // 导航条切换
    handleClick(e) {
      let currentTab = e.currentTarget.dataset.index
      this.setData({
        currentTab
      })
    },
    // 滑动swiper切换导航条
    handleSwiper(e) {
      let {
        current,
        source
      } = e.detail
      if (source === 'autoplay' || source === 'touch') {
        const currentTab = current
        this.setData({
          currentTab
        })
      }
    }
  },
});

至于引用的地方就更简单了:

json引用组件配置就不说了,

页面操作如下:

<tabs list="{{tabList}}" color="blue"/>



以上就是一个简单的tabs导航条联动切换效果,有问题欢迎评论指正,代码可自行修改新增属性。

最后放上代码片段:访问运行查看效果

https://pen.mini-code.com/s/8f444464-c2e5-4350-8289-45be27497701

目录
相关文章
uni-app实现swiper滑动放大缩小、实现scroll-view与swiper双向联动
uni-app实现swiper滑动放大缩小、实现scroll-view与swiper双向联动
921 0
u-view使用轮播组件u-swiper不能正常显示
Swiper 轮播图 | uView 2.0 - 全面兼容nvue的uni-app生态框架 - uni-app UI框架
897 0
u-view使用轮播组件u-swiper不能正常显示
|
1月前
|
小程序 前端开发 定位技术
【微信小程序】-- 常用视图容器类组件介绍 -- view、scroll-view和swiper(六)
【微信小程序】-- 常用视图容器类组件介绍 -- view、scroll-view和swiper(六)
|
3月前
|
Web App开发 小程序 Android开发
Uniapp swiper 滚动图组件
Uniapp swiper 滚动图组件
26 0
|
4月前
elementUI el-tabs 实现动态选项卡页面效果
elementUI el-tabs 实现动态选项卡页面效果
|
7月前
element-ui框架的el-dialog弹出框被遮罩层挡住了/el-drawer....会生成v-model元素的组件被遮罩层挡住
element-ui框架的el-dialog弹出框被遮罩层挡住了/el-drawer....会生成v-model元素的组件被遮罩层挡住
uniapp使用button移除边框效果demo(整理)
uniapp使用button移除边框效果demo(整理)
|
9月前
|
容器
Echarts实战案例代码(50):tooltip组件提示框被div折叠的解决方案
Echarts实战案例代码(50):tooltip组件提示框被div折叠的解决方案
71 0
|
10月前
|
小程序 安全 前端开发
微信小程序自定义导航栏机型适配--底部Tabbar--view高度--底部按钮适配
微信小程序自定义导航栏机型适配--底部Tabbar--view高度--底部按钮适配
592 0
elementui源码学习之仿写一个el-drawer
elementui源码学习之仿写一个el-drawer
233 0