uniapp 实现顶部菜单scroll-view与swiper每个滑动页面都自适应

效果图:

 

  tab栏可以滑动,切换页面跟随tab栏同步滑动。这里需要注意的是使用swiper组件时,它会有一个默认的高度,你必须动态的获取数据列表的高度覆盖原来的默认高度。

经过上一篇的问题,切换页面高度都统一了,现在进行了处理切换每个页面高度都自适应当前高度。

最下面有精简代码

swiper 滑块视图容器

current 当前index索引

swiper-item 仅可放置在 <swiper> 组件中

示例

<swiper :current="currentTab" @change="bindChange">
    <swiper-item>
        //容器里的内容....
    </swiper-item>
</swiper>

注:<u-alert>和<u-swiper> 为uview,需要到官方导入组件uView2.0组件导入

如不需要删掉<u-alert>和<u-swiper>代码,不然会报错

代码

html

<template>
	<view>
		<scroll-view class="scroll1" scroll-x="true">
			<view :class="currentTab==index ? 'select' : 'select1'" :data-current="index" @click="swichNav"
				v-for="(item,index) in scoll" :key='index'>{{item.txt}}
			</view>
		</scroll-view>
		<view v-if="infos == 1" class="infos">
			<swiper class="info" autoplay vertical circular interval="3000">
				<swiper-item v-for="(item,index) in info" :key='index'>
					<view class="info_item">
						<image src="../../static/logo.png"></image>
						<text>{{item.user}}</text><text>已下单</text>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<swiper :current="currentTab" @change="bindChange" class='swp' :style="{height:aheight?aheight+'px':'auto'}">
			<swiper-item>
				<view class="list-item">
					<view class="list" v-for="(item,index) in list" :key='index'>
						<view class="list-img">
							<image src="/static/zatu/2.jpg" mode="aspectFit"></image>
						</view>
						<view class="list-con">
							<view class="title">{{item.title}}</view>
							<view class="list-foot">
								<view style="width: 150rpx;">
									<view class="price1">¥{{item.user}}</view>
								</view>
								<view style="width: 150rpx; text-align: right;display: inline-block;">
									<view class="price2">¥{{item.like}}</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</swiper-item>
			<swiper-item>
				<view class="item_tv">直播间</view>
			</swiper-item>
			<swiper-item style="margin: 0rpx; 10rpx">
				<view class="item_image">
					<view class="marhin_view">
						<u-alert :title="text1" type="error"></u-alert>
						<u-swiper :list="list1" indicator indicatorMode="dot" circular></u-swiper>
					</view>
					<view class="marhin_view">
						<u-alert :title="text2" type="error"></u-alert>
						<u-swiper indicator :list="list2" keyName="url" indicatorMode="dot" :autoplay="false">
						</u-swiper>
					</view>
					<view class="marhin_view">
						<u-alert :title="text3" type="error"></u-alert>
						<view style="display: flex;align-items: center;justify-content: center;margin-top:5rpx;">
							<u--image :src="list3" mode="widthFix" height="100%" shape="square" radius="10"></u--image>
						</view>
					</view>
				</view>
			</swiper-item>
		</swiper>
	</view>
</template>

核心代码

js

uni.createSelectorQuery() 获取元素节点

query.select('选择元素').boundingClientRect

