uniapp 两个scroll-view相互作用

如何在uniapp,scroll-view标题和scroll-view内容相互联动效果?
talk is less, show the code:

<template>
	<view class="img-show-wrap fz">
		<scroll-view class="tabs-con" scroll-x="true" scroll-with-animation :show-scrollbar=false>
			<view class="tabs-con">
				<view class="tab-item" v-for="(item,i) in tabs" :key="i" @tap="onTabClick(i)" :class="{active:i==activeIndex}" :id="item.sid">
					<text>{{item.label}}</text>
				</view>
			</view>
		</scroll-view>
		<scroll-view class="main-con" @touchstart="mainTouch" id="main-con" scroll-y="true" scroll-with-animation :show-scrollbar=false @scroll="mainScroll" :scroll-into-view="scrollingId">
			<wrap>
				<view class="img-item" v-for="(item,i) in tabs" :key="i" :class="{active:i==activeIndex}" :id="item.id">
					<text class="tit">{{item.label}}</text>
					<view class="item-box">
						<image src="../../static/list/adv.png" />
						<image src="../../static/list/adv.png" />
						<image src="../../static/list/adv.png" />
						<image src="../../static/list/adv.png" />
						<image src="../../static/list/adv.png" />
						<image src="../../static/list/adv.png" />
					</view>
				</view>
			</wrap>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabs: [
					{ label: '云视频(2)', id: 'tab_0', sid: 'tab_11' },
					{ label: '楼盘概况(5)', id: 'tab_1', sid: 'tab_12' },
					{ label: '跑盘实拍(10)', id: 'tab_2' , sid: 'tab_13'},
					{ label: '地理交通(10)', id: 'tab_3' , sid: 'tab_14'},
				],
				activeIndex: 0,
				scrollingId: 'tab_0',
				isMainScroll: true
			};
		},
		methods: {
			mainTouch(){
				this.isMainScroll = true
			},
			mainScroll(e) {
				if (!this.isMainScroll) {
					return;
				}
				let top = e.detail.scrollTop;
				let index = -1;

				if (top >= this.topArr[this.topArr.length - 1]) {
					index = this.topArr.length - 1;
				} else {
					index = this.topArr.findIndex((item, index) => {
						return this.topArr[index + 1] >= top;
					});
				}
				this.activeIndex = (index < 0 ? 0 : index);
			},
			onTabClick(i) {
				this.activeIndex = i
				this.scrollingId = `tab_${i}`
				this.isMainScroll = false
			},
			//获取元素距离顶部的高度
			getScrollTop(selector) {
				return new Promise((resolve, reject) => {
					let query = uni.createSelectorQuery().in(this);
					query.select(selector + '').boundingClientRect(data => {
						resolve(data.top)
					}).exec();
				})
			},
			/* 获取元素顶部信息 */
			async getElementTop() {
				let p_arr = [];
			
				for (let i = 0; i < this.tabs.length; i++) {
					const resu = await this.getScrollTop(`#tab_${i}`)
					p_arr.push(resu)
				}

				/* 减掉主区域滚动容器的顶部距离 */
				this.getScrollTop("#main-con").then(res => {
					/* 所有节点信息返回后调用该方法 */
					this.topArr = p_arr.map(item => item-=res);
				})
			},
		},
		onLoad(e) {
			uni.setNavigationBarTitle({
			  title:'乐米选房-悦府'
			})
		},
		mounted() {
			this.getElementTop()
		}
	}
</script>

<style lang="scss">
.tabs-con{
	text-align: center;  line-height: 70rpx; position:fixed; top:0; width: 100%; font-size: 30rpx; white-space: nowrap; height:70rpx; 
	.tab-item{
		display:inline-block; margin: 0 10rpx;
		text{
			padding: 0 10rpx; display: inline-block;  position:relative;
			&:after{
				position: absolute; height: 1px; background: $theme; content: ""; transition: all .5s ease;
				bottom:0; left:0; width: 0; left: 50%; transform: translateX(-50%);
			}
		}
		&.active {
			font-weight: bold; color: $theme;
			text:after{
				width: 100%; 
			}
		}
	}
}

