I
I
Igor Bezlepkin2018-11-14 16:11:59
Vue.js
Igor Bezlepkin, 2018-11-14 16:11:59

How to stay in the same place while scrolling while adding elements in Vue?

Friends! Tell! I'm making a type of messenger, an element on which the messages themselves scroll. So, when adding elements through vuex, the position seems to go astray. that is, you are on a specific message, as soon as the elements are added to the beginning of the array, the scrolling flies to another position.
maybe there is a hack :)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2018-11-14
@Bezlepkin

Keep the scrollHeight value. And do a watch on an array of elements - in the handler, calculate the difference between the old and new scrollHeight values, adjust the scrollTop accordingly. Somehow so .

E
Edouard Faber, 2018-11-14
@eduha

Data from vuex is passed through computed, right? Add watch to this variable, there already calculate the height of the added element ($nextTick I think), and change the scrollTop

I
Igor Bezlepkin, 2018-11-16
@Bezlepkin

In general, after playing around, I came to the conclusion that when you add elements to the DOM in the old fashioned way, ParentNode.prepend(),
then everything is fine. But now the problem is this. I want to take a template as in mustache.js, push variables (in a loop) and prepend to DOM.
Is there any way to do this in Vue?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question