Yacine Jernite
commited on
Commit
·
4884b2f
1
Parent(s):
63f3b21
fix sticky nav auto minimize
Browse files- js/utils/stickyNavigation.js +64 -1
- styles.css +2 -2
js/utils/stickyNavigation.js
CHANGED
|
@@ -25,10 +25,73 @@ export function initializeStickyNavigation(navElementId) {
|
|
| 25 |
|
| 26 |
// Scroll handler with throttling
|
| 27 |
let scrollTimeout;
|
|
|
|
|
|
|
| 28 |
const handleScroll = () => {
|
| 29 |
const scrollPosition = window.pageYOffset || document.documentElement.scrollTop;
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
window.addEventListener('scroll', () => {
|
| 34 |
if (scrollTimeout) cancelAnimationFrame(scrollTimeout);
|
|
|
|
| 25 |
|
| 26 |
// Scroll handler with throttling
|
| 27 |
let scrollTimeout;
|
| 28 |
+
let navOriginalHeight = 0; // Initialize to 0
|
| 29 |
+
|
| 30 |
const handleScroll = () => {
|
| 31 |
const scrollPosition = window.pageYOffset || document.documentElement.scrollTop;
|
| 32 |
+
const shouldBeSticky = scrollPosition >= stickyThreshold;
|
| 33 |
+
|
| 34 |
+
// Toggle sticky class
|
| 35 |
+
if (shouldBeSticky && !navSection.classList.contains('sticky-nav')) {
|
| 36 |
+
// Capture TOTAL HEIGHT including margins BEFORE becoming sticky
|
| 37 |
+
const computedStyle = window.getComputedStyle(navSection);
|
| 38 |
+
const marginTop = parseInt(computedStyle.marginTop) || 0;
|
| 39 |
+
const marginBottom = parseInt(computedStyle.marginBottom) || 0;
|
| 40 |
+
navOriginalHeight = navSection.offsetHeight + marginTop + marginBottom;
|
| 41 |
+
|
| 42 |
+
// Becoming sticky - auto-collapse navigation
|
| 43 |
+
navSection.classList.add('sticky-nav');
|
| 44 |
+
|
| 45 |
+
// Push down following content to compensate for removed element
|
| 46 |
+
const nextElement = navSection.nextElementSibling;
|
| 47 |
+
if (nextElement) {
|
| 48 |
+
nextElement.style.marginTop = `${navOriginalHeight}px`;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
autoCollapseNavigation();
|
| 52 |
+
} else if (!shouldBeSticky && navSection.classList.contains('sticky-nav')) {
|
| 53 |
+
// Leaving sticky - auto-expand navigation
|
| 54 |
+
navSection.classList.remove('sticky-nav');
|
| 55 |
+
|
| 56 |
+
// Remove the compensating margin
|
| 57 |
+
const nextElement = navSection.nextElementSibling;
|
| 58 |
+
if (nextElement) {
|
| 59 |
+
nextElement.style.marginTop = '';
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
autoExpandNavigation();
|
| 63 |
+
}
|
| 64 |
};
|
| 65 |
+
|
| 66 |
+
/**
|
| 67 |
+
* Auto-collapse navigation when becoming sticky
|
| 68 |
+
*/
|
| 69 |
+
function autoCollapseNavigation() {
|
| 70 |
+
const navItems = document.getElementById('nav-items');
|
| 71 |
+
const navToggle = document.getElementById('nav-toggle');
|
| 72 |
+
const navContainer = document.getElementById('nav-container');
|
| 73 |
+
|
| 74 |
+
if (navItems && !navItems.classList.contains('nav-collapsed')) {
|
| 75 |
+
navItems.classList.add('nav-collapsed');
|
| 76 |
+
navToggle?.setAttribute('aria-expanded', 'false');
|
| 77 |
+
navContainer?.classList.add('nav-container-collapsed');
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
/**
|
| 82 |
+
* Auto-expand navigation when leaving sticky mode
|
| 83 |
+
*/
|
| 84 |
+
function autoExpandNavigation() {
|
| 85 |
+
const navItems = document.getElementById('nav-items');
|
| 86 |
+
const navToggle = document.getElementById('nav-toggle');
|
| 87 |
+
const navContainer = document.getElementById('nav-container');
|
| 88 |
+
|
| 89 |
+
if (navItems && navItems.classList.contains('nav-collapsed')) {
|
| 90 |
+
navItems.classList.remove('nav-collapsed');
|
| 91 |
+
navToggle?.setAttribute('aria-expanded', 'true');
|
| 92 |
+
navContainer?.classList.remove('nav-container-collapsed');
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
|
| 96 |
window.addEventListener('scroll', () => {
|
| 97 |
if (scrollTimeout) cancelAnimationFrame(scrollTimeout);
|
styles.css
CHANGED
|
@@ -201,7 +201,7 @@ p {
|
|
| 201 |
}
|
| 202 |
|
| 203 |
.page-navigation.sticky-nav #nav-container {
|
| 204 |
-
background: rgba(
|
| 205 |
backdrop-filter: blur(16px);
|
| 206 |
-webkit-backdrop-filter: blur(16px);
|
| 207 |
padding: 0.75rem 1.5rem;
|
|
@@ -212,7 +212,7 @@ p {
|
|
| 212 |
/* Add breathing room between sticky nav and content */
|
| 213 |
.page-navigation.sticky-nav ~ section:first-of-type,
|
| 214 |
.page-navigation.sticky-nav ~ div:first-of-type {
|
| 215 |
-
margin-top: 0
|
| 216 |
}
|
| 217 |
|
| 218 |
/* Navigation collapse/expand */
|
|
|
|
| 201 |
}
|
| 202 |
|
| 203 |
.page-navigation.sticky-nav #nav-container {
|
| 204 |
+
background: rgba(240, 247, 248, 0.3);
|
| 205 |
backdrop-filter: blur(16px);
|
| 206 |
-webkit-backdrop-filter: blur(16px);
|
| 207 |
padding: 0.75rem 1.5rem;
|
|
|
|
| 212 |
/* Add breathing room between sticky nav and content */
|
| 213 |
.page-navigation.sticky-nav ~ section:first-of-type,
|
| 214 |
.page-navigation.sticky-nav ~ div:first-of-type {
|
| 215 |
+
margin-top: 0;
|
| 216 |
}
|
| 217 |
|
| 218 |
/* Navigation collapse/expand */
|