File tree Expand file tree Collapse file tree 2 files changed +30
-9
lines changed Expand file tree Collapse file tree 2 files changed +30
-9
lines changed Original file line number Diff line number Diff line change 10
10
<button @click =" addItems(10)" >
11
11
Add 10 items
12
12
</button >
13
+ <button @click =" addItems(50)" >
14
+ Add 50 items
15
+ </button >
13
16
</div >
14
17
15
18
<DynamicScroller
25
28
:active =" active"
26
29
:data-index =" index"
27
30
>
28
- <div
29
- class =" message"
30
- :style =" {
31
- height: `${item.size}px`,
32
- }"
33
- >
31
+ <div class =" message" >
34
32
{{ item.text }}
35
33
</div >
36
34
</DynamicScrollerItem >
@@ -54,9 +52,8 @@ export default {
54
52
addItems (count = 1 ) {
55
53
for (let i = 0 ; i < count; i++ ) {
56
54
this .items .push ({
57
- text: faker .lorem .sentence (),
55
+ text: faker .lorem .lines (),
58
56
id: this .items .length + 1 ,
59
- size: Math .random () * 120 + 40 ,
60
57
})
61
58
}
62
59
this .scrollToBottom ()
Original file line number Diff line number Diff line change @@ -140,9 +140,33 @@ export default {
140
140
direction (value ) {
141
141
this .forceUpdate (true )
142
142
},
143
+
144
+ itemsWithSize (next , prev ) {
145
+ const scrollTop = this .$el .scrollTop
146
+
147
+ // Calculate total diff between prev and next sizes
148
+ // over current scroll top. Then add it to scrollTop to
149
+ // avoid jumping the contents that the user is seeing.
150
+ let prevActiveTop = 0 ; let activeTop = 0
151
+ const length = Math .min (next .length , prev .length )
152
+ for (let i = 0 ; i < length; i++ ) {
153
+ if (prevActiveTop >= scrollTop) {
154
+ break
155
+ }
156
+ prevActiveTop += prev[i].size || this .minItemSize
157
+ activeTop += next[i].size || this .minItemSize
158
+ }
159
+ const offset = activeTop - prevActiveTop
160
+
161
+ if (offset === 0 ) {
162
+ return
163
+ }
164
+
165
+ this .$el .scrollTop += offset
166
+ },
143
167
},
144
168
145
- created () {
169
+ beforeCreate () {
146
170
this .$_updates = []
147
171
this .$_undefinedSizes = 0
148
172
this .$_undefinedMap = {}
You can’t perform that action at this time.
0 commit comments