uniapp实现左侧菜单右侧商品锚点组件

scroll-view:用于区域滚动

原理:利用scroll-view绑定scroll-into-view String 值应为某子元素id(id不能以数字开头)。设置哪个方向可滚动,则在哪个方向滚动到该元素
注意点: 滚动增加防抖
//1: 防抖
// timer定义在全局
// 如果没有防抖会触发许多次
// 对性能不友好

<template>

	<view class="page">
	  <view class="left_right_column_box">
	  			<view class="left_column">
	  				<scroll-view :style="{height: viewHeight}" scroll-y="true" :scroll-top="scrollTop">
	  					<view v-for="item in titleContenData" :key="item.id"
	  						:class="{'left_column_for': true, activity: selectIndex==item.index?true:false}"
	  						@click="clickTitle(item)">
	  						{{item.title}}
	  					</view>
	  				</scroll-view>
	  			</view>
	  
	  			<view class="right_column">
	  				<scroll-view :style="{height: viewHeight}" scroll-y="true" :scroll-into-view="selectId"
	  					scroll-with-animation="true" @scroll="scroll">
	  					<view :id="item.id" class="floorType right_title_content_for" v-for="item in titleContenData"
	  						:key="item.id">
	  						<view class="right_title">{{item.title}}</view>
	  						<view v-for="items in item.contents" :key="items.id">{{items.content}}</view>
	  					</view>
	  				</scroll-view>
	  			</view>
	  	</view>
</view>

</template>

<script>
  let timer = undefined;
	import {
		getHomelist
	} from '../../api/home.js'

	export default {
		data() {
			return {
				// 屏幕高度
							viewHeight: null,
							// 源数据
							titleContenData: [],
							// 设置锚点
							selectId: 'id1',
							// 设置高亮
							selectIndex: 0,
							// 设置左栏顶部距离
							scrollTop: 0,
			};
		},
	
    mounted() {
    		let that = this;
    		uni.getSystemInfo({
    			success: function({
    				windowHeight
    			}) {
    				that.viewHeight = windowHeight + 'px';
    			}
    		});
    
    		// 创建数据
    		that.createData();
    
    		// 此处使用$nextTick是非常有必要
    		// 官方建议
    		// 实际测试如果不用会报错
    		that.$nextTick(function() {
    			const query = uni.createSelectorQuery().in(that);
    
    			query.selectAll('.floorType').boundingClientRect(VNodeAll => {
    				VNodeAll.forEach(({
    					top
    				}, i) => {
    					// 获取并存储每个视图到顶部的距离
    					this.titleContenData[i].viewTop = top;
    				});
    			}).exec();
    
    			console.log('titleContenData:', this.titleContenData);
    		});
    	},

		methods: {
			// 滚动时触发
					scroll({
						detail: {
							scrollTop
						}
					}) {
						let that = this,
							titleContenData = that.titleContenData;
			
						// 防抖
						// timer定义在全局
						// 如果没有防抖会触发许多次
						// 对性能不友好
						if (timer !== undefined) clearTimeout(timer);
			
						timer = setTimeout(function() {
							// 当右侧滚动到顶部时强制赋值为0
							// 因为在滚动时一般获取到的数据是0-10的范围
							// 小概率会获取到0
							// 因为原先存储viewTop属性的第一个值就是0
							scrollTop = scrollTop < 10 ? 0 : scrollTop;
							let selectIndex = titleContenData.findIndex((item) => item.viewTop >= scrollTop);
							console.log('scrollTop:', scrollTop);
							// 设置高亮
							that.selectIndex = selectIndex;
							// 此属性联动左侧滚动条
							// 当右侧滚动时
							// 左侧也会相应的滚动
							// 只是滚动的距离不一样
							that.scrollTop = 5 * that.selectIndex;
						}, 70);
					},
			
					// 标题点击事件
					clickTitle({
						id,
						index
					}) {
						console.log(id, index);
						// 设置锚点
						this.selectId = id;
						// 设置高亮
						this.selectIndex = index;
					},
			
					// 生成内容
					createContent(n) {
						let content = [];
						for (let i = 0; i < n; i++) content.push({
							id: i + 1,
							content: `内容${i+1}内容`
						});
						return content;
					},
			
					// 创建数据
					createData() {
						// 生成标题
						for (let i = 0; i < 24; i++) this.titleContenData.push({
							// 因为需要绑定id作为锚点目标
							// 所以不能以数字开头
							id: `id${i+1}`,
							index: i,
							title: '标题' + (i + 1),
							contents: this.createContent(parseInt(Math.random() * 24 + 1, 10))
						})
					}
				
		}
	}
</script>

<style scoped lang="scss">
	uni-page-body {
		height: 100%;
	}

	.page {
		width: 100%;
		height: 100vh;
		overflow: hidden;
		display: flex;
		flex-direction: column;
	}

	/* 公共样式 */
	.left_right_column_box {
		width: 100%;
		display: flex;
		justify-content: space-evenly;
	}
	
	/* 左侧样式 */
	.left_column {
		flex: 1;
	}
	
	.left_column_for {
		text-align: center;
		padding: 10rpx 0;
	}
	
	.activity {
		color: #000fff;
	}
	
	/* 右侧样式 */
	.right_column {
		flex: 3;
		margin-left: 36rpx;
	}
	
	.right_title_content_for {
		margin-top: 36rpx;
	}
	
	.right_title_content_for:first-child {
		margin-top: 0;
	}
	
	.right_title {
		font-weight: 700;
	}
	
	/* 隐藏scroll-wiew元素的滚动条 */
	scroll-view ::-webkit-scrollbar {
		width: 0;
		height: 0;
		background-color: transparent;
	}
</style>

UniApp 是一个基于 Vue.js 的跨平台应用开发框架,可以同时构建 iOS、Android 和 Web 应用。关于锚点导航,UniApp 提供了一个内置的组件 `uni-page-scroll`,用于实现页面内的锚点导航。 使用 `uni-page-scroll` 组件,你需要在页面中设置多个锚点,并在导航菜单中设置对应的锚点链接。具体的步骤如下: 1. 在页面中设置锚点:在需要跳转的位置标签上添加 `id` 属性,作为锚点的标识。例如: ```html <!-- 页面内容 --> <div id="section1">Section 1</div> <div id="section2">Section 2</div> <div id="section3">Section 3</div> ``` 2. 在导航菜单中设置锚点链接:使用 `<a>` 标签,并设置 `href` 属性为对应的锚点标识。例如: ```html <!-- 导航菜单 --> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> <li><a href="#section3">Section 3</a></li> </ul> ``` 3. 在页面中使用 `uni-page-scroll` 组件:将 `uni-page-scroll` 组件放置在需要实现锚点导航的位置,并设置 `scroll-into-view` 属性为当前显示的锚点标识。例如: ```html <!-- 页面 --> <template> <view> <uni-page-scroll scroll-into-view="{{currentView}}"> <!-- 页面内容 --> <div id="section1">Section 1</div> <div id="section2">Section 2</div> <div id="section3">Section 3</div> </uni-page-scroll> </view> </template> <script> export default { data() { return { currentView: 'section1' // 默认显示第一个锚点 } } } </script> ``` 通过以上步骤,你就可以实现 UniApp 中的锚点导航了。当点击导航菜单中的链接时,页面会滚动到对应的锚点位置。希望对你有帮助!如果有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值