koel/resources/assets/js/utils/$.ts

29 lines
763 B
TypeScript
Raw Normal View History

2022-04-15 14:24:30 +00:00
/**
* A utility that aims to replace jQuery for the most basic DOM methods.
*/
export const $ = {
2022-04-25 16:38:33 +00:00
is: (el: Element, selector: string) => el.matches(selector),
addClass: (el: Element | null, className: string) => el?.classList.add(className),
removeClass: (el: Element | null, className: string) => el?.classList.remove(className),
2022-04-15 14:24:30 +00:00
scrollTo (el: Element, to: number, duration: number, cb?: Closure) {
2022-04-15 14:24:30 +00:00
if (duration <= 0 || !el) {
return
}
const difference = to - el.scrollTop
const perTick = difference / duration * 10
2022-04-25 16:38:33 +00:00
window.setTimeout(() => {
2022-04-15 14:24:30 +00:00
el.scrollTop = el.scrollTop + perTick
if (el.scrollTop === to) {
cb && cb()
return
}
this.scrollTo(el, to, duration - 10)
}, 10)
}
}