-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
81 lines (67 loc) · 2.11 KB
/
script.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
////////////////////////////////////////////////////////////////////////////////
// Setup
window.addEventListener('load', main);
function main() {
window.addEventListener('scroll', handleScrollFramerate);
onScroll();
window.addEventListener('resize', handleResizeFramerate);
onResize();
var scrollUpButton = document.getElementById('scroll-up');
scrollUpButton.addEventListener('click', scrollToStart);
var scrollDownButton = document.getElementById('scroll-down');
scrollDownButton.addEventListener('click', scrollToEnd);
}
////////////////////////////////////////////////////////////////////////////////
// Scroll
function handleScrollFramerate() {
window.requestAnimationFrame(onScroll);
}
function onScroll() {
updateScrollPosition();
updateInfoBackground();
}
function updateScrollPosition() {
var offset = document.getElementById('offset');
offset.textContent = window.scrollY;
}
function updateInfoBackground() {
var bodyHeight = document.body.clientHeight - window.innerHeight;
var scrollPercentage = window.scrollY / bodyHeight;
var info = document.getElementById('info');
info.style.background = `hsl(${parseInt(360 * scrollPercentage)}, 100%, 50%)`;
}
////////////////////////////////////////////////////////////////////////////////
// Resize
function handleResizeFramerate() {
window.requestAnimationFrame(onResize);
}
function onResize() {
updateWindowDimensions();
updateWindowBackground();
}
function updateWindowDimensions() {
var width = document.getElementById('width');
var height = document.getElementById('height');
width.textContent = window.innerWidth;
height.textContent = window.innerHeight;
}
function updateWindowBackground() {
document.body.style.backgroundSize = `${window.innerWidth / 10}px`;
}
////////////////////////////////////////////////////////////////////////////////
// Click
function scrollToStart() {
window.scroll({
top: 0,
left: 0,
behavior: 'smooth'
});
}
function scrollToEnd() {
var bodyHeight = document.body.clientHeight - window.innerHeight;
window.scroll({
top: bodyHeight,
left: 0,
behavior: 'smooth'
});
}