当前位置:   article > 正文

uni-app滑动/点击切换tab组件,动态数据内容获取swiper动态高度_

可以点击切换tab也可以滑动切换tab

1.定义一个tabbar.vue,代码如下

  1. <template>
  2.     <view class="tabs">
  3.         <view style="position:fixed;z-index:20;">
  4.             <scroll-view id="tab-bar" class="scroll-h" :scroll-x="true" :show-scrollbar="false" :scroll-into-view="scrollInto">
  5.                 <view style="" :class="{'tabCenter':tabBars.length<4}">
  6.                     <view v-for="(tab,index) in tabBars" :key="tab.id" class="uni-tab-item" :id="tab.id"
  7.                         :data-current="index" @click="ontabtap">
  8.                         <text class="uni-tab-item-title"
  9.                             :class="tabIndex==index ? 'uni-tab-item-title-active' : ''">{{tab.name}}<text v-if="tab.count">({{tab.count}})</text></text>
  10.                         <view class="uni-tab-item-line" v-if="tabIndex==index"></view>
  11.                     </view>
  12.                 </view>
  13.             </scroll-view>
  14.             <slot name="center"></slot>
  15.         </view>
  16.         <swiper :current="tabIndex" class="swiper-box" style="flex: 1;min-height:calc(100vh - 200rpx - 142rpx);margin-top:184rpx;" :duration="300" @animationfinish="ontabchange"
  17.             :style="{height: swiperHeight+'px'}">
  18.             <swiper-item class="swiper-item" v-for="(item,i) in tabBars">
  19.                 <scroll-view class="scroll-v" :class="'list'+(i+1)" enableBackToTop="true" scroll-y>
  20.                     <slot name='list{{i+1}}'></slot>
  21.                 </scroll-view>
  22.             </swiper-item>
  23.         </swiper>
  24.     </view>
  25. </template>
  26. <script>
  27.     export default {
  28.         props: {
  29.             tabBars: {
  30.                 type: Array,
  31.                 default: () => {
  32.                     return []
  33.                 }
  34.             },
  35.         },
  36.         data() {
  37.             return {
  38.                 tabIndex: 0,
  39.                 swiperHeight:0,
  40.                 scrollInto:'',
  41.             };
  42.         },
  43.         watch:{
  44.         },
  45.         methods: {
  46.             ontabtap(e) {
  47.                 let index = e.target.dataset.current || e.currentTarget.dataset.current;
  48.                 this.tabIndex = index;
  49.                 this.scrollInto = this.tabBars[index].id;
  50.             },
  51.             ontabchange(e){
  52.                 let index = e.detail.current;
  53.                 this.tabIndex = index;
  54.                 this.scrollInto = this.tabBars[index].id;
  55.                 this.$emit('changeTabbar',this.tabIndex)
  56.             },
  57.             //动态获取高度(swiper组件高度固定,动画获取数据需要动态获取高度)
  58.             getElementHeight(element) {
  59.                 //一定要 this.$nextTick 完成之后在获取dom节点高度
  60.                 setTimeout(() => {
  61.                     this.$nextTick(() => {
  62.                         let query = uni.createSelectorQuery().in(this);
  63.                         query.select(element).boundingClientRect(data => {
  64.                             // console.log(111,data)
  65.                             if (!data) { //如果没获取到,再调一次
  66.                                 this.getElementHeight('.list');
  67.                             } else {
  68.                                 this.swiperHeight = data.height;
  69.                                 //关闭loading
  70.                                 this.$emit('closeLoading')
  71.                             }
  72.                         }).exec()
  73.                     })
  74.                 }, 10)
  75.             },
  76.         }
  77.     }
  78. </script>
  79. <style lang="scss" scoped>
  80.     .tabs {
  81.         flex: 1;
  82.         flex-direction: column;
  83.         overflow: hidden;
  84.         height:100%;
  85.         overflow-y: scroll;
  86.         .scroll-h {
  87.             width: 750rpx;
  88.             /* #ifdef H5 */
  89.             width: 100%;
  90.             /* #endif */
  91.             flex-direction: row;
  92.             /* #ifndef APP-PLUS */
  93.             white-space: nowrap;
  94.             /* #endif */
  95.             flex-wrap: nowrap;
  96.             background-color: #fff;
  97.         }
  98.         .uni-tab-item {
  99.             /* #ifndef APP-PLUS */
  100.             display: inline-block;
  101.             /* #endif */
  102.             flex-wrap: nowrap;
  103.             padding-left: 34rpx;
  104.             padding-right: 34rpx;
  105.         }
  106.         .uni-tab-item-line {
  107.             width: 54rpx;
  108.             height: 6rpx;
  109.             background: #1975F7;
  110.             border-radius: 3rpx 3rpx 3rpx 3rpx;
  111.             margin: 0 auto;
  112.         }
  113.         .uni-tab-item-title {
  114.             color: #555;
  115.             font-size: 30rpx;
  116.             height: 60rpx;
  117.             line-height: 60rpx;
  118.             flex-wrap: nowrap;
  119.             /* #ifndef APP-PLUS */
  120.             white-space: nowrap;
  121.             /* #endif */
  122.         }
  123.         .uni-tab-item-title-active {
  124.             color: #1975F7;
  125.         }
  126.         .swiper-box {
  127.             flex: 1;
  128.         }
  129.         .swiper-item {
  130.             flex: 1;
  131.             flex-direction: row;
  132.         }
  133.         .scroll-v {
  134.             flex: 1;
  135.             /* #ifndef MP-ALIPAY */
  136.             flex-direction: column;
  137.             /* #endif */
  138.             width: 750rpx;
  139.             width: 100%;
  140.         }
  141.         .tabCenter {
  142.             display: flex;
  143.             align-items: center;
  144.             justify-content: center;
  145.         }
  146.     }
  147. </style>

2.在界面引入,使用

  1. //引入组件
  2. import tabbarPage from '../components/tabbar/index.vue';
  3. //使用
  4. <tabbar-page :tabBars="tabBarLists" ref="tabBarPage" @changeTabbar="changeTabbar">
  5. <template slot="list1"> //写对应界面的代码 </template>
  6. <template slot="list2"> //写对应界面的代码</template>
  7. <template slot="list3"> //写对应界面的代码</template>
  8. </tabbar-page>

3.注意。如果切换界面对应的是动态数据,swiper因为高度固定,需要获取动态高度

数据请求到之后调用组件接口,代码如下

  1. setTimeout(() => {
  2. this.$nextTick(() => {
  3. this.$refs.tabBarPage.getElementHeight('.list1')
  4. })
  5. }, 10)


**uni-app滑动/点击切换tab组件,动态数据内容获取swiper动态高度**

**uni-app滑动/点击切换tab组件,动态数据内容获取swiper动态高度**

**uni-app滑动/点击切换tab组件,动态数据内容获取swiper动态高度**

**uni-app滑动/点击切换tab组件,动态数据内容获取swiper动态高度**

**uni-app滑动/点击切换tab组件,动态数据内容获取swiper动态高度**

**uni-app滑动/点击切换tab组件,动态数据内容获取swiper动态高度**

**uni-app滑动/点击切换tab组件,动态数据内容获取swiper动态高度**

**uni-app滑动/点击切换tab组件,动态数据内容获取swiper动态高度**

**uni-app滑动/点击切换tab组件,动态数据内容获取swiper动态高度**

**uni-app滑动/点击切换tab组件,动态数据内容获取swiper动态高度**

**uni-app滑动/点击切换tab组件,动态数据内容获取swiper动态高度**

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/312425
推荐阅读
相关标签