-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
84 lines (68 loc) · 2.31 KB
/
index.html
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
82
83
84
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scroll Up Header</title>
<style>
body{
padding:0;
margin:0;
}
header{
display:block;
position: fixed;
background:#ddd;
padding:0;
width:100%;
height:90px;
margin-top:0px;
transition: margin 200ms ease-in;
}
.scrolling-down header{margin-top:-90px;}
.scrolling-up header{margin-top:0px;}
main{
height:200vh;
background:#ccc;
padding:120px 30px;
}
</style>
</head>
<body>
<header id="masthead">Header</header>
<main>Body</main>
<script>
//ref: https://developer.mozilla.org/en-US/docs/Web/API/Document/scroll_event
let lastScrollPosition = window.scrollY;
let currentScrollPosition = 0;
let ticking = false; // stops the event from overfiring see ref link
function scrollUpDown(scrollPos, lastScrollPos) {
// Do something with the scroll position
//console.log('scrolling' + scrollPos);
//console.log('scrolling last pos' + lastScrollPos);
if (scrollPos > lastScrollPos){
//SCROLLING UP
document.body.classList.remove('scrolling-up');
document.body.classList.add('scrolling-down');
console.log('SCROLLING DOWN');
} else {
//SCROLLING DOWN
document.body.classList.add('scrolling-up');
document.body.classList.remove('scrolling-down');
console.log('SCROLLING UP');
}
}
window.addEventListener('scroll', function(e) {
currentScrollPosition = window.scrollY;
if (!ticking) {
window.requestAnimationFrame(function() {
scrollUpDown(currentScrollPosition, lastScrollPosition);
lastScrollPosition = currentScrollPosition;
ticking = false;
});
ticking = true;
}
});
</script>
</body>
</html>