Skip to content

Commit c9772bf

Browse files
dam1r89Akryum
andauthored
fix: scrollToItem works with pageMode (#396)
Co-authored-by: Guillaume Chau <[email protected]>
1 parent c85f07b commit c9772bf

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

docs-src/src/components/RecycleScrollerDemo.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
max="500000"
4343
> buffer
4444
</span>
45+
<span>
46+
<button @mousedown="$refs.scroller.scrollToItem(scrollTo)">Scroll To: </button>
47+
<input
48+
v-model.number="scrollTo"
49+
type="number"
50+
min="0"
51+
:max="list.length - 1"
52+
>
53+
</span>
4554
<span>
4655
<button @mousedown="renderScroller = !renderScroller">Toggle render</button>
4756
<button @mousedown="showScroller = !showScroller">Toggle visibility</button>
@@ -122,6 +131,7 @@ export default {
122131
enableLetters: true,
123132
pageMode: false,
124133
pageModeFullPage: true,
134+
scrollTo: 100,
125135
updateParts: { viewStartIdx: 0, viewEndIdx: 0, visibleStartIdx: 0, visibleEndIdx: 0 },
126136
showMessageBeforeItems: true,
127137
}),

src/components/RecycleScroller.vue

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,11 +625,33 @@ export default {
625625
},
626626
627627
scrollToPosition (position) {
628-
if (this.direction === 'vertical') {
629-
this.$el.scrollTop = position
628+
const direction = this.direction === 'vertical'
629+
? { scroll: 'scrollTop', start: 'top' }
630+
: { scroll: 'scrollLeft', start: 'left' }
631+
632+
let viewport
633+
let scrollDirection
634+
let scrollDistance
635+
636+
if (this.pageMode) {
637+
const viewportEl = ScrollParent(this.$el)
638+
// HTML doesn't overflow like other elements
639+
const scrollTop = viewportEl.tagName === 'HTML' ? 0 : viewportEl[direction.scroll]
640+
const bounds = viewportEl.getBoundingClientRect()
641+
642+
const scroller = this.$el.getBoundingClientRect()
643+
const scrollerPosition = scroller[direction.start] - bounds[direction.start]
644+
645+
viewport = viewportEl
646+
scrollDirection = direction.scroll
647+
scrollDistance = position + scrollTop + scrollerPosition
630648
} else {
631-
this.$el.scrollLeft = position
649+
viewport = this.$el
650+
scrollDirection = direction.scroll
651+
scrollDistance = position
632652
}
653+
654+
viewport[scrollDirection] = scrollDistance
633655
},
634656
635657
itemsLimitError () {

0 commit comments

Comments
 (0)