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

<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>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现 `scroll-view` 的自动滚动,可以使用 `setInterval` 方法和 `scroll-view` 的 `scroll-top` 属性。具体实现方法如下: 1. 在 `scroll-view` 标签中添加 `scroll-top="{{scrollTop}}"` 属性,用于绑定滚动条的位置。 2. 在 `data` 中定义 `scrollTop` 和 `intervalId` 变量,分别用于存储滚动条的位置和定时器的 ID。 3. 在 `onLoad` 生命周期函数中,调用 `setInterval` 方法,设置定时器每隔一段时间执行一次滚动操作。 4. 在定时器中,先判断当前滚动条的位置是否已经到达底部,如果到达底部则将滚动条位置设置为 0,否则将滚动条位置加上一个适当的值,使其向下滚动。 下面是实现代码示例: ```html <scroll-view scroll-y="true" scroll-top="{{scrollTop}}"> <!-- 内容区域 --> </scroll-view> ``` ```js Page({ data: { scrollTop: 0, // 滚动条位置 intervalId: null // 定时器 ID }, onLoad: function() { // 开启定时器,每隔 3 秒执行一次滚动操作 this.data.intervalId = setInterval(() => { // 判断当前是否到达底部 wx.createSelectorQuery().select('.scroll-view').boundingClientRect((rect) => { if (rect.bottom <= wx.getSystemInfoSync().screenHeight) { this.setData({ scrollTop: 0 }); } else { // 每次滚动 20px this.setData({ scrollTop: this.data.scrollTop + 20 }); } }).exec(); }, 3000); }, onUnload: function() { // 清除定时器 clearInterval(this.data.intervalId); } }); ``` 在上面的代码中,通过 `wx.createSelectorQuery().select('.scroll-view').boundingClientRect((rect) => {...})` 获取 `scroll-view` 的位置信息,判断是否到达底部。如果到达底部,则将滚动条位置设置为 0;否则将滚动条位置加上一个适当的值,使其向下滚动。在页面销毁时,清除定时器,避免内存泄漏。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值