.img-item{
	height: 700rpx;  line-height: 80rpx;
	.item-box{
		margin: 10rpx -5rpx 0; 
	}
	image{
		width:25%; padding: 0 5rpx; float:left; height: 150rpx; box-sizing: border-box; margin-bottom: 10rpx;
	}
}
.main-con{
	height: calc(100vh - 70rpx);margin-top: 70rpx;
}

</style>

可以直接复制到项目中看效果

效果图:
在这里插入图片描述

<think>嗯,用户的问题是关于如何在UniApp中判断scroll-view组件的滚动高度。首先,我需要回顾一下UniAppscroll-view组件的相关文档和常见问题。根据之前的引用,用户提到了scroll-view的高度设置、滚动条显示问题、横向滚动布局等,但这次的重点是滚动高度的判断。 首先,scroll-view组件在UniApp中确实需要设置固定高度才能滚动,用户可能已经设置了高度,但需要实时获取滚动的位置。常见的做法是使用scroll事件,当用户滚动时触发,并获取事件对象中的滚动位置数据。 然后,参考引用中的例子,比如引用2中的代码,用户通过设置scroll-top属性来控制滚动位置,这说明scroll-view确实有相关的属性和事件来处理滚动。因此,处理滚动高度判断应该依赖于scroll事件,并通过事件对象获取scrollTop值。 接下来,用户可能需要将获取到的滚动高度用于触底加载更多内容,类似分页加载。这时候需要结合scroll-viewscroll-into-view属性或者判断scrollTop是否接近内容的总高度减去容器高度。同时,要注意防止多次触发触底事件,可能需要设置一个防抖机制。 另外,用户可能遇到的常见问题包括scroll事件不触发,或者获取的高度不正确。这时候需要检查是否正确绑定了scroll事件,是否设置了scroll-y或scroll-x,以及容器的高度是否正确。根据引用4,如果布局有问题,比如使用了flex但没正确配置,可能会导致滚动失效,进而影响高度判断。 还需要考虑不同平台的兼容性,比如在iOS和Android上,滚动事件的触发频率可能不同,或者某些CSS样式影响滚动条显示(如引用3提到的滚动条样式问题),虽然这可能不影响高度判断,但用户可能需要确保滚动功能正常。 总结步骤应该是:绑定scroll事件,获取scrollTop值,计算是否触底,然后执行相应操作。同时提供示例代码,说明如何获取当前滚动高度,如何判断触底,并处理可能的性能问题或多次触发的情况。</think>在UniApp中判断`scroll-view`滚动高度的实现方法可分为三个步骤: ### 一、绑定滚动事件 通过`@scroll`事件监听滚动行为,事件对象包含滚动位置数据: ```html <scroll-view scroll-y :style="'height:'+scrollHeight+'px'" @scroll="handleScroll"> <!-- 内容区域 --> </scroll-view> ``` ### 二、获取滚动高度 在事件回调中获取`detail.scrollTop`值,即当前滚动位置: ```javascript data() { return { scrollHeight: 500, // 容器高度 currentScrollTop: 0 } }, methods: { handleScroll(e) { this.currentScrollTop = e.detail.scrollTop console.log('当前滚动高度', this.currentScrollTop) } } ``` ### 三、触底判断逻辑 通过计算`scrollTop + 容器高度 >= 内容总高度`实现触底检测: ```javascript const containerHeight = this.scrollHeight // 容器可见高度 const contentHeight = 2000 // 通过dom查询获取实际内容高度 if (this.currentScrollTop + containerHeight >= contentHeight) { console.log('触底加载') } ``` ### 关键注意事项 1. **容器高度要求**:必须显式设置固定高度值(推荐使用`px`单位),否则无法触发滚动[^1] 2. **性能优化**:高频触发的scroll事件建议配合防抖函数 3. **跨平台差异**:iOS与Android的滚动惯性表现不同,可通过`scroll-with-animation`属性控制 4. **内容高度获取**:需要通过`uni.createSelectorQuery()`动态获取实际内容高度
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值