uniapp Implementation of Sliding Tabs
- 2021-08-31 07:17:59
- OfStack
In this article, we share the specific code of uniapp to realize sliding tab for your reference. The specific contents are as follows
tabControl-tag.vue
<template name="tabControl">
<scroll-view scroll-x="true" :style="'background-color:'+bgc+';top:'+top+'px;'" :class="fixed?'fxied':''" :scroll-left='scrollLeft' scroll-with-animation id="tabcard">
<view class="tabList" :style="isEqually?'display: flex;justify-content: space-between;padding-left:0;':''">
<view
:class="'tabItem'+(currentIndex==index?' thisOpenSelect':'')"
:style="isEqually? 'width:'+windowWidth/values.length+'px;margin-right:0;':''"
v-for="(item,index) in values"
:id="'item'+index"
:key='index'
@click="_onClick(index)">
<text :style="(currentIndex==index?'font-size:'+activeSize+'rpx;color:'+activeColor:'font-size:'+itemSize+'rpx')">{{item}}</text>
<view class="activeLine" :style="{width: lineWidth+'rpx'}"></view>
</view>
</view>
</scroll-view>
</template>
<script>
export default {
name:'tabControl',
props:{
current: {
type: Number,
default: 0
},
values: {
type: Array,
default () {
return []
}
},
bgc:{
type:String,
default:''
},
fixed:{
type:Boolean,
default:false
},
scrollFlag:{
type:Boolean,
default:false
},
lineWidth:{
type:Number,
default: 100
},
itemSize:{
type:Number,
default: 30
},
activeSize:{
type:Number,
default: 32
},
activeColor:{
type:String,
default:''
},
top:{
type:Number,
default: 0
},
isEqually:{
type:Boolean,
default:false
}
},
data() {
return {
currentIndex: 0,
windowWidth:0, // Device width
leftList:[], // The distance from the option to the left
widthList:[], // Option width
scrollLeft:0, // Moving distance
newScroll:0, // Upper 1 Secondary moving distance ( Used to judge whether it is sliding left or right )
wornScroll:0, // Upper 1 Secondary moving distance ( Used to judge whether it is sliding left or right )
};
},
created(){
},
mounted(){
setTimeout(()=>{
uni.createSelectorQuery().in(this).select("#tabcard").boundingClientRect((res)=>{
this.$emit('getTabCardHeight', {height:res.height})
}).exec()
uni.getSystemInfo({
success: (res)=> {
this.windowWidth = res.windowWidth;
// console.log(this.windowWidth);
this.values.forEach((i,v)=>{
let info = uni.createSelectorQuery().in(this);
info.select("#item"+v).boundingClientRect((res)=>{
// Get the 1 The distance from elements to the left
// if(v==0){
// this.startLenght = res.left
// }
this.widthList.push(res.width)
this.leftList.push(res.left)
}).exec()
})
// console.log(this.leftList)
// console.log(this.widthList)
}
});
})
},
created() {
this.currentIndex = this.current
if(this.scrollFlag){
setTimeout(()=>{
this.tabListScroll(this.current)
},300)
}
},
watch: {
current(val) {
if (val !== this.currentIndex) {
this.currentIndex = val
if(this.scrollFlag){
this.tabListScroll(val)
}
}
},
},
methods: {
_onClick(index) {
if (this.currentIndex !== index) {
this.currentIndex = index
this.$emit('clickItem', {currentIndex:index})
// Scroll on
if(this.scrollFlag){
this.tabListScroll(index)
}
}
},
// Option move
tabListScroll(index){
let scoll = 0;
this.wornScroll = index;
// this.wornScroll-this.newScroll>0 Sliding to the left Towards Towards Towards Towards
if(this.wornScroll-this.newScroll>0){
for(let i = 0;i<this.leftList.length;i++){
if(i>1&&i==this.currentIndex){
scoll = this.leftList[i-1]
}
}
// console.log(' Sliding to the left ',scoll)
}else{
if(index>1){
for(let i = 0;i<this.leftList.length;i++){
if(i<index-1){
scoll = this.leftList[i]
}
}
}else{
scoll = 0
}
// console.log(' Sliding to the right ')
}
this.newScroll = this.wornScroll;
this.scrollLeft = scoll;
}
}
}
</script>
<style lang="less" scoped>
.fxied{
position: fixed;
z-index: 2;
}
.tabList{
padding-top: 24rpx;
padding-left: 24rpx;
padding-bottom: 8rpx;
white-space: nowrap;
text-align: center;
.tabItem{
margin-right: 60rpx;
display: inline-block;
position: relative;
text{
// font-size: 30rpx;
line-height: 44rpx;
color: #666;
transition: all 0.3s ease 0s;
}
.activeLine{
// width: 48rpx;
height: 8rpx;
border-radius: 4rpx;
background-color: #F57341;
margin-top: 8rpx;
margin-left: 50%;
transform: translateX(-50%);
opacity: 0;
transition: all 0.3s ease 0s;
}
}
.tabItem:first-child{
// margin-left: 22rpx;
}
.tabItem:last-child{
margin-right: 24rpx;
}
.thisOpenSelect{
text{
color: #333;
font-weight:600;
// font-size: 32rpx;
}
.activeLine{
opacity: 1;
}
}
}
</style>
Page reference
<template>
<view class="page">
<tabControl :current="current" :values="items" bgc="#fff" :fixed="true" :scrollFlag="true" :isEqually="false" @clickItem="onClickItem"></tabControl>
<!-- Use swiper Cooperate Sliding switching -->
<swiper class="swiper" style="height: 100%;" @change="scollSwiper" :current="current">
<swiper-item v-for="(item, index) in items" :key="index">
<!-- Use scroll-view To scroll through the content area -->
<scroll-view scroll-y="true" style="height: 100%;">{{ item }}</scroll-view>
</swiper-item>
</swiper>
</view>
</template>
<script>
import tabControl from '@/components/tabControl-tag/tabControl-tag.vue';
export default {
components: { tabControl },
data() {
return {
items: [' Performance statistics ', ' Tab 2', ' Tab 3', ' Tab 4', ' Tab 5'],
current: 0
};
},
onLoad() {},
methods: {
onClickItem(val) {
this.current = val.currentIndex;
},
scollSwiper(e) {
this.current = e.target.current;
}
}
};
</script>
<style>
page {
height: 100%;
}
.page {
padding-top: 98rpx;
height: 100%;
}
</style>
1. Usage:
scrollFlag-whether to turn on option scrolling (true-turn on false-turn off) according to your own needs. If the option length exceeds the screen length, it is recommended to turn it on
fixed-Fixed Positioning
bgc-Background Color
values--Options array
current--Index of currently selected options
isEqually-whether to turn on the option bisection width (true, false)
lineWidth-Length of underline (may affect the width of the option box in the non-bisect option state-debug the desired effect by yourself, default is 48rpx)
itemSize--Option font size unchecked (default is 30rpx)
activeSize-Select option font size (default is 32rpx)
activeColor--Select option font color (default # 333)
top--Tab Fixed Positioning Custom top Distance
Note:
When using fixed to fix the header, the whole page should be padding-top: 98rpx;; Otherwise, the content area will be covered.
When using swiper to realize sliding switching, you should set page height to 100% swiper height to 100% before you can slide switching in full screen