<script>
	export default {
		data() {
			return {
				list1: [
					'https://cdn.uviewui.com/uview/swiper/swiper1.png',
					'https://cdn.uviewui.com/uview/swiper/swiper2.png',
					'https://cdn.uviewui.com/uview/swiper/swiper3.png',
				],
				list2: [{
					url: 'https://cdn.uviewui.com/uview/resources/video.mp4',
					poster: 'https://cdn.uviewui.com/uview/swiper/swiper1.png'
				}, {
					url: 'https://cdn.uviewui.com/uview/swiper/swiper2.png',
				}, {
					url: 'https://cdn.uviewui.com/uview/swiper/swiper3.png',
				}],
				list3: '/static/zatu/1.jpg',
				text1: '福利一览',
				text2: '攻略教程',
				text3: '转发海报',
				currentTab: 0,
				aheight: '',
				infos: '1',
				info: [{
					user: '大魔王',
				}, {
					user: '小魔女',
				}, {
					user: '名称长长的',
				}],
				scoll: [{
					txt: '精选'
				}, {
					txt: '直播间'
				}, {
					txt: '代言福利'
				}],
				list: [{
						user: '1000000',
						title: 'ssssssssss',
						like: '33333'
					}, {
						user: '2',
						title: 'ssaassssssss',
						like: '3333'
					},
					{
						user: '3',
						title: 'ss2ssssssss',
						like: '33'
					},
					{
						user: '4',
						title: 'ss2ssssssss',
						like: '33'
					},
					{
						user: '5',
						title: 'ss2ssssssss',
						like: '33'
					},
					{
						user: '6',
						title: 'ss2ssssssss',
						like: '33'
					},
					{
						user: '7',
						title: 'ss2ssssssss',
						like: '33'
					},
					{
						user: '8',
						title: 'ss2ssssssss',
						like: '33'
					},
					{
						user: '9',
						title: 'ss2ssssssss',
						like: '33'
					},
					{
						user: '10',
						title: 'ss2ssssssss',
						like: '33'
					}
				]
			}
		},
		onShow() {
            //当前的页面swiper高度
			const query = uni.createSelectorQuery().in(this);
			query.select('.list-item').boundingClientRect(data => {
				this.aheight = data.height
			}).exec();
		},
		methods: {
			// 滑动页面同步tab栏
			bindChange: function(e) {
				this.currentTab = e.detail.current
				// 动态获取滑动页面高度
				const query = uni.createSelectorQuery().in(this);
				if (this.currentTab == 0) {
					query.select('.list-item').boundingClientRect(data => {
						this.aheight = data.height
					}).exec();
				} else if (this.currentTab == 1) {
					query.select('.item_tv').boundingClientRect(data => {
						this.aheight = data.height	
					}).exec();
				} else {
					query.select('.item_image').boundingClientRect(data => {
						this.aheight = data.height	
					}).exec();
				}
			},
			//点击tab切换
			swichNav: function(e) {
				var that = this;
				if (this.currentTab === e.target.dataset.current) {
					return false;
				} else {
					this.currentTab = e.target.dataset.current
				}
			}
		}
	}
</script>

css

<style scoped lang="scss">
	.scroll1 {
		position: fixed;
		top: 0rpx;
		z-index: 99;
		width: 100%;
		white-space: nowrap;
		padding: 10rpx 20rpx;
		background-color: #ffffff;

		& view {
			white-space: normal;
			display: inline-block;
		}

		& view:before {
			content: '|';
			color: #f4f4f4;
			margin: 0 20rpx;
		}

		& view:first-child:before {
			display: none;
		}

		.select {
			color: #fb6583;
			font-size: 40rpx;
		}

		.select1 {
			color: $uni-text-color-placeholder;
		}
	}


	.infos {
		position: fixed;
		top: 90rpx;
		z-index: 99;
		border-top-right-radius: 90rpx;
		border-bottom-right-radius: 90rpx;
		padding: 5rpx;
		background-color: rgba(0, 0, 0, 0.5);

		.info {
			height: 40rpx;
			width: 250rpx;

			swiper-item {

				.info_item {
					display: flex;
					align-items: center;
					justify-content: center;

					image {
						border-radius: 90rpx;
						width: 35rpx;
						height: 35rpx;
					}

					text {
						margin-left: 10rpx;
						color: #ffffff;
						font-size: 20rpx;
					}
				}
			}
		}
	}

	.swp {
		margin-top: 70rpx;

		.list-item {
			overflow: hidden;

			.list {
				display: flex;
				margin: 10rpx;
				padding: 15rpx;
				border-bottom: 1rpx solid #fb6583;

				.list-img {
					background-color: #fef0f0;
					width: 400rpx;
					height: 400rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 12rpx;

					image {
						border-radius: 12rpx;
						width: 380rpx;
						height: 380rpx;
					}
				}

				.list-con {
					margin-left: 20rpx;

					.title {
						font-size: 45rpx;
					}

					.list-foot {
						margin-top: 250rpx;
						display: flex;
						align-items: center;

						.price1 {
							font-size: 25rpx;
						}

						.price2 {
							// position: fixed;

							font-size: 45rpx;
							color: #fb6583;
						}
					}
				}
			}
		}
	}

	.item_image {
		.marhin_view {
			margin: 10rpx;
		}
	}
