由于官方給了 swiper 固定高度150,且 swiper-item 都是 absolute 定位,所以實際應用中經常會碰到問題,在此記錄一下修改方式。
<mp-tabs tabs="{{tabs}}" activeTab="{{activeTab}}" swiperClass="weui-tabs-swiper" bindtabclick="onTabCLick" bindchange="onChange" activeClass="tab-bar-title__selected" swiperStyle="height: {{tabSwiperHeight}}px" > <block wx:for="{{tabs}}" wx:for-item="tab" wx:for-index="index" wx:key="index"> <view class="tab-content tab-content-{{index}}" slot="tab-content-{{index}}" > {{tab.title}} </view> </block> </mp-tabs> Page({ data: { tabs: [{title: '首頁'}, {title: '外賣'}, {title: '商超生鮮'}, {title: '購物'}, {title: '美食飲品'}, {title: '生活服務'}, {title: '休閑娛樂'}], activeTab: 0, tabSwiperHeight: 0 }, tabsSwiperHeight() { // tab 組件內的swiper高度自適應問題 let index = this.data.activeTab; let queryDom = wx.createSelectorQuery() queryDom.select('.tab-content-' + index).boundingClientRect().exec(rect => { this.setData({ tabSwiperHeight: rect[0].height }) }) }, onTabCLick(e) { const index = e.detail.index this.setData({activeTab: index}) }, onChange(e) { const index = e.detail.index this.setData({activeTab: index}) this.tabsSwiperHeight(); } } |