mirror of
https://github.com/koel/koel
synced 2024-12-25 20:13:06 +00:00
41 lines
990 B
TypeScript
41 lines
990 B
TypeScript
import { ref } from 'vue'
|
|
import ToTopButton from '@/components/ui/BtnScrollToTop.vue'
|
|
|
|
export const useInfiniteScroll = (loadMore: Closure) => {
|
|
const scroller = ref<HTMLElement>()
|
|
|
|
const scrolling = (event: UIEvent) => {
|
|
const target = event.target as HTMLElement
|
|
|
|
// 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 (target.scrollTop + target.clientHeight >= target.scrollHeight - 32) {
|
|
loadMore()
|
|
}
|
|
}
|
|
|
|
let tries = 0
|
|
const MAX_TRIES = 5
|
|
|
|
const makeScrollable = async () => {
|
|
const container = scroller.value
|
|
|
|
if (!container) {
|
|
window.setTimeout(() => makeScrollable(), 200)
|
|
return
|
|
}
|
|
|
|
if (container.scrollHeight <= container.clientHeight && tries < MAX_TRIES) {
|
|
tries++
|
|
await loadMore()
|
|
window.setTimeout(() => makeScrollable(), 200)
|
|
}
|
|
}
|
|
|
|
return {
|
|
ToTopButton,
|
|
scroller,
|
|
scrolling,
|
|
makeScrollable
|
|
}
|
|
}
|