uni-app 动态设置 swiper 的高度 自定义Tab

 功能,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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘海583

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值