- 准备一个高度确定的容器, 通过
ref
给元素注册引用信息,唯一id
<div class= "height" ref= "chatRoom"></div>
<style>
.height {
height: 800px;
}
</style>
methods
里声明一个函数,在页面初始化和更新时调用
mounted() {
//页面挂载完成时调用
this.scrollToBottom()
},
updated() {
//页面更新后调用
this.scrollToBottom()
},
methods: {
scrollToBottom() {
// 聊天定位到底部
let chatRoom = this.$refs.chatRoom //通过$refs获取到该元素
chatRoom.scrollTop = chatRoom.scrollHeight
console.log('聊天定位到底部!')
}
}