</style>

精简代码

HTML

<!-- 顶部导航 -->
<scroll-view class="scroll1" scroll-x="true">
    <view :class="currentTab==index ? 'select' : 'select1'" :data-current="index" @click="swichNav" v-for="(item,index) in scoll" :key='index'>{{item.txt}}
    </view>
</scroll-view>

<!-- 轮播 -->
<swiper :current="currentTab" @change="bindChange" class='swp' :style="{height:aheight?aheight+'px':'auto'}">
    <!-- 轮播1 -->
    <swiper-item>
        <view class="list-item">//注意这里是节点
            //....放你的内容
            <image src="../../../../static/gwc.png"></image>
			<view>大魔王</view>
			<view>3652</view>
        </view>
    </swiper-item>

   <!-- 轮播2 -->
    <swiper-item>
        <view class="item_tv">
            //....内容
         
        </view>
    </swiper-item>

   <!-- 轮播3 -->
    <swiper-item>
        <view class="item_image">
            //....内容
           
        </view>
    </swiper-item>
</swiper>

JS

<script>
	export default {
		data() {
			return {
				currentTab: 0,//默认菜单
				aheight: '',
				scoll: [{
					txt: '精选'
				}, {
					txt: '直播间'
				}, {
					txt: '代言福利'
				}],
			}
		},
		onShow() {
			//当前的页面swiper高度,query.select('.list-item'),.list-item就是你的类节点
			const query = uni.createSelectorQuery().in(this);
			query.select('.list-item').boundingClientRect(data => {
				this.aheight = data.height
			}).exec();
		},
		methods: {
			// 滑动页面同步tab栏query.select('.list-item'),类节点
			bindChange: function(e) {
				this.currentTab = e.detail.current
				// 动态获取滑动页面高度
				const query = uni.createSelectorQuery().in(this);
				if (this.currentTab == 0) {
					query.select('.list-item').boundingClientRect(data => {
						this.aheight = data.height
					}).exec();
				} else if (this.currentTab == 1) {
					query.select('.item_tv').boundingClientRect(data => {
						this.aheight = data.height
					}).exec();
				} else {
					query.select('.item_image').boundingClientRect(data => {
						this.aheight = data.height
					}).exec();
				}
			},
			//点击tab切换
			swichNav: function(e) {
				var that = this;
				if (this.currentTab === e.target.dataset.current) {
					return false;
				} else {
					this.currentTab = e.target.dataset.current
				}
			}
		}
	}
</script>

CSS

<style lang="scss">
	.scroll1 {
		position: fixed;
		top: 0rpx;
		z-index: 99;
		width: 100%;
		white-space: nowrap;
		padding: 10rpx 20rpx;
		background-color: #ffffff;

		& view {
			white-space: normal;
			display: inline-block;
		}

		& view:before {
			content: '|';
			color: #f4f4f4;
			margin: 0 20rpx;
		}

		& view:first-child:before {
			display: none;
		}

		.select {
			color: #fb6583;
			font-size: 40rpx;
		}

		.select1 {
			color: $uni-text-color-placeholder;
		}
	}

	.swp {
		margin-top: 70rpx;

		.list-item {
			overflow: hidden;

			.list {
				display: flex;
				margin: 10rpx;
				padding: 15rpx;
				border-bottom: 1rpx solid #fb6583;
			}
		}
	}
</style>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值