【前端】【解决问题】uniapp的scroll-view组件的scroll-top,设置回到底部失效,解决方法

官方解决
我一开始看官方方法以为不行,到处找,结果搞了两三个小时,然后一试官方方法就搞定了。还是官方人考虑的全。
在这里插入图片描述
我的代码:
html:

<scroll-view id="scrollView" 
scroll-y 
:scroll-top="scrollTop" 
:scroll-with-animation="true" 
:style="{height:style.contentH+'px'}"
@scroll="scroll">
	<block v-for="(item,index) in list" :key="index">
		<user-chat-list :item="item" :index="index"></user-chat-list>
	</block>
</scroll-view>

js:

export default {
		components: {
			userChatBottom,
			userChatList
		},
		data() {
			return {
				scrollTop: 0, // 滚动条的位置
				old: {
					scrollTop: 0
				},
				style: {
					contentH: 0,
					itemH: 0
				},
				list: [{
						isme: false,
						userpic: "../../static/demo/userpic/11.jpg",
						type: "text",
						data: "哈哈哈",
						time: "1595904223",
						gstime: ""
					},
					{
						isme: true,
						userpic: "../../static/demo/userpic/12.jpg",
						type: "text",
						data: "哈哈哈",
						time: "1596163423",
						gstime: ""
					},
					{
						isme: false,
						userpic: "../../static/demo/userpic/11.jpg",
						type: "text",
						data: "哈哈哈",
						time: "1596163423",
						gstime: ""
					},
					{
						isme: true,
						userpic: "../../static/demo/userpic/12.jpg",
						type: "img",
						data: "../../static/demo/3.jpg",
						time: "1596163423",
						gstime: ""
					},
				]
			}
		},
		onLoad() {
			this.getData();
			this.initData();
		},
		onReady() {
			this.pageToBottom()
		},
		methods: {
			scroll(e){
				console.log(e)
				this.old.scrollTop = e.detail.scrollTop
			},
			//初始化参数,聊天页面中间空白处大小
			initData() {
				try {
					this.style.contentH = uni.getSystemInfoSync().windowHeight - uni.upx2px(120)
				} catch (e) {}
			},
			// 回到底部
			pageToBottom() {
				this.style.itemH = 0
				let q = uni.createSelectorQuery()
				// let botomInputHeight = uni.upx2px(120)
				q.select('#scrollView').boundingClientRect();
				q.selectAll(".user-chat-item").boundingClientRect()
				q.exec((res) => {20
					// console.log(res[0][res[0].length-1].bottom)
					// this.style.itemH = res[0][res[0].length-1].bottom
					res[1].forEach((ret) => {
						this.style.itemH += ret.height
					})
					console.log(res[1])
					console.log(this.style.itemH ,this.style.contentH)
						this.scrollTop = this.old.scrollTop
						this.$nextTick(function() {
							this.scrollTop = this.style.itemH - this.style.contentH + uni.upx2px(130)
						});
				})
			},
			//得到聊天数据
			getData() {
				for (let i = 0; i < this.list.length; i++) {
					this.list[i].gstime = time.gettime.getChatTime(this.list[i].time, i > 0 ? this.list[i - 1].time : 0)
				}
			},
			submit(data) {
				if (this.isEmpty(data)) {
					return
				}
				console.log("当前输入的是", data)
				// 构建数据
				let now = new Date().getTime()
				let obj = {
					isme: true,
					userpic: "../../static/demo/userpic/12.jpg",
					type: "text",
					data: data,
					time: now,
					gstime: time.gettime.getChatTime(now, this.list[this.list.length - 1].time)
				}
				this.list.push(obj)
				this.pageToBottom()
			}
		}
	}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值