uniapp侧边导航栏大概思路

效果

在这里插入图片描述

实际代码

<template>
	<view class="content">
		<scroll-view scroll-y="true" class="nave-left" :scroll-into-view="scrollToLeft">
			<view class="scroll">
				<view
					class="goods-type"
					:class="{ isSelect: scrollToLeft === 'idx' + index }"
					v-for="(val, index) in items"
					:key="index"
					@click="scrollRight(index)"
					:id="'idx' + index"
				>
					{{ val.name }}
				</view>
			</view>
		</scroll-view>
		<scroll-view scroll-y="true" class="nav-right" :scroll-into-view="scrollToRight" scroll-with-animation="" @scroll="scrolling">
			<view class="scroll">
				<view class="goods-list" v-for="(val, index) in items" :key="index" :id="'id' + index">
					<view class="goods-head">{{ val.name }}</view>
					<view class="goods-body" v-for="(item, idx) in val.children" :key="idx">{{ item.name }}</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
export default {
	name: 'goodsSecond',
	data() {
		return {
			scrollToRight: '',
			nodeHeight: [],
			scrollToLeft: 'idx0',

			items: [
				{
					name: 'A',
					children: [
						{
							name: 'A1'
						},
						{
							name: 'A2'
						},
						{
							name: 'A3'
						},
						{
							name: 'A1'
						},
						{
							name: 'A2'
						},
						{
							name: 'A3'
						}
					]
				},
				{
					name: 'B',
					children: [
						{
							name: 'B1'
						},
						{
							name: 'B2'
						},
						{
							name: 'B3'
						},
						{
							name: 'B1'
						},
						{
							name: 'B2'
						},
						{
							name: 'B3'
						}
					]
				},
				{
					name: 'C',
					children: [
						{
							name: 'C1'
						},
						{
							name: 'C2'
						},
						{
							name: 'C3'
						},
						{
							name: 'C1'
						},
						{
							name: 'C2'
						},
						{
							name: 'C3'
						}
					]
				}
			]
		};
	},
	mounted() {
		this.getGoodsLsit();
	},
	methods: {
		getGoodsLsit() {
			let view = uni
				.createSelectorQuery()
				.in(this)
				.selectAll('.goods-list');

			view.boundingClientRect(data => {
				console.log('得到布局位置信息');
				this.nodeHeight = data;
				console.log(data);
			}).exec();
		},
		scrollRight(index) {
			this.scrollToRight = 'id' + index;
			this.scrollToLeft = 'idx' + index;
		},
		scrolling(e) {
			this.scrollLeft(Math.ceil(e.detail.scrollTop));
			console.log(e.detail);
		},
		scrollLeft(e) {
			for (let i = 2; i < this.nodeHeight.length; i++) {
				if (e > this.nodeHeight[i - 1].top && e < this.nodeHeight[i].top) {
					this.scrollToLeft = 'idx' + (i - 1);
				} else if (e > this.nodeHeight[this.nodeHeight.length - 1].top) {
					this.scrollToLeft = 'idx' + (this.nodeHeight.length - 1);
				} else if (e < this.nodeHeight[1].top) {
					this.scrollToLeft = 'idx0';
					this.scrollToRight = 'id0';
				}
				this.scrollToRight = '';
			}
		}
	}
};
</script>

<style>
.content {
	width: 100%;
	height: 100%;
	display: flex;
}

.nave-left {
	width: 187rpx;
	height: 100%;
}
.nav-right {
	width: 562rpx;
	height: 100%;
}
.scroll {
	width: 100%;
	height: 100%;
	display: flex;
	flex-direction: column;
	align-items: center;
}

.goods-type {
	width: 150rpx;
	height: 80rpx;
	box-shadow: 0px 4px 7px 1px rgba(206, 148, 5, 0.24);
	border-radius: 20px;

	display: flex;
	align-items: center;
	justify-content: center;

	font-size: 24rpx;
	font-family: Microsoft YaHei UI;
	font-weight: 400;
	color: #a1a1a1;
}

.isSelect {
	border: 2rpx solid rgb(206, 148, 5);
}

.goods-list {
	width: 500rpx;
}
.goods-head {
	width: 500rpx;
	height: 80rpx;
	display: flex;
	align-items: center;
}
.goods-body {
	width: 500rpx;
	height: 150rpx;

	display: flex;
	align-items: center;

	box-shadow: 0px 4px 7px 1px rgba(206, 148, 5, 0.24);
	border-radius: 20px;
}
</style>

下面是思路

结构

