uniapp實(shí)現(xiàn)可滑動(dòng)選項(xiàng)卡
本文實(shí)例為大家分享了uniapp實(shí)現(xiàn)可滑動(dòng)選項(xiàng)卡的具體代碼,供大家參考,具體內(nèi)容如下
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, //設(shè)備寬度
leftList:[], //選項(xiàng)距離左邊的距離
widthList:[], //選項(xiàng)寬度
scrollLeft:0, //移動(dòng)距離
newScroll:0, //上一次移動(dòng)距離(用來(lái)判斷是左滑還是右滑)
wornScroll:0, //上一次移動(dòng)距離(用來(lái)判斷是左滑還是右滑)
};
},
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)=>{
// 獲取第一個(gè)元素到左邊的距離
// 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})
// 開(kāi)啟滾動(dòng)
if(this.scrollFlag){
this.tabListScroll(index)
}
}
},
// 選項(xiàng)移動(dòng)
tabListScroll(index){
let scoll = 0;
this.wornScroll = index;
// this.wornScroll-this.newScroll>0 在向左滑 ←←←←←
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('在向左滑',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('在向右滑')
}
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>
頁(yè)面引用
<template>
<view class="page">
<tabControl :current="current" :values="items" bgc="#fff" :fixed="true" :scrollFlag="true" :isEqually="false" @clickItem="onClickItem"></tabControl>
<!-- 使用 swiper 配合 滑動(dòng)切換 -->
<swiper class="swiper" style="height: 100%;" @change="scollSwiper" :current="current">
<swiper-item v-for="(item, index) in items" :key="index">
<!-- 使用 scroll-view 來(lái)滾動(dòng)內(nèi)容區(qū)域 -->
<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: ['業(yè)績(jī)統(tǒng)計(jì)', '選項(xiàng)卡2', '選項(xiàng)卡3', '選項(xiàng)卡4', '選項(xiàng)卡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.使用方式:
scrollFlag --是否開(kāi)啟選項(xiàng)滾動(dòng)(true -開(kāi)啟 false -關(guān)閉) 根據(jù)自己需求如果選項(xiàng)長(zhǎng)度超出屏幕長(zhǎng)度 建議開(kāi)啟
fixed --固定定位
bgc --背景色
values --選項(xiàng)數(shù)組
current --當(dāng)前選中選項(xiàng)索引
isEqually --是否開(kāi)啟選項(xiàng)平分寬度(true,false)
lineWidth --下劃線(xiàn)長(zhǎng)度(在非平分選項(xiàng)狀態(tài)下 可能會(huì)影響選項(xiàng)盒子的寬度-自行調(diào)試想要的效果,默認(rèn)為48rpx)
itemSize --未選中選項(xiàng)字體大小(默認(rèn)為30rpx)
activeSize --選中選項(xiàng)字體大小(默認(rèn)為32rpx)
activeColor --選中選項(xiàng)字體顏色(默認(rèn)#333)
top --選項(xiàng)卡固定定位 自定義top距離
注意:
使用fixed固定頭部的時(shí)候 要將頁(yè)面整體padding-top:98rpx;不然會(huì)蓋住內(nèi)容區(qū)域。
使用swiper實(shí)現(xiàn)滑動(dòng)切換時(shí) 要將page 高度設(shè)置100% swiper高度100% 才可以全屏滑動(dòng)切換
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
webpack-dev-server搭建本地服務(wù)器的實(shí)現(xiàn)
當(dāng)我們使用webpack打包時(shí),發(fā)現(xiàn)每次更新了一點(diǎn)代碼,都需要重新打包,我們希望本地能搭建一個(gè)服務(wù)器,本文就介紹如何使用webpack-dev-server搭建本地服務(wù)器,感興趣的可以了解一下2021-07-07
用JS實(shí)現(xiàn)一個(gè)簡(jiǎn)單的打磚塊游戲
這篇文章主要介紹了用JS實(shí)現(xiàn)一個(gè)簡(jiǎn)單的打磚塊游戲,代碼分為html+css+js三部分,具體實(shí)例代碼感興趣的朋友跟隨小編一起看看吧2019-12-12
JavaScript中變量提升導(dǎo)致未定義(undefined)的問(wèn)題及解決方法
在 JavaScript 中,變量提升(Hoisting)是一個(gè)相對(duì)常見(jiàn)的行為,尤其是當(dāng)你遇到 undefined 錯(cuò)誤時(shí),本文將詳細(xì)探討變量提升的概念、其對(duì)代碼執(zhí)行的影響以及如何避免因?yàn)樽兞刻嵘鴮?dǎo)致 undefined 的問(wèn)題,需要的朋友可以參考下2024-09-09
JS基于面向?qū)ο髮?shí)現(xiàn)的拖拽功能示例
這篇文章主要介紹了JS基于面向?qū)ο髮?shí)現(xiàn)的拖拽功能,涉及javascript面向?qū)ο笈c頁(yè)面元素動(dòng)態(tài)操作的相關(guān)技巧,需要的朋友可以參考下2016-12-12
js實(shí)現(xiàn)視頻播放時(shí)屏幕顯示水印
這篇文章主要為大家詳細(xì)介紹了js如何實(shí)現(xiàn)視頻播放時(shí)屏幕顯示水印的效果,文中的示例代碼講解詳細(xì),對(duì)我們深入掌握js有一定的幫助,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
JS實(shí)現(xiàn)頁(yè)面內(nèi)跳轉(zhuǎn)的簡(jiǎn)單代碼
這篇文章主要介紹了JS實(shí)現(xiàn)頁(yè)面內(nèi)跳轉(zhuǎn)的簡(jiǎn)單代碼,需要的朋友可以參考下2017-09-09
JavaScript中為什么null==0為false而null大于=0為true(個(gè)人研究)
今天閑來(lái)沒(méi)啥事,研究了一下有關(guān)“null”和“0”的關(guān)系。希望大家看完了能有所收獲,在此與大家分享下,希望也可以受益匪淺2013-09-09
BootStrap智能表單實(shí)戰(zhàn)系列(七)驗(yàn)證的支持
這篇文章主要介紹了BootStrap智能表單實(shí)戰(zhàn)系列(七)驗(yàn)證的支持 ,凡是涉及到用戶(hù)編輯信息然后保存的頁(yè)面,都涉及到一個(gè)數(shù)據(jù)是否符合要求的檢查,需要客服端和服務(wù)器端的校驗(yàn)的問(wèn)題,本文介紹非常詳細(xì),具有參考價(jià)值,需要的朋友可以參考下2016-06-06

