15815213711
2022-03-07 56f8b51c26bd1fb7e1fdc62acab5151cdf83c860
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function easeInOutQuad(t: number, b: number, c: number, d: number) {
    t /= d / 2
    if (t < 1) {
        return c / 2 * t * t + b
    }
    t--
    return -c / 2 * (t * (t - 2) - 1) + b
}
 
// requestAnimationFrame for Smart Animating http://goo.gl/sx5sts
var requestAnimFrame = (function () {
    return window.requestAnimationFrame || function (callback) {
        window.setTimeout(callback, 1000 / 60)
    } //|| window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame
})()
 
/**
 * Because it's so fucking difficult to detect the scrolling element, just move them all
 * @param {number} amount
 */
function move(amount: number) {
    document.documentElement.scrollTop = amount
    //document.body.parentNode.scrollTop = amount
    document.body.scrollTop = amount
}
 
function position() {
    return document.documentElement.scrollTop || document.body.scrollTop
}
 
export function scrollTo(to: number, duration: number, callback?: any) {
    const start = position();
    const change = to - start;
    const increment = 10;
    let currentTime = 0;
    duration = (typeof (duration) === 'undefined') ? 500 : duration;
    const animateScroll = () => {
        currentTime += increment;
        let val = easeInOutQuad(currentTime, start, change, duration);
        move(val);
        if (currentTime < duration) {
            requestAnimFrame(animateScroll)
        } else {
            if (callback && typeof (callback) === 'function') {
                // the animation is done so lets callback
                callback()
            }
        }
    }
    animateScroll()
}