uniapp組件之tab選項卡滑動切換功能實現(xiàn)
更新時間:2023年01月31日 16:11:45 作者:薇森
這篇文章主要介紹了uniapp組件之tab選項卡滑動切換功能實現(xiàn),本文結(jié)合實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
uniapp組件之tab選項卡滑動切換
效果如下:

代碼如下:
<template>
<view class="content">
<view class="nav">
<!-- 選項卡水平方向滑動,scroll-with-animation是滑動到下一個選項時,有一個延時效果 -->
<scroll-view class="tab-scroll" scroll-x="true" scroll-with-animation :scroll-left="scrollLeft">
<view class="tab-scroll_box">
<!-- 選項卡類別列表 -->
<view class="tab-scroll_item" v-for=" (item,index) in category" :key="index" :class="{'active':isActive==index}" @click="chenked(index)">
{{item.name}}
</view>
</view>
</scroll-view>
</view>
<!-- 選項卡內(nèi)容輪播滑動顯示,current為當(dāng)前第幾個swiper子項 -->
<swiper @change="change" :current="isActive" class="swiper-content" :style="fullHeight">
<swiper-item class="swiperitem-content">
<scroll-view scroll-y style="height: 100%;">
<view class="nav_item" >
選項卡1頁面
</view>
</scroll-view>
</swiper-item>
<swiper-item class="swiperitem-content">
<scroll-view scroll-y style="height: 100%;">
<view class="nav_item" >
選項卡2頁面
</view>
</scroll-view>
</swiper-item>
<swiper-item class="swiperitem-content">
<scroll-view scroll-y style="height: 100%;">
<view class="nav_item" >
選項卡3頁面
</view>
</scroll-view>
</swiper-item>
<swiper-item class="swiperitem-content">
<scroll-view scroll-y style="height: 100%;">
<view class="nav_item" >
選項卡4頁面
</view>
</scroll-view>
</swiper-item>
<swiper-item class="swiperitem-content">
<scroll-view scroll-y style="height: 100%;">
<view class="nav_item" >
選項卡5頁面
</view>
</scroll-view>
</swiper-item>
<swiper-item class="swiperitem-content">
<scroll-view scroll-y style="height: 100%;">
<view class="nav_item" >
選項卡6頁面
</view>
</scroll-view>
</swiper-item>
</swiper>
</view>
</template>
<script>
export default {
watch:{
// swiper與上面選項卡聯(lián)動
currentindex(newval){
this.isActive = newval;
this.scrollLeft = 0;
// 滑動swiper后,每個選項距離其父元素最左側(cè)的距離
for (let i = 0; i < newval - 1; i++) {
this.scrollLeft += this.category[i].width
};
},
},
data() {
return {
isActive: 0,
index: 0,
currentindex:0,
category:[
{
id:1,
name:'選項卡一',
},
{
id:2,
name:'選項卡二',
},
{
id:3,
name:'選項卡三',
},
{
id:4,
name:'選項卡四',
},
{
id:5,
name:'選項卡五',
},
{
id:6,
name:'選項卡六',
},
],
contentScrollW: 0, // 導(dǎo)航區(qū)寬度
scrollLeft: 0, // 橫向滾動條位置
fullHeight:"",
}
},
mounted() {
var that = this;
//獲取手機(jī)屏幕的高度,讓其等于swiper的高度,從而使屏幕充滿
uni.getSystemInfo({
success: function (res) {
that.fullHeight ="height:" + res.windowHeight + "px";
}
});
// 獲取標(biāo)題區(qū)域?qū)挾?,和每個子元素節(jié)點的寬度
this.getScrollW()
},
methods: {
// 獲取標(biāo)題區(qū)域?qū)挾?,和每個子元素節(jié)點的寬度以及元素距離左邊欄的距離
getScrollW() {
const query = uni.createSelectorQuery().in(this);
query.select('.tab-scroll').boundingClientRect(data => {
// 拿到 scroll-view 組件寬度
this.contentScrollW = data.width
}).exec();
query.selectAll('.tab-scroll_item').boundingClientRect(data => {
let dataLen = data.length;
for (let i = 0; i < dataLen; i++) {
// scroll-view 子元素組件距離左邊欄的距離
this.category[i].left = data[i].left;
// scroll-view 子元素組件寬度
this.category[i].width = data[i].width
}
}).exec()
},
// 當(dāng)前點擊子元素靠左留一個選項展示,子元素寬度不相同也可實現(xiàn)
chenked(index) {
this.isActive = index;
this.scrollLeft = 0;
for (let i = 0; i < index - 1; i++) {
this.scrollLeft += this.category[i].width
};
},
// swiper滑動時,獲取其索引,也就是第幾個
change(e){
const {current} = e.detail;
this.currentindex = current;
},
}
}
</script>
<style lang="scss">
page{
height: 100%;
display: flex;
background-color: #FFFFFF;
}
.content{
display: flex;
flex-direction: column;
width: 100%;
flex: 1;
.nav{
border-top: 1rpx solid #f2f2f2;
background-color: #fceeee;
position: fixed;
z-index: 99;
width: 100%;
align-items: center;
height: 100rpx;
.tab-scroll{
flex: 1;
overflow: hidden;
box-sizing: border-box;
padding-left: 30rpx;
padding-right: 30rpx;
.tab-scroll_box{
display: flex;
align-items: center;
flex-wrap: nowrap;
box-sizing: border-box;
.tab-scroll_item{
line-height: 60rpx;
margin-right: 35rpx;
flex-shrink: 0;
padding-bottom:10px;
display: flex;
justify-content: center;
font-size: 16px;
padding-top: 10px;
}
}
}
}
.swiper-content{
padding-top: 120rpx;
flex: 1;
.swiperitem-content{
background-color: #ffffff;
.nav_item{
background-color: #FFFFFF;
padding:20rpx 40rpx 0rpx 40rpx ;
}
}
}
}
.active {
position: relative;
color: #ff0000;
font-weight: 600;
}
.active::after {
content: "";
position: absolute;
width: 130rpx;
height: 4rpx;
background-color: #ff0000;
left: 0px;
right: 0px;
bottom: 0px;
margin: auto;
}
/* 隱藏滾動條,但依舊具備可以滾動的功能 */
/deep/.uni-scroll-view::-webkit-scrollbar {
display: none
}
</style>
注意:css當(dāng)中需要加上以下,為了隱藏滾動條,否則會出現(xiàn)下圖效果
/* 隱藏滾動條,但依舊具備可以滾動的功能 */
/deep/.uni-scroll-view::-webkit-scrollbar {
display: none
}
補充:uniapp實現(xiàn)tabs切換(可滑動)
uniapp實現(xiàn)tabs切換(可滑動)

