scroll-view 滑动到底部

在做聊天页面的时候,发送消息,消息会自动上移
直接上代码

view

<view class="char bg" :style="{height: windowHeight+'px'}">
	<scroll-view class="scroll" id="scrollview" scroll-y="true" :scroll-top="scrollTop">
		<view class="cu-item">
		
		</view>
	<scroll-view>
</view>

js

data() {
	return{
		scrollTop: 0, //滑动距离
		mitemHeight: 0, //所有消息的高
		windowHeight: 0, //显示的高度
	}
}
methods:{
	// 滚动
	scrollToBottom () {  //哪里需要用到就在哪里使用
		let that = this;
		let query = uni.createSelectorQuery();
		query.selectAll('.cu-item').boundingClientRect();
		query.select('#scrollview').boundingClientRect();
		query.exec((res) => {
			that.mitemHeight = 0;
			//获取所有内部子元素的高度
			// 因为vue的虚拟DOM 每次生成的新消息都是之前的,所以采用异步setTimeout
			res[0].forEach((rect) => that.mitemHeight = that.mitemHeight + rect.height + 40)   
			setTimeout(() => {
			 	//判断子元素高度是否大于显示高度
				if (that.mitemHeight > (that.windowHeight - 100)) {  
				//用子元素的高度减去显示的高度就获益获得序言滚动的高度
					that.scrollTop = that.mitemHeight - that.windowHeight    
				}
	    }, 100)
   })
	},
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值