-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
46 lines (39 loc) · 1.3 KB
/
index.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
const nav = document.querySelector('.nav');
const move = document.querySelector('.move-up');
const audio = document.querySelector('#myAudio');
document.addEventListener('click', () => {
// Start the Song
audio.play();
});
document.addEventListener('scroll', () => {
let scroll_position = window.scrollY;
if (scroll_position > 250) {
nav.classList.add('nav--scroll');
move.classList.add('move-up--vis');
} else {
nav.classList.remove('nav--scroll');
move.classList.remove('move-up--vis');
}
move.addEventListener("click", function () {
scrollToTop(1000);
});
// Start the Song
audio.play();
});
function scrollToTop(duration) {
// Cancel if already on top
if (document.scrollingElement.scrollTop === 0) return;
const totalScrollDistance = document.scrollingElement.scrollTop;
let scrollY = totalScrollDistance, oldTimestamp = null;
function step(newTimestamp) {
if (oldTimestamp !== null) {
// If duration is 0 scrollY will be -Infinity
scrollY -= totalScrollDistance * (newTimestamp - oldTimestamp) / duration;
if (scrollY <= 0) return document.scrollingElement.scrollTop = 0;
document.scrollingElement.scrollTop = scrollY;
}
oldTimestamp = newTimestamp;
window.requestAnimationFrame(step);
}
window.requestAnimationFrame(step);
}