koel/resources/assets/js/mixins/infinite-scroll.js

35 lines
942 B
JavaScript
Raw Normal View History

2016-11-26 03:25:35 +00:00
import toTopButton from '../components/shared/to-top-button.vue'
2015-12-13 04:42:28 +00:00
/**
2015-12-14 03:45:15 +00:00
* Add a "infinite scroll" functionality to any component using this mixin.
* Such a component should have a `scrolling` method bound to `scroll` event on
* the wrapper element: @scroll="scrolling"
2015-12-13 04:42:28 +00:00
*/
export default {
2016-06-25 16:05:24 +00:00
components: { toTopButton },
2016-11-26 03:25:35 +00:00
data () {
2016-06-25 16:05:24 +00:00
return {
numOfItems: 30, // Number of currently loaded and displayed items
perPage: 30 // Number of items to be loaded per "page"
2016-11-26 03:25:35 +00:00
}
2016-06-25 16:05:24 +00:00
},
methods: {
2016-11-26 03:25:35 +00:00
scrolling (e) {
2016-06-25 16:05:24 +00:00
// Here we check if the user has scrolled to the end of the wrapper (or 32px to the end).
// If that's true, load more items.
if (e.target.scrollTop + e.target.clientHeight >= e.target.scrollHeight - 32) {
2016-11-26 03:25:35 +00:00
this.displayMore()
2016-06-25 16:05:24 +00:00
}
2015-12-13 04:42:28 +00:00
},
2016-06-25 16:05:24 +00:00
/**
* Load and display more items into the scrollable area.
*/
2016-11-26 03:25:35 +00:00
displayMore () {
this.numOfItems += this.perPage
}
}
}