2022-04-15 14:24:30 +00:00
|
|
|
/**
|
|
|
|
* A utility that aims to replace jQuery for the most basic DOM methods.
|
|
|
|
*/
|
|
|
|
export const $ = {
|
2022-05-05 15:30:10 +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)
|
|
|
|
}
|
|
|
|
}
|