-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-browser.ts
48 lines (40 loc) · 1.42 KB
/
gatsby-browser.ts
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
const scrollUtils = require('./src/utils/scrollUtils');
exports.shouldUpdateScroll = ({ routerProps, getSavedScrollPosition }: any) => {
const { location } = routerProps;
const hash = location && location.hash;
const targetOffset = hash
? document.querySelector(hash).getBoundingClientRect().top
: getSavedScrollPosition(location)[1] - window.scrollY;
const startOffset = window.scrollY;
const change = targetOffset;
const duration = 1000;
const startTime = performance.now();
function animateScroll() {
const currentTime = performance.now();
const elapsed = currentTime - startTime;
window.scrollTo(
0,
easeInOutQuad(elapsed, startOffset, change, duration)
);
if (elapsed < duration) {
requestAnimationFrame(animateScroll);
}
}
function easeInOutQuad(
timeProgress: number,
startValue: number,
difference: number,
animationDuration: number
) {
timeProgress /= animationDuration;
const slowdown = scrollUtils.customScrollEase(timeProgress);
return difference * slowdown + startValue;
}
if (scrollUtils.navigateScrollEffect.enabled) {
requestAnimationFrame(animateScroll);
} else {
const savedPosition = getSavedScrollPosition(location);
window.scrollTo(...(savedPosition || [0, 0]));
}
return false;
};