-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmoothScroll.js
22 lines (19 loc) · 891 Bytes
/
smoothScroll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[
{ trigger: ".btn1", target: "section1", offset: 0, speed: 1000 },
{ trigger: ".btn2", target: "section2", offset: 0, speed: 1000 },
{ trigger: ".btn3", target: "section3", offset: 0, speed: 1000 }
].forEach(({ trigger, target, offset, speed }) => {
document.querySelectorAll(trigger).forEach(button => {
button.addEventListener("click", () => {
const start = window.scrollY,
end = document.getElementById(target).offsetTop - offset,
t0 = performance.now();
requestAnimationFrame(function step(t) {
const p = Math.min((t - t0) / speed, 1),
e = p < .5 ? 2 * p * p : 1 - Math.pow(-2 * p + 2, 2) / 2;
window.scrollTo(0, start + (end - start) * e);
if (p < 1) requestAnimationFrame(step);
});
});
});
});