I have a chat application which loads the recent chat messages and store them in messages
array. Previous chat history is loaded on scroll and will be added to the beginning of messages
array. I need to preserve the scroll position when history is loaded
//chat.vue
<div ref="chatHistory" @scroll="fetchHistory">
<div v-for="item in messages" :key="item.id">
<span>{{item.body}}</span>
</div>
</div>
In fetchHistory
method, I add the old messages to beginning of messages
array
fetchHistory(){
if(this.$refs.messageHistory.scrollTop == 0){
var vm = this
this.currentPage.prevPage().then(paginator => {
vm.messages.unshift(...paginator.messages)
});
}
}