uniapp組件之tab選項(xiàng)卡滑動(dòng)切換功能實(shí)現(xiàn)
uniapp組件之tab選項(xiàng)卡滑動(dòng)切換
效果如下:

代碼如下:
<template>
<view class="content">
<view class="nav">
<!-- 選項(xiàng)卡水平方向滑動(dòng),scroll-with-animation是滑動(dòng)到下一個(gè)選項(xiàng)時(shí),有一個(gè)延時(shí)效果 -->
<scroll-view class="tab-scroll" scroll-x="true" scroll-with-animation :scroll-left="scrollLeft">
<view class="tab-scroll_box">
<!-- 選項(xiàng)卡類(lèi)別列表 -->
<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>
<!-- 選項(xiàng)卡內(nèi)容輪播滑動(dòng)顯示,current為當(dāng)前第幾個(gè)swiper子項(xiàng) -->
<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" >
選項(xiàng)卡1頁(yè)面
</view>
</scroll-view>
</swiper-item>
<swiper-item class="swiperitem-content">
<scroll-view scroll-y style="height: 100%;">
<view class="nav_item" >
選項(xiàng)卡2頁(yè)面
</view>
</scroll-view>
</swiper-item>
<swiper-item class="swiperitem-content">
<scroll-view scroll-y style="height: 100%;">
<view class="nav_item" >
選項(xiàng)卡3頁(yè)面
</view>
</scroll-view>
</swiper-item>
<swiper-item class="swiperitem-content">
<scroll-view scroll-y style="height: 100%;">
<view class="nav_item" >
選項(xiàng)卡4頁(yè)面
</view>
</scroll-view>
</swiper-item>
<swiper-item class="swiperitem-content">
<scroll-view scroll-y style="height: 100%;">
<view class="nav_item" >
選項(xiàng)卡5頁(yè)面
</view>
</scroll-view>
</swiper-item>
<swiper-item class="swiperitem-content">
<scroll-view scroll-y style="height: 100%;">
<view class="nav_item" >
選項(xiàng)卡6頁(yè)面
</view>
</scroll-view>
</swiper-item>
</swiper>
</view>
</template>
<script>
export default {
watch:{
// swiper與上面選項(xiàng)卡聯(lián)動(dòng)
currentindex(newval){
this.isActive = newval;
this.scrollLeft = 0;
// 滑動(dòng)swiper后,每個(gè)選項(xiàng)距離其父元素最左側(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:'選項(xiàng)卡一',
},
{
id:2,
name:'選項(xiàng)卡二',
},
{
id:3,
name:'選項(xiàng)卡三',
},
{
id:4,
name:'選項(xiàng)卡四',
},
{
id:5,
name:'選項(xiàng)卡五',
},
{
id:6,
name:'選項(xiàng)卡六',
},
],
contentScrollW: 0, // 導(dǎo)航區(qū)寬度
scrollLeft: 0, // 橫向滾動(dòng)條位置
fullHeight:"",
}
},
mounted() {
var that = this;
//獲取手機(jī)屏幕的高度,讓其等于swiper的高度,從而使屏幕充滿
uni.getSystemInfo({
success: function (res) {
that.fullHeight ="height:" + res.windowHeight + "px";
}
});
// 獲取標(biāo)題區(qū)域?qū)挾?,和每個(gè)子元素節(jié)點(diǎn)的寬度
this.getScrollW()
},
methods: {
// 獲取標(biāo)題區(qū)域?qū)挾?,和每個(gè)子元素節(jié)點(diǎn)的寬度以及元素距離左邊欄的距離
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)前點(diǎn)擊子元素靠左留一個(gè)選項(xiàng)展示,子元素寬度不相同也可實(shí)現(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滑動(dòng)時(shí),獲取其索引,也就是第幾個(gè)
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;
}
/* 隱藏滾動(dòng)條,但依舊具備可以滾動(dòng)的功能 */
/deep/.uni-scroll-view::-webkit-scrollbar {
display: none
}
</style>
注意:css當(dāng)中需要加上以下,為了隱藏滾動(dòng)條,否則會(huì)出現(xiàn)下圖效果
/* 隱藏滾動(dòng)條,但依舊具備可以滾動(dòng)的功能 */
/deep/.uni-scroll-view::-webkit-scrollbar {
display: none
}
補(bǔ)充:uniapp實(shí)現(xiàn)tabs切換(可滑動(dòng))
uniapp實(shí)現(xiàn)tabs切換(可滑動(dòng))

<template>
<view class="body-view">
<!-- 使用scroll-view實(shí)現(xiàn)tabs滑動(dòng)切換 -->
<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: '社會(huì)保障'
},
{
id: 5,
name: '民生熱點(diǎn)'
},
{
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: '社會(huì)保障'
},
{
id: 5,
content: '民生熱點(diǎn)'
},
{
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選項(xiàng)卡滑動(dòng)切換的文章就介紹到這了,更多相關(guān)uniapp tab選項(xiàng)卡滑動(dòng)切換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 在uniapp中實(shí)現(xiàn)中英文切換的方法
- uniapp?動(dòng)態(tài)組件實(shí)現(xiàn)Tabs標(biāo)簽切換組件(喜馬拉雅app作為案例)
- vue、uniapp實(shí)現(xiàn)組件動(dòng)態(tài)切換效果
- uniapp單頁(yè)面實(shí)現(xiàn)頁(yè)面切換的使用示例
- uniapp實(shí)現(xiàn)tabs切換(可滑動(dòng))效果實(shí)例
- uniapp實(shí)現(xiàn)全局設(shè)置字體大小(小中大的字體切換)
- Uniapp 實(shí)現(xiàn)頂部標(biāo)簽頁(yè)切換功能(詳細(xì)步驟)
相關(guān)文章
Google Map API更新實(shí)現(xiàn)用戶自定義標(biāo)注坐標(biāo)
由于工作需要,又要開(kāi)始看Google Map API 代碼,今天再把我之前的GoogleMap類(lèi),又更新了下,加了個(gè)簡(jiǎn)單的用戶自定義標(biāo)注坐標(biāo)的功能。看看吧(代碼沒(méi)怎么優(yōu)化,別見(jiàn)笑)2009-07-07
如何給ss bash 寫(xiě)一個(gè) WEB 端查看流量的頁(yè)面
由于剛畢業(yè)的窮大學(xué)生,和朋友合租了一臺(tái)服務(wù)器開(kāi)了多個(gè)端口提供 ss 服務(wù),懶得配置 ss-panel,就使用了 ss-bash 來(lái)監(jiān)控不同端口的流量,但每次都要等上服務(wù)器才能看到流量使用情況,很麻煩,于是就寫(xiě)了個(gè)簡(jiǎn)單的頁(yè)面來(lái)提供 WEB 訪問(wèn),具體內(nèi)容一起通過(guò)本文學(xué)習(xí)吧2017-03-03
BootStrap實(shí)現(xiàn)郵件列表的分頁(yè)和模態(tài)框添加郵件的功能
這篇文章主要介紹了bootstrap分頁(yè),模態(tài)框,實(shí)現(xiàn)郵件列表的分頁(yè),和模態(tài)框添加郵件的功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
解決layui 復(fù)選框等內(nèi)置控件不顯示的問(wèn)題
今天小編就為大家分享一篇解決layui 復(fù)選框等內(nèi)置控件不顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08

