-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed virtual scroll package, implement custom virtual scroll Added offset prop Fix issue with scroll not being stopped and stop rerendering of already seen items Refresh items after they have been changed
- Loading branch information
Showing
3 changed files
with
216 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<template> | ||
<div class="vscroll-holder"> | ||
<div | ||
class="vscroll-spacer" | ||
:style="{ | ||
opacity: 0, | ||
clear: 'both', | ||
height: topHeight + 'px' | ||
}" | ||
/> | ||
<slot :items="visibleItems" /> | ||
<div | ||
class="vscroll-spacer" | ||
:style="{ | ||
opacity: 0, | ||
clear: 'both', | ||
height: bottomHeight + 'px' | ||
}" | ||
/> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
name: 'VirtualScroll', | ||
props: { | ||
items: { | ||
type: Array, | ||
required: true | ||
}, | ||
itemHeight: { | ||
type: Number, | ||
required: true | ||
}, | ||
offset: { | ||
type: Number, | ||
default: 5 | ||
} | ||
}, | ||
data () { | ||
return { | ||
topHeight: 0, | ||
bottomHeight: 0, | ||
visibleItems: [], | ||
lastRenderedItemIndex: 0 | ||
} | ||
}, | ||
watch: { | ||
items: function () { | ||
this.refreshItems() | ||
} | ||
}, | ||
mounted () { | ||
this._checkScrollPosition = this.checkScrollPosition.bind(this) | ||
this.checkScrollPosition() | ||
this.$el.addEventListener('scroll', this._checkScrollPosition) | ||
this.$el.addEventListener('wheel', this._checkScrollPosition) | ||
}, | ||
beforeDestroy () { | ||
this.$el.removeEventListener('scroll', this._checkScrollPosition) | ||
this.$el.removeEventListener('wheel', this._checkScrollPosition) | ||
}, | ||
methods: { | ||
checkScrollPosition (e = {}) { | ||
const el = this.$el | ||
// prevent parent scroll | ||
if ( | ||
(el.scrollTop === 0 && e.deltaY < 0) || | ||
(Math.abs(el.scrollTop - (el.scrollHeight - el.clientHeight)) <= 1 && | ||
e.deltaY > 0) | ||
) { | ||
e.preventDefault() | ||
} | ||
this.updateWindow(e) | ||
}, | ||
updateWindow () { | ||
const visibleItemsCount = Math.ceil( | ||
this.$el.clientHeight / this.itemHeight | ||
) | ||
const totalScrollHeight = this.items.length * this.itemHeight | ||
const scrollTop = this.$el.scrollTop | ||
const firstVisibleIndex = Math.floor(scrollTop / this.itemHeight) | ||
const lastVisibleIndex = firstVisibleIndex + visibleItemsCount | ||
const lastCutIndex = lastVisibleIndex + this.offset | ||
if (lastCutIndex > this.lastRenderedItemIndex) { | ||
this.visibleItems = this.items.slice(0, lastCutIndex) | ||
this.lastRenderedItemIndex = lastCutIndex | ||
} | ||
this.bottomHeight = | ||
totalScrollHeight - | ||
this.visibleItems.length * this.itemHeight - | ||
this.topHeight | ||
}, | ||
refreshItems () { | ||
this.visibleItems = this.items.slice(0, this.lastRenderedItemIndex) | ||
} | ||
} | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters