koel/resources/assets/js/directives/overflowFade.ts

19 lines
661 B
TypeScript
Raw Normal View History

import { Directive } from 'vue'
const toggleClasses = (el: HTMLElement) => {
el.classList.toggle('fade-top', el.scrollTop !== 0)
el.classList.toggle('fade-bottom', el.scrollTop + el.clientHeight !== el.scrollHeight)
}
const observeVisibility = (el: HTMLElement, callback: Closure) => {
const observer = new IntersectionObserver(entries => entries.forEach(entry => entry.isIntersecting && callback()))
observer.observe(el)
}
export const overflowFade: Directive = {
mounted: async (el: HTMLElement) => {
observeVisibility(el, () => toggleClasses(el))
el.addEventListener('scroll', () => requestAnimationFrame(() => toggleClasses(el)))
}
}