vue中如何实现窗口可视区域上下滚动,并切换商品展示

该文介绍了在Vue和Uniapp框架中使用scroll-view标签实现区域滚动的方法,包括设置scroll-y和scroll-x属性来控制滚动方向,以及通过动态获取窗口高度实现内容区域的适配。同时,文章展示了如何处理点击事件,通过切换数据绑定实现左侧菜单选中高亮,并同步更新右侧显示内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前提

在vue+uniapp开发中使用scroll-view标签,来实现区域滚动

纵向或横向滚动

1.设置scroll-view标签中的属性scroll-y设定为true开启纵向滚动,scroll-x设定为true开启横向滚动;
2.给scroll-view设置一个高度或者宽度,当内容高度或者宽度大于scroll-view高度时即可开启滚动功能
(内容高度小于scroll-view高度时无法体现滚动功能);
3.使用组件uni.getSystemInfoSync()动态获取窗口可用高度,让页面中的左右两块滚动区域的高度撑满屏幕
4.从系统信息中获取窗口可用高度并赋值给scroll-view设置的高度,并减去上下导航栏的高度

案例展示:

在这里插入图片描述

如何实现点击左侧切换右侧界面,实现逻辑

1.左侧点击高亮效果,在data中定义一个属性selectedIndex,当做左侧遍历数组的下标,默认值为0,也就是默认数组第一个;
2.给左侧元素注册点击事件,将点击是的下标赋值给selectedIndex;
3.通过v-bind:class类名样式增强,来添加点击的样式,当selectedIndex=== index时,显示点击后的样式;
4.右侧数组显示列表的下标等于左侧点击时数组的下标就可以实现啦!

具体看以下代码:

<template>
	<view>
			<my-search holderwords="搜索你想要的商品" bgcolor="#f4f4f4"></my-search>
			<view class="scroll-content">
				<scroll-view class="left" scroll-y :style="{ height: wh + 'px' }">
					<view 
					class="left-item" 
					v-for="(item,index) in list"
					:key="index"
					:class="{active:selectedIndex === index}"
					@click="selectedCate(index)"
					>{{item}}</view>
				</scroll-view>
				<scroll-view class="right" scroll-y :style="{ height: wh + 'px' }">
					<view class="right-item">{{list2[selectedIndex]}}</view>
				</scroll-view>
			</view>
	</view>
</template>

<script>
	import mySearch from '@/components/my-search.vue'
	export default{
		components:{
			mySearch
		},
		data(){
			return{
				selectedIndex:0,
				list:[1,2,3,4,5,6,7,8,9,10],
				list2:['列表1','列表2','列表3','列表4','列表5','列表6','列表7','列表8','列表9','列表10'],
			}
		},
		created(){
			// 动态获取窗口可用高度,以便让页面中的左右两块滚动区域的高度撑满屏幕
			// 1. 获取当前系统信息
			const sysInfo = uni.getSystemInfoSync();
			// 2. 从系统信息中获取窗口可用高度并赋值给 wh
			// 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar高度
			this.wh = sysInfo.windowHeight-72;
		},
		methods:{
			selectedCate(index){
				this.selectedIndex = index
			},
		}
		
	}
</script>
<style scoped lang="scss">
	.scroll-content{
		display: flex;
		color:#444;
		font-size: 26rpx;
		.left{
			width: 240rpx;
			.left-item{
				line-height: 120rpx;
				background-color: #f4f4f4;
				text-align: center;
				&.active{
					background-color: #fff;
					font-size:28rpx;
					color:red;
					position: relative;
					&::before{
						content:"";
						display: block;
						width: 60rpx;
						height:4rpx;
						background-color: red;
						position: absolute;
						bottom:0;
						left:50%;
						transform: translate(-50%);
					}
				}
			}
			}
		.right{
			text-align: center;
		}	
	}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值