-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
49 lines (41 loc) · 1.49 KB
/
app.js
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
const menuLinks = document.querySelectorAll('.menu a[href^="#"]');
function getDistanceFromTheTop(element) {
const id = element.getAttribute("href");
return document.querySelector(id).offsetTop;
}
// function nativeScroll(distanceFromTheTop) {
// window.scroll({
// top: distanceFromTheTop,
// behavior: "smooth",
// });
// }
function scrollToSection(event) {
event.preventDefault();
const distanceFromTheTop = getDistanceFromTheTop(event.target) - 90;
smoothScrollTo(0, distanceFromTheTop);
}
menuLinks.forEach((link) => {
link.addEventListener("click", scrollToSection);
});
function smoothScrollTo(endX, endY, duration) {
const startX = window.scrollX || window.pageXOffset;
const startY = window.scrollY || window.pageYOffset;
const distanceX = endX - startX;
const distanceY = endY - startY;
const startTime = new Date().getTime();
duration = typeof duration !== "undefined" ? duration : 400;
const easeInOutQuart = (time, from, distance, duration) => {
if ((time /= duration / 2) < 1)
return (distance / 2) * time * time * time * time + from;
return (-distance / 2) * ((time -= 2) * time * time * time - 2) + from;
};
const timer = setInterval(() => {
const time = new Date().getTime() - startTime;
const newX = easeInOutQuart(time, startX, distanceX, duration);
const newY = easeInOutQuart(time, startY, distanceY, duration);
if (time >= duration) {
clearInterval(timer);
}
window.scroll(newX, newY);
}, 1000 / 60);
}