scroll-view 实现自动滚动到最底部

文章讲述了在Vue.js应用中,如何使用`scroll-view`组件实现内容区域自动调整到可视区域底部的功能,通过监听滚动事件和计算内容高度与视口高度差来设置`scrollTop`值。

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

<template>
    <view>
        	<scroll-view 
                    class="dialogue-box" 
                    style="height: calc(100vh - var(--window-top) - 114rpx - var(--window-bottom));"
				    @scrolltolower='scrollreach()' 
                    scroll-y="true" 
                    :scroll-top="scrollTop" 
                    :scroll-with-animation="true">
				        <view class="dialogue-box-content">
                        </view>
            </scroll-view>
    </view>
</template>

 

<script>
    export default {
		data() {
			return {
				scrollTop: 0, //scrollTop 值的变量名
			}
		},
		onLoad(e) {
			this.chatScrollTop()
		},
        methods:{
            // 在 scroll-view 内容改变时调用 chatScrollTop 方法
			chatScrollTop() {

				this.$nextTick(() => {
					uni.createSelectorQuery().in(this).select('.dialogue-box-content').boundingClientRect((res) => {
						console.log(res, 'res')

						// let top = res.height - this.scrollViewHeight;
						let top = res.height - 85;//这个85 滚动区域占据页面85vh
						if (top > 0) {
							this.scrollTop = top;
						}
					}).exec()
				})

                //这种写法会出现无法到最底部的情况
                // 	this.$nextTick(() => {
			// 		const query = uni.createSelectorQuery()
			// 		query.select('.dialogue-box').boundingClientRect()
			// 		query.select('.dialogue-box-content').boundingClientRect()
			// 		query.exec(res => {
			// 			const scrollViewHeight = res[0].height
			// 			const scrollContentHeight = res[1].height
			// 			if (scrollContentHeight > scrollViewHeight) {
			// 				const scrollTop = scrollContentHeight - scrollViewHeight
			// 				this.scrollTop = scrollTop
			// 			}
			// 		})
			// 	})
			},
        }
</script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值