我需要为我的应用程序创建聊天,并在进入聊天页面时显示最后的消息,我需要在div的底部自动滚动.
VueJs:VueJs:
<template>
<div id="app">
<div class="comments" v-chat-scroll>
<div v-for="comment in comments">
//What defines the comment
</div>
</div>
</div>
</template>
Css:以下内容:
.comments {
margin: 2.5rem auto 0;
max-width: 60.75rem;
padding: 0 1.25rem;
height: 300px;
overflow-y: scroll;
}
Typescript :
export default {
mounted() {
this.scrollToEnd();
},
methods: {
scrollToEnd() {
var container = this.$el.querySelector(".comments");
var scrollHeight = container.scrollHeight;
container.scrollTop = scrollHeight;
},
}
}
我试图通过给scroll赋予div的scllHeight来使用scllTop. ScllHeight获得较好的值(在本例中为300),但scllTop保持为0.
希望有人能帮上忙. 提前谢谢!