<template>
<view class="body-view">
<!-- 使用scroll-view實現(xiàn)tabs滑動切換 -->
<scroll-view class="top-menu-view" scroll-x="true" :scroll-into-view="tabCurrent">
<view class="menu-topic-view" v-for="item in tabs" :id="'tabNum'+item.id" :key="(item.id - 1)" @click="swichMenu(item.id - 1)">
<view :class="currentTab==(item.id - 1) ? 'menu-topic-act' : 'menu-topic'">
<text class="menu-topic-text">{{item.name}}</text>
<view class="menu-topic-bottom">
<view class="menu-topic-bottom-color"></view>
</view>
</view>
</view>
</scroll-view>
<!-- 內(nèi)容 -->
<swiper class="swiper-box-list" :current="currentTab" @change="swiperChange">
<swiper-item class="swiper-topic-list" v-for="item in swiperDateList" :key="item.id">
<view class="swiper-item">
{{item.content}}
</view>
</swiper-item>
</swiper>
</view>
</template>
<script>
export default {
data() {
return {
tabs: [{
id: 1,
name: '推薦'
},
{
id: 2,
name: '交通交通'
},
{
id: 3,
name: '住房'
},
{
id: 4,
name: '社會保障'
},
{
id: 5,
name: '民生熱點'
},
{
id: 6,
name: '即日頭條'
},
{
id: 7,
name: '新聞聯(lián)播'
},
],
currentTab: 0,
tabCurrent: 'tabNum1',
// Tab切換內(nèi)容
swiperDateList: [{
id: 1,
content: '推薦'
},
{
id: 2,
content: '交通交通'
},
{
id: 3,
content: '住房'
},
{
id: 4,
content: '社會保障'
},
{
id: 5,
content: '民生熱點'
},
{
id: 6,
content: '即日頭條'
},
{
id: 7,
content: '新聞聯(lián)播'
},
],
}
},
onLoad() {
},
methods: {
swichMenu(id) {
this.currentTab = id
console.log(11,id)
this.tabCurrent = 'tabNum'+ id
},
swiperChange(e) {
console.log(22,e.detail.current)
let index = e.detail.current
this.swichMenu(index)
}
}
}
</script>
<style scoped lang="scss">
.body-view {
height: 100vh;
width: 100%;
display: flex;
flex: 1;
flex-direction: column;
overflow: hidden;
align-items: flex-start;
justify-content: center;
}
.top-menu-view {
display: flex;
position: fixed;
top: 100rpx;
left: 0;
white-space: nowrap;
width: 100%;
background-color: #FFFFFF;
height: 86rpx;
line-height: 86rpx;
border-top: 1rpx solid #d8dbe6;
.menu-topic-view {
display: inline-block;
white-space: nowrap;
height: 86rpx;
position: relative;
.menu-topic-text {
font-size: 30rpx;
color: #303133;
padding: 10rpx 40rpx;
}
// .menu-topic-act {
// margin-left: 30upx;
// margin-right: 10upx;
// position: relative;
// height: 100%;
// display: flex;
// align-items: center;
// justify-content: center;
// }
.menu-topic-bottom {
position: absolute;
bottom: 0;
width: 100%;
.menu-topic-bottom-color {
width: 40rpx;
height: 4rpx;
}
}
.menu-topic-act .menu-topic-bottom {
display: flex;
justify-content: center;
}
.menu-topic-act .menu-topic-bottom-color {
background: #3d7eff;
}
}
}
.swiper-box-list {
width: 100%;
padding-top: 200rpx;
flex:1;
background-color: #FFFFFF;
.swiper-topic-list {
width: 100%;
}
}
</style>
繼續(xù)加油呀~
到此這篇關(guān)于uniapp組件之tab選項卡滑動切換的文章就介紹到這了,更多相關(guān)uniapp tab選項卡滑動切換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Javascript圖像處理—圖像形態(tài)學(xué)(膨脹與腐蝕)
上一篇文章,我們講解了圖像處理中的閾值函數(shù),這一篇文章我們來做膨脹和腐蝕函數(shù)2013-01-01
JS如何實現(xiàn)在頁面上快速定位(錨點跳轉(zhuǎn)問題)
本篇文章主要介紹了JS如何實現(xiàn)在頁面上快速定位(錨點跳轉(zhuǎn)問題),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08

