uniapp中商品分类数据左右联动的实现Demo

最终效果:

 思路:
         左边分类与右边分类一一对应,点击左边分类右边对应向上滚动,同理,右边滚动动态改变左边currentIndex的值
         需要用到scroll-view中的scroll-into-view属性[值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素)]

<template>
	<view class="container">
		<view class="scrollBox">
			<view class="left">
				<scroll-view 
				scroll-y="true" 
				:style="{height:windowHeight+'px'}"
				:scroll-into-view="rightId"
				:scroll-with-animation="true"
				class="scroll-left">
					<view 
					v-for="(item,index) in list" 
					:key="index" 
					class="left-item"
					:class="[currentIndex===index?'active':'']"
					@click="changeIndex(index)"
					:id="'left'+index"
					>{{item.title}}</view>
				</scroll-view>
			</view>
			
			<view class="right">
				<scroll-view 
				:scroll-y="true"
				@scroll="leftScroll"
				:scroll-with-animation="true"
				:scroll-into-view="leftId" 
				:style="{'height':windowHeight+'px'}">
					<view 
					v-for="(item, index) in list"
					:id="'right'+index"
					:class="[index===list.length-1?'padd-b':'']"
					:style="{paddingBottom:index===list.length-1?windowHeight-rightLastHeight+'px':''}"
					:key="index">
						<view class="title right-title">{{item.title}}</view>
						<view v-for="(item2, index2) in item.list" :key="index2">
							<text class="item">{{item2}}</text>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				currentIndex:0,
				list: [
					{
						title:'房屋漏水',
						list:['你家才漏水','我家不漏水','水','水']
					},
					{
						title:'墙面刷新',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					},
					{
						title:'墙面刷新1',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					},
					{
						title:'墙面刷新2',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					},
					{
						title:'墙面刷新3',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					},
					{
						title:'墙面刷新4',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					},
					{
						title:'墙面刷新5',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					},
					{
						title:'墙面刷新6',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					},
					{
						title:'墙面刷新7',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					},
					{
						title:'墙面刷新8',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					},{
						title:'墙面刷新9',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					},{
						title:'墙面刷新10',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					},{
						title:'墙面刷新11',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					},{
						title:'墙面刷新12',
						list:['次卧墙面刷新','主卧墙面书信','婚房墙面刷新','全屋墙面刷新']
					}
				],
				windowHeight:0 ,//窗口高度
				windowHeight1:0,
				topList:[]    ,//右边title top的值
				rightId:'',    //左边滚动到右边的id
				leftId:''		,//右边滚动到左边左边的id
				rightLastHeight:'' ,//右边最后一个盒子的高度
			}
		},
		methods: {
			changeIndex(index){
				this.currentIndex = index
				this.leftId = "right" + index;
				
			},
			leftScroll(e){
				const scrollTop = e.target.scrollTop;  //获取屏幕滚动的距离
				
				for(var i=0;i<=this.topList.length;i++){
					if(scrollTop>=this.topList[i]&&scrollTop<=this.topList[i+1]){
						this.currentIndex = i
						this.rightId = 'left' +i
						console.log(i)
					}
				}
			},
			// 获取右边title节点属性
			getRightTop(){
				const query = uni.createSelectorQuery().in(this);
				query.selectAll('.title').boundingClientRect(data => {
					// console.log(data)
					data.forEach((item)=>{
						this.topList.push(item.top)
					})
				}).exec();
			},
			getRightBottom(){
				const query = uni.createSelectorQuery().in(this);
				query.select('.padd-b').boundingClientRect(data => {
					this.rightLastHeight = data.height
				}).exec();
			}
		},
		onLoad() {
			var _this = this
			uni.getSystemInfo({
				success:function(res){
					_this.windowHeight = res.windowHeight;
				}
			})
			this.getRightTop()
			this.getRightBottom()
		}
	}
</script>

<style scoped lang="scss">
.container{
	.scrollBox{
		display: flex;
		.left{
			.scroll-left{
				width: 300rpx;
				.left-item{
					height: 100rpx;
					line-height: 100rpx;
					text-align: center;
				}
				.active{
					background-color: yellow;
				}
			}
		}
		.right{
			width: 450rpx;
			.title{
				color: red;
			}
		}
	}
}
</style>

欢迎指正

  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单的示例代码,演示了如何在 uniapp 实现分类左右联动效果: ```html <template> <view class="container"> <view class="left"> <!-- 左侧分类列表 --> <scroll-view scroll-y="true"> <view v-for="(category, index) in categories" :class="{ active: currentIndex === index }" @click="handleCategoryClick(index)"> {{ category }} </view> </scroll-view> </view> <view class="right"> <!-- 右侧商品列表 --> <scroll-view scroll-y="true"> <view v-for="(product, index) in products" :key="product.id"> <image :src="product.image" /> <view>{{ product.name }}</view> <view>¥{{ product.price }}</view> </view> </scroll-view> </view> </view> </template> <script> export default { data() { return { categories: ['水果', '蔬菜', '肉类', '海鲜'], // 左侧分类列表 products: [ // 右侧商品列表 { id: 1, name: '苹果', price: 5, image: 'https://example.com/apple.png', category: '水果', }, { id: 2, name: '香蕉', price: 3, image: 'https://example.com/banana.png', category: '水果', }, { id: 3, name: '西红柿', price: 2, image: 'https://example.com/tomato.png', category: '蔬菜', }, { id: 4, name: '鸡肉', price: 12, image: 'https://example.com/chicken.png', category: '肉类', }, { id: 5, name: '虾', price: 20, image: 'https://example.com/shrimp.png', category: '海鲜', }, ], currentIndex: 0, // 当前选中分类索引 }; }, methods: { handleCategoryClick(index) { // 点击分类列表时更新当前选中分类索引 this.currentIndex = index; }, }, }; </script> <style> .container { display: flex; flex-direction: row; height: 100%; } .left { width: 30%; height: 100%; background-color: #f2f2f2; padding: 10px; } .left .active { color: #007aff; font-weight: bold; } .right { flex: 1; height: 100%; padding: 10px; } .right image { width: 100%; height: 200px; object-fit: cover; } </style> ``` 在这个示例,我们定义了一个 `categories` 数组作为左侧分类列表,以及一个 `products` 数组作为右侧商品列表。同时,我们使用了一个 `currentIndex` 变量来记录当前选中分类索引。当用户点击左侧分类列表的某个分类时,我们会更新 `currentIndex` 变量,然后根据当前选中分类来过滤右侧商品列表商品。最后,我们使用了 `v-for` 指令来循环渲染左侧分类列表和右侧商品列表,并使用了 `class` 绑定来控制左侧选中分类样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值