在对于uni-app框架了解之后,今天就实现一个滚动切换tab效果,这个很常见的一个效果,最后封装成一个组件,便于以后使用,写这个需要引入uni官方提供的uni.css样式,用到了写好的样式,就不需要自己写了

 

 

 这种切换无论是在app端还是小程序或者H5页面都是很常见的功能。对于这种功能,为单独封装成功组件,方便每个页面都能用到,

tab顶部导航栏

页面布局,使用uni-app提供的scroll-view组件。

  1. <template>
  2. <view class="uni-tab-bar">
  3. <scroll-view class="uni-swiper-tab" scroll-x>
  4. <block v-for="(tab,index) in tabBars" :key="tab.id" :style="scrollStyle">
  5. <view
  6. class="swiper-tab-list"
  7. :class="{\'active\' : tabIndex==index}"
  8. @tap="tabtap(index)"
  9. :style="scrollItemStyle"
  10. >
  11. {{tab.name}} {{tab.num?tab.num:""}}
  12. <view class="swiper-tab-line"></view>
  13. </view>
  14. </block>
  15. </scroll-view>
  16. </view>
  17. </template>

这个页面相当于封装一个组件,便于其他他们调用使用,tabIndex这个是tab内容,tabIndex对应的索引值,表示第几个。scrollStyle父级样式设置,scrollItemStyle每一个tab内容样式设置

  1. <script>
  2. export default {
  3. props:{
  4. tabBars:Array,
  5. tabIndex:Number,
  6. scrollStyle:{
  7. type:String,
  8. default:""
  9. },
  10. scrollItemStyle:{
  11. type:String,
  12. default:""
  13. }
  14. },
  15. methods:{
  16. //点击切换导航
  17. tabtap(index){
  18. // this.tabIndex = index;
  19. this.$emit(\'tabtap\',index)
  20. }
  21. }
  22. }
  23. </script> 

样式

  1. <style scoped >
  2. .uni-swiper-tab{
  3. border-bottom: 1upx solid #EEEEEE;
  4. }
  5. .swiper-tab-list{
  6. color: #969696;
  7. font-weight: bold;
  8. }
  9. .uni-tab-bar .active{
  10. color: #343434;
  11. }
  12. .active .swiper-tab-line{
  13. border-bottom: 6upx solid #FEDE33;
  14. width: 70upx;
  15. margin: auto;
  16. border-top: 6upx solid #FEDE33;
  17. border-radius: 20upx;
  18. }
  19. </style>

tabtap点击切换效果,自定义一个tabtap事件,传递给父级,和vue语法一样

在父级组件

1.第一步先引入上面封装好的组件,

  1. import swiperTabHead from "../../components/index/swiper-tab-head.vue";
  2. 注册组件
  3. components:{
  4. swiperTabHead,
  5. }

2.使用组件

  1. <swiperTabHead :tabBars="tabBars" :tabIndex="tabIndex" @tabtap="tabtap"></swiperTabHead>

3.在data定义好数据

  1. export default {
  2. data(){
  3. tabIndex:0,// 选中的
  4. tabBars:[
  5. { name:"关注",id:"guanzhu" },
  6. { name:"推荐",id:"tuijian" },
  7. { name:"体育",id:"tiyu" },
  8. { name:"热点",id:"redian" },
  9. { name:"财经",id:"caijing" },
  10. { name:"娱乐",id:"yule" },
  11. ]
  12. }
  13. }

4.在方法中使用传过来的事件

  1. //接受子组件传过来的值点击切换导航
  2. tabtap(index){
  3. this.tabIndex = index;
  4. },

切换内容,直接在父组件写

  1. <view class="uni-tab-bar">
  2. <swiper class="swiper-box" :style="{height:swiperheight+\'px\'}" :current="tabIndex" @change="tabChange">
  3. <swiper-item v-for="(items,index) in newslist" :key="index">
  4. <scroll-view scroll-y class="list">
  5. <template v-if="items.list.length > 0">
  6. <!-- 图文列表 -->
  7. <block v-for="(item,index1) in items.list" :key="index1">
  8. <view>{{item}}</view>
  9. </block>
  10. </template>
  11. </scroll-view>
  12. </swiper-item>
  13. </swiper>
  14. </view>

5.也是需要在data定义一下内容,newslist是循环遍历的tab内容的内容,大概数据结构就是这样的,swiperheight这个是需要动态计算可视区域内容的高度

  1. swiperheight:500,//高度
  2. newslist:[
  3. {
  4. list:[
  5. 数据内天
  6. ]
  7. },
  8. {},
  9. {},
  10. {},
  11. {},
  12. {}
  13. ]

6.在methods方法中写手动切换的效果

  1. //滑动切换导航
  2. tabChange(e){
  3. this.tabIndex = e.detail.current;
    },

 7.动态计算高度,upx2x是吧px转换成upx,调用这个api,需要在onLoad生命周期写

  1. onLoad() {
  2. uni.getSystemInfo({
  3. success:(res)=>{
  4. let height = res.windowHeight-uni.upx2px(100)
  5. this.swiperheight = height;
  6. }
  7. })
    },

  以上就是用uni-app实现的一个tab切换的效果,可以使用任何平台,已经测试几个平台,都没有问题,

版权声明:本文为zhoulifeng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zhoulifeng/p/11613430.html