【经验分享】如何使用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

目录
相关文章
u-view使用轮播组件u-swiper不能正常显示
Swiper 轮播图 | uView 2.0 - 全面兼容nvue的uni-app生态框架 - uni-app UI框架
1023 0
u-view使用轮播组件u-swiper不能正常显示
uni-app实现swiper滑动放大缩小、实现scroll-view与swiper双向联动
uni-app实现swiper滑动放大缩小、实现scroll-view与swiper双向联动
1326 0
|
1月前
|
小程序 容器
微信小程序常用组件的简单使用 view,scroll-view,swiper,swiper-item,text,rich-text,button,image
本文介绍了微信小程序中常用组件的使用方法,包括view、scroll-view、swiper与swiper-item、text与rich-text、button以及image组件。详细解释了各组件的功能、属性以及如何在小程序页面中进行使用。
微信小程序常用组件的简单使用 view,scroll-view,swiper,swiper-item,text,rich-text,button,image
|
3月前
|
开发框架 JavaScript 前端开发
循序渐进BootstrapVue,开发公司门户网站(4)--- 使用b-carousel-slide组件实现图片轮播以及vue-awesome-swiper实现图片滑动展示
循序渐进BootstrapVue,开发公司门户网站(4)--- 使用b-carousel-slide组件实现图片轮播以及vue-awesome-swiper实现图片滑动展示
|
5月前
Nuxt3 实战 (六):Footer 底部布局
这篇文章介绍了开发项目的Footer布局,参考了Nuxt-UI官网的布局。文章从需求拆分开始,讲解了准备工作和组件开发的过程。最后展示了最终效果,并进行了简单的总结,指出下一篇将讨论主体内容的开发。文末提供了Github仓库和线上预览的链接。
Nuxt3 实战 (六):Footer 底部布局
|
5月前
|
小程序 前端开发 定位技术
【微信小程序】-- 常用视图容器类组件介绍 -- view、scroll-view和swiper(六)
【微信小程序】-- 常用视图容器类组件介绍 -- view、scroll-view和swiper(六)
|
5月前
|
Web App开发 小程序 Android开发
Uniapp swiper 滚动图组件
Uniapp swiper 滚动图组件
67 0
|
小程序 前端开发 容器
【微信小程序】常用组件基本使用(view/scroll-view/swiper、text/rich-text、button/image)
【微信小程序】常用组件基本使用(view/scroll-view/swiper、text/rich-text、button/image)
222 0
elementui源码学习之仿写一个el-drawer
elementui源码学习之仿写一个el-drawer
284 0
|
小程序
【笔记】微信小程序组件swiper实现跑马灯(marquee)效果
微信小程序组件swiper实现跑马灯(marquee)效果
1291 0
【笔记】微信小程序组件swiper实现跑马灯(marquee)效果