<view class="nav">
	 <scroll-view scroll-y="true" class="nav-left" :scroll-top="scrollLeftTop" scroll-with-animation>//左侧一个scroll,scroll-top决定左侧scroll滑动到顶部的位置,scroll-with-animation加个动画
	 
		<view :id="'id' + val" class="menu"  v-for="val in 15" @click="categoryClick(index)">//点击事件触发右侧scroll-into-view滑动到子元素位置
		//分类标题
		</view>
		
	</scroll-view>
x
	<scroll-view scroll-y="true" class="nav-right" :scroll-into-view="'idx' + scrollRightTop" @scroll="scrollTop" scroll-with-animation>//右侧详细的商品,scroll-into-view决定滚动到相应的子元素的位置上,@scroll滚动时触发
		
		<swiper class="swiper" circular="true" autoplay="true" interval="5000" duration="500" indicator-dots="true" indicator-active-color="#ffffff">//右侧滑动
			
			<swiper-item v-for="(img, imgId) in banImg" :key="imgId">
				//右侧的轮播图
				<image :src="img" mode="aspectFill"></image>
			
			</swiper-item>
		
		</swiper>
	<view :id="'id' + val" class="menuTitle" v-for="val in 15">//商品列表内容遍历
	{{val}}
	</view>
	
	</scroll-view>
	
</view>

样式

.nav{
width:100%;
height:100%;
display:flex;
}
.nav-left{
width:30%;
heght:100%;
display:flex;
flex-direction: column;
align-items: center;
}
.nav-right{
width:70%;
heght:100%;
display:flex;
flex-direction: column;
align-items: center;
}
//商品列表的内容样式自定义

交互

export default {
	data() {
		return {
			banImg: ['轮播图图片地址'],
			scrollLeftTop:'',//左侧滑动到相应子元素的索引
			scrollRightTop: ''//右侧滑动到相应子元素的索引
			arr:[]//右侧滑轮各个子元素距离实际顶部的距离,自行计算,或者通过
			
		};
	},
	methods: {
	categoryClick( index) {
			this.scrollRightTop == index ? (this.scrollRightTop =index  - 1) : (this.scrollRightTop = index );//点击左侧触发传输点击位置的索引,滑动右侧到对应的子元素中

			
		},
		scrollTop(e) {
			for (let i = 0; i < this.arr.length; i++) {//遍历各个元素距离顶部的距离
				if (i > 0 && (e.detail.scrollTop >= this.arr[i - 1] && e.detail.scrollTop < this.arr[i])) {//判断是否处于某两个子元素之间
					this.scrollLeftTop = i;//滑动左侧到对应的子元素中
					return false;
				} else if (i == 0 && e.detail.scrollTop < this.arr[i]) {//这个是判断是否第一个的,也可以在arr中的第一个加一个0
					this.scrollLeftTop = i;
					return false;
				}
			}
		}
	}
}
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现侧边分类导航栏,您需要执行以下步骤: 1.创建一个分类表 您需要创建一个分类表,该表包含分类的名称和对应的ID。您可以使用数据库软件(如MySQL)来创建此表。如下所示: CREATE TABLE categories ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(50) NOT NULL, PRIMARY KEY (id) ); 2.添加分类数据 在分类表中添加分类数据。如下所示: INSERT INTO categories (name) VALUES ('电子产品'); INSERT INTO categories (name) VALUES ('服装'); INSERT INTO categories (name) VALUES ('家居用品'); 3.在Vue中获取分类数据 使用Vue的axios库来获取分类数据。您可以在uniapp的main.js文件中添加以下代码: import axios from 'axios'; Vue.prototype.$http = axios; Vue.prototype.$http.get('/api/categories') .then(response => { this.categories = response.data; }) .catch(error => { console.log(error); }); 4.在侧边栏中显示分类 在侧边栏中显示分类。您可以使用uniapp组件来创建侧边栏,如u-slide-menu和u-list组件。您可以在侧边栏中使用v-for指令来遍历分类数据,然后将每个分类显示为列表项。如下所示: <u-slide-menu :open.sync="open"> <u-list> <u-list-item v-for="category in categories" :key="category.id"> {{ category.name }} </u-list-item> </u-list> </u-slide-menu> 5.显示分类的产品 当用户单击分类时,显示该分类的产品。您可以在分类表中添加产品数据,并将产品与分类关联。然后,在单击分类时,使用axios来获取该分类的产品数据,并显示该数据。如下所示: methods: { onCategoryClick(category) { this.$http.get(`/api/categories/${category.id}/products`) .then(response => { this.products = response.data; }) .catch(error => { console.log(error); }); } } 以上是实现侧边分类导航栏的基本步骤。您可以将其扩展以满足您的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值