-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHideNavbar.js
69 lines (47 loc) · 1.62 KB
/
HideNavbar.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
// PARAM1: ELEMENT TO HIDE
// PARAM2: ELEMENT ID TO HIDE
export default function hideNavbar (element, elementId) {
const nav = document.querySelector(element + "[id*='" + elementId + "']");
const supportPageOffset = window.pageXOffset !== undefined;
const isCSS1Compat = (document.compatMode || "") === "CSS1Compat";
let previousScrollPosition = 0;
const isScrollingDown = () => {
let scrolledPosition = supportPageOffset
? window.pageYOffset
: isCSS1Compat
? document.documentElement.scrollTop
: document.body.scrollTop;
let isScrollDown;
if (scrolledPosition > previousScrollPosition) {
isScrollDown = true;
} else {
isScrollDown = false;
}
previousScrollPosition = scrolledPosition;
return isScrollDown;
};
const handleNavScroll = () => {
if (isScrollingDown() && !nav.contains(document.activeElement)) {
nav.classList.add("scroll-down");
nav.classList.remove("scroll-up");
} else {
nav.classList.add("scroll-up");
nav.classList.remove("scroll-down");
}
};
var throttleTimer;
const throttle = (callback, time) => {
if (throttleTimer) return;
throttleTimer = true;
setTimeout(() => {
callback();
throttleTimer = false;
}, time);
};
const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
window.addEventListener("scroll", () => {
if (mediaQuery && !mediaQuery.matches) {
throttle(handleNavScroll, 250);
}
});
}