I need to create a chat for my application, and to show the last messages when going on the chat page, I need to scroll automatically at the bottom of the div.
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;
},
}
}
I tried to use scrollTop by giving the scrollHeight of the div with the scroll.
scrollHeight get the good value (in this case 300), but scrollTop stays at 0.
Hope someone can help.
Thanks in advance !
2
Answers
Your code works for me as is, without the
v-chat-scroll
:Also, the
v-chat-scroll
directive does the same thing, it works for me too:the function
.scrollIntoView(false)
should scroll you to the bottom of a div. I have added a codesnippet, but this is just made out of html,js,css just to show how it works.