<view class="tab-container">
<scroll-view scroll-x scroll-with-animation class="tabs" :scroll-left="scrollLeft">
<view class="tab" :class="{'active':currentTab ===index }" v-for="(item,index) in tabs" :key="index" @tap="switchTab(index)">
{{item.name }}
</view>
</scroll-view>
<button v-for="(i,v) in tabs[currentTab].subTags" @click="tiao">{{i}}</button>
</view>
</template>
<script>
import RecommendInfoApi from "@/api/RecommendInfoList";
export default {
data() {
return {
tabs:[],
currentTab:0,// 当前选中的tab索引
scrollLeft: -1,// scroll-view的scroll-left值
tabRects:[],
scrollRect:[],
screenWidth:0,
indexId:0,
isFirstLoad:true,
isFirstSwitchTab:true,
initialTabIndex:0,// 初始需要滚动到的.tab索引
};
},
onLoad(option) {
this.indexId=Number(option.id)
},
onShow() {
this.getShareRecommendInfo()
this.initialTabIndex =this.indexId;// 设置初始需要滚动到的.tab索引
if (this.indexId &&this.isFirstSwitchTab) {
this.switchTab(this.indexId);
this.isFirstSwitchTab =false;
}
},
methods:{
async getShareRecommendInfo() {
uni.showLoading();
try {
const res =await RecommendInfoApi.getShareRecommendInfo();
const recommendInfoList =res.data.data.getShareRecommendInfoList;
this.tabs =recommendInfoList
if (this.isFirstLoad) {
this.calculateTabPositions();
this.isFirstLoad =false;
}
uni.hideLoading();
} catch (error) {
console.error('Failed to fetch share recommend info:',error);
}
},
switchTab(index) {
console.log(index)
this.currentTab =index;
this.adjustScroll();// 点击tab后调整scroll-view的位置
const tabRect =this.tabRects[index];
if (tabRect &&this.scrollRect) {
const screenWidth =tabRect.left -(this.scrollRect.width -tabRect.width) /2;
this.scrollLeft =screenWidth;
} else {
this.scrollLeft =0;
}
},
calculateTabPositions() {
wx.nextTick(() =>{
const query =wx.createSelectorQuery().in(this);
query.selectAll(".tab").boundingClientRect();
query.select(".tabs").boundingClientRect();
query.exec((rects) =>{
this.tabRects =rects[0];
this.scrollRect =rects[1];
if (this.initialTabIndex >=0 &&this.initialTabIndex
const tabRect =this.tabRects[this.initialTabIndex];
if (tabRect &&this.scrollRect) {
const screenWidth =tabRect.left -(this.scrollRect.width -tabRect.width) /2;
this.scrollLeft =screenWidth;
}
}
});
});
},
adjustScroll() {
this.scrollLeft =0;// 每次切换tab时重置scroll-left值
},
tiao(){
uni.navigateTo({
url:"/pages/shopcart/navcart",
fail(err) {
console.log(err);
},
});
}
},
};
</script>
<style scoped>
.tab-container {
width:100%;
height:40px;
background-color:#f8f8f8;
}
.tabs {
width:100%;
white-space:nowrap;
overflow-x:auto;
}
.tab {
display:inline-block;
padding:0 24rpx;
color:#333;
background:pink;
border-radius:36rpx;
transition:opacity 0.3s ease-in-out;/* Add transition animation */
margin:0 10rpx;
height:70rpx;
font-size:30rpx;
font-weight:400;
line-height:70rpx;
}
.tab:nth-child(1){
margin-left:24rpx;
}
.tab.active {
color:#ff0000;
font-weight:700;
}
</style>
使用 uniapp 写微信小程序实现点击 tab 滚动居中位置
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 微信小程序地图实现点击气泡,展示callout,开发过程中遇到的需求,大致就和共享单车那个差不多,在很多个mark...