功能,tab栏切换,左右滑动,swiper 的高度动态化
参考文章uni-app 动态设置 swiper 的高度_uniapp swiper高度__AZU的博客-CSDN博客
关键代码
/** 获取子元素的高度 */
const getSwiperItemHeight = (current = 0) => {
uni.createSelectorQuery()
.in(instance)
.selectAll('.swiper-item-wrap')
.boundingClientRect()
.exec(data => {
swiperHeight.value = data[0][current].height
})
}
onMounted(async () => {
getSwiperItemHeight()
})
html部分
<template>
<view class="content">
<image class="image-bg" src="/static/game/bg.png" />
<scroll-view id="tab" scroll-x="true">
<view v-for="(item,index) in barNameList" :key="item.id" class="tabName" :data-current="index"
@click="ontabtap">
<text class="tabName_text" :class="tabIndex == index?'active_text':''">{{item.name}}</text>
</view>
</scroll-view>
<swiper id="tabContent" :current="tabIndex" @change="tabChange" :style="{height:swiperHeight+'px'}">
<swiper-item>
<scroll-view scroll-y style="height: 100%;" class="nav_item">
<view class="swiper-item-wrap" style="height: 50vh;background-color: #fff;">
1111
</view>
</scroll-view>
</swiper-item>
<swiper-item>
<scroll-view scroll-y style="height: 100%;" class="nav_item ">
<view class="swiper-item-wrap" style="height: 20vh;background-color: #fff;">
1111
</view>
</scroll-view>
</swiper-item>
<swiper-item>
<scroll-view scroll-y style="height: 100%;" class="nav_item ">
<view class="swiper-item-wrap" style="height: 70vh;background-color: #fff;">
1111
<!-- <cloudComputer/> -->
</view>
</scroll-view>
</swiper-item>
</swiper>
</view>
</template>
逻辑部分
<script setup>
import {
ref,
reactive,
getCurrentInstance,
onMounted
} from 'vue'
import {
onShow
} from '@dcloudio/uni-app'
// import home from './page/home.vue' //首页
// import gameLibrary from './page/gameLibrary.vue' //游戏库
// import cloudComputer from './page/cloudComputer.vue' //云电脑
const tabIndex = ref(0) //索引值
const barNameList = reactive([{
name: '首页',
id: '0'
}, {
name: '游戏库',
id: '1'
}, {
name: '云电脑',
id: '2'
}])
const instance = getCurrentInstance()
const swiperHeight = ref() //swiper高度
const swiperItemHeight =ref(80)
// 窗口高度
const windowHeight = ref()
/** 获取屏幕剩余高度 */
const getSwiperHeight = () => {
uni.createSelectorQuery()
.in(instance)
.select('.page-head')
.boundingClientRect()
.exec(res => {
const windowHeight = uni.getSystemInfoSync().windowHeight
// 屏幕高度 - 顶部高度
swiperHeight.value = windowHeight - res[0].height
})
}
/** 获取子元素的高度 */
const getSwiperItemHeight = (current = 0) => {
uni.createSelectorQuery()
.in(instance)
.selectAll('.swiper-item-wrap')
.boundingClientRect()
.exec(data => {
swiperHeight.value = data[0][current].height
})
}
onMounted(async () => {
getSwiperItemHeight()
})
/** Tab点击 */
const ontabtap = (e) => {
let index = e.target.dataset.current || e.currentTarget.dataset.current;
switchTab(index);
}
/** switchTab切换 */
const switchTab = (index) => {
if (tabIndex.value == index) {
return
}
getSwiperItemHeight(index)
tabIndex.value = index;
}
/** swiper滑动时改变下标 */
const tabChange = (e) => {
let index = e.target.current || e.detail.current; // 获取到当前移动到第几个
switchTab(index);
}
</script>
css样式部分
<style lang="scss" scoped>
.content {
.image-bg {
position: absolute;
z-index: -2;
top: 0;
// left: 0;
// right: 0;
// bottom: 0;
// right: 0;
width: 100%;
height: 80vh;
}
.image-bg::before {
content: '';
display: inline-block;
width: 100vw;
height: 20vh;
position: absolute;
top: 60vh;
background: linear-gradient(to top, #FFFFFF 0%, rgba(123, 123, 123, 0.00) 100%);
}
#tab {
width: 100%;
display: flex;
white-space: nowrap;
padding: 40rpx 30rpx 0 30rpx;
position: fixed;
top: 0;
z-index: 99;
.tabName {
color: #fff;
display: inline-block;
height: 80rpx;
line-height: 80rpx;
white-space: nowrap;
}
.tabName_text {
line-height: 60rpx;
margin-right: 35rpx;
flex-shrink: 0;
padding-bottom: 10rpx;
display: flex;
justify-content: center;
font-size: 16px;
padding-top: 10rpx;
}
}
.active_text {
position: relative;
font-weight: 600;
}
.active_text::after {
content: "";
position: absolute;
width: 50rpx;
height: 4rpx;
background-color: #F0B83D;
left: 0px;
right: 0px;
bottom: 0px;
margin: auto;
}
#tabContent {
width: 100%;
position: relative;
top: 120rpx;
.nav_item {
padding-left: 20rpx;
}
}
}
</style>
整体代码
<template>
<view class="content">
<image class="image-bg" src="/static/game/bg.png" />
<scroll-view id="tab" scroll-x="true">
<view v-for="(item,index) in barNameList" :key="item.id" class="tabName" :data-current="index"
@click="ontabtap">
<text class="tabName_text" :class="tabIndex == index?'active_text':''">{{item.name}}</text>
</view>
</scroll-view>
<swiper id="tabContent" :current="tabIndex" @change="tabChange" :style="{height:swiperHeight+'px'}">
<swiper-item>
<scroll-view scroll-y style="height: 100%;" class="nav_item">
<view class="swiper-item-wrap" style="height: 50vh;background-color: #fff;">
1111
</view>
</scroll-view>
</swiper-item>
<swiper-item>
<scroll-view scroll-y style="height: 100%;" class="nav_item ">
<view class="swiper-item-wrap" style="height: 20vh;background-color: #fff;">
1111
</view>
</scroll-view>
</swiper-item>
<swiper-item>
<scroll-view style="height: 100%;" class="nav_item ">
<view class="swiper-item-wrap" style="height: 70vh;background-color: #fff;">
1111
<!-- <cloudComputer/> -->
</view>
</scroll-view>
</swiper-item>
</swiper>
</view>
</template>
<script setup>
import {
ref,
reactive,
getCurrentInstance,
onMounted
} from 'vue'
import {
onShow
} from '@dcloudio/uni-app'
// import home from './page/home.vue' //首页
// import gameLibrary from './page/gameLibrary.vue' //游戏库
// import cloudComputer from './page/cloudComputer.vue' //云电脑
const tabIndex = ref(0) //索引值
const barNameList = reactive([{
name: '首页',
id: '0'
}, {
name: '游戏库',
id: '1'
}, {
name: '云电脑',
id: '2'
}])
const instance = getCurrentInstance()
const swiperHeight = ref() //swiper高度
const swiperItemHeight =ref(80)
// 窗口高度
const windowHeight = ref()
/** 获取屏幕剩余高度 */
const getSwiperHeight = () => {
uni.createSelectorQuery()
.in(instance)
.select('.page-head')
.boundingClientRect()
.exec(res => {
const windowHeight = uni.getSystemInfoSync().windowHeight
// 屏幕高度 - 顶部高度
swiperHeight.value = windowHeight - res[0].height
})
}
/** 获取子元素的高度 */
const getSwiperItemHeight = (current = 0) => {
uni.createSelectorQuery()
.in(instance)
.selectAll('.swiper-item-wrap')
.boundingClientRect()
.exec(data => {
swiperHeight.value = data[0][current].height
})
}
onMounted(async () => {
getSwiperItemHeight()
})
/** Tab点击 */
const ontabtap = (e) => {
let index = e.target.dataset.current || e.currentTarget.dataset.current;
switchTab(index);
}
/** switchTab切换 */
const switchTab = (index) => {
if (tabIndex.value == index) {
return
}
getSwiperItemHeight(index)
tabIndex.value = index;
}
/** swiper滑动时改变下标 */
const tabChange = (e) => {
let index = e.target.current || e.detail.current; // 获取到当前移动到第几个
switchTab(index);
}
</script>
<style lang="scss" scoped>
.content {
.image-bg {
position: absolute;
z-index: -2;
top: 0;
// left: 0;
// right: 0;
// bottom: 0;
// right: 0;
width: 100%;
height: 80vh;
}
.image-bg::before {
content: '';
display: inline-block;
width: 100vw;
height: 20vh;
position: absolute;
top: 60vh;
background: linear-gradient(to top, #FFFFFF 0%, rgba(123, 123, 123, 0.00) 100%);
}
#tab {
width: 100%;
display: flex;
white-space: nowrap;
padding: 40rpx 30rpx 0 30rpx;
position: fixed;
top: 0;
z-index: 99;
.tabName {
color: #fff;
display: inline-block;
height: 80rpx;
line-height: 80rpx;
white-space: nowrap;
}
.tabName_text {
line-height: 60rpx;
margin-right: 35rpx;
flex-shrink: 0;
padding-bottom: 10rpx;
display: flex;
justify-content: center;
font-size: 16px;
padding-top: 10rpx;
}
}
.active_text {
position: relative;
font-weight: 600;
}
.active_text::after {
content: "";
position: absolute;
width: 50rpx;
height: 4rpx;
background-color: #F0B83D;
left: 0px;
right: 0px;
bottom: 0px;
margin: auto;
}
#tabContent {
width: 100%;
position: relative;
top: 120rpx;
.nav_item {
padding-left: 20rpx;
}
}
}
</style>