-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
54 lines (43 loc) · 1.47 KB
/
main.js
File metadata and controls
54 lines (43 loc) · 1.47 KB
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
document.addEventListener("DOMContentLoaded", function () {
const wrapper = document.querySelector('.header-navbar-wrapper');
const main = document.querySelector('main');
// Calculate height dynamically and set CSS variable
const updateWrapperHeight = () => {
const totalHeight = wrapper.offsetHeight;
document.documentElement.style.setProperty('--header-navbar-height', totalHeight + 'px');
};
updateWrapperHeight();
window.addEventListener('resize', updateWrapperHeight);
let lastScrollTop = 0;
let scrollTimeout;
function handleScroll() {
const currentScroll = window.pageYOffset || document.documentElement.scrollTop;
if (scrollTimeout) clearTimeout(scrollTimeout);
if (currentScroll <= 10) {
wrapper.style.transform = 'translateY(0)';
lastScrollTop = currentScroll;
return;
}
if (currentScroll > lastScrollTop && currentScroll > 100) {
// Hide wrapper (header + navbar)
wrapper.style.transform = 'translateY(-100%)';
} else if (currentScroll < lastScrollTop) {
// Show wrapper
wrapper.style.transform = 'translateY(0)';
}
scrollTimeout = setTimeout(() => {
wrapper.style.transform = 'translateY(0)';
}, 1500);
lastScrollTop = Math.max(currentScroll, 0);
}
let ticking = false;
window.addEventListener('scroll', () => {
if (!ticking) {
requestAnimationFrame(() => {
handleScroll();
ticking = false;
});
ticking = true;
}
});
});