fix: sticky nav

This commit is contained in:
2024-12-13 00:04:56 +01:00
parent daa02d36d5
commit 9f410a339e
2 changed files with 20 additions and 31 deletions

View File

@@ -1,8 +1,6 @@
document.addEventListener('DOMContentLoaded', function () { document.addEventListener('DOMContentLoaded', function () {
const nav = document.getElementById('mainNav'); const nav = document.getElementById('mainNav');
const toggle = document.getElementById('navToggle'); const toggle = document.getElementById('navToggle');
const navLinks = document.getElementById('navLinks');
let lastScrollTop = 0;
// Toggle menu on mobile // Toggle menu on mobile
toggle.addEventListener('click', function () { toggle.addEventListener('click', function () {
@@ -10,19 +8,9 @@ document.addEventListener('DOMContentLoaded', function () {
toggle.classList.toggle('active'); toggle.classList.toggle('active');
}); });
// Sticky on scroll // No need to add or remove sticky class here since CSS handles position: sticky
window.addEventListener('scroll', function () { // We simply rely on the CSS-based sticky behavior and reserved space via body padding.
const currentScroll = window.pageYOffset || document.documentElement.scrollTop;
if (currentScroll > 0) {
nav.classList.add('sticky');
} else {
nav.classList.remove('sticky');
}
lastScrollTop = currentScroll <= 0 ? 0 : currentScroll;
});
// Optionally handle dropdowns on mobile (if desired)
// For example:
const submenuParents = document.querySelectorAll('.nav-item.has-submenu'); const submenuParents = document.querySelectorAll('.nav-item.has-submenu');
submenuParents.forEach(parent => { submenuParents.forEach(parent => {
parent.addEventListener('click', (e) => { parent.addEventListener('click', (e) => {
@@ -31,7 +19,6 @@ document.addEventListener('DOMContentLoaded', function () {
const subMenu = parent.querySelector('.sub-menu'); const subMenu = parent.querySelector('.sub-menu');
if (subMenu) { if (subMenu) {
const isOpen = subMenu.style.display === 'block'; const isOpen = subMenu.style.display === 'block';
// Close all open submenus first (optional)
document.querySelectorAll('.sub-menu').forEach(sm => sm.style.display = 'none'); document.querySelectorAll('.sub-menu').forEach(sm => sm.style.display = 'none');
subMenu.style.display = isOpen ? 'none' : 'block'; subMenu.style.display = isOpen ? 'none' : 'block';
} }

View File

@@ -1,23 +1,26 @@
.main-nav { // Define a fixed height for the navigation and apply that as padding-top on the body
width: 100%; // so that when it becomes sticky, the content doesn't jump.
background: $secondary-color url('EXT:my_sitepackage/Resources/Public/Images/linen-texture.png') repeat; $nav-height: 60px; // Adjust as needed
position: relative;
z-index: 1000;
&.sticky { header {
position: fixed; width: 100%;
height: $nav-height;
line-height: $nav-height;
background: $secondary-color url('EXT:my_sitepackage/Resources/Public/Images/linen-texture.png') repeat;
position: sticky;
top: 0; top: 0;
left: 0; z-index: 1000;
right: 0;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
} }
.main-nav {
.container { .container {
display: flex; display: flex;
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-between;
margin: 0 auto; margin: 0 auto;
padding: 1rem; height: 100%;
padding: 0 1rem;
} }
.nav-logo { .nav-logo {
@@ -82,6 +85,7 @@
.nav-links { .nav-links {
display: flex; display: flex;
list-style: none; list-style: none;
align-items: center;
.nav-item { .nav-item {
margin: 0 1rem; margin: 0 1rem;
@@ -102,13 +106,11 @@
} }
} }
// Responsive: show hamburger and collapse menu on smaller screens
@media (max-width: $breakpoint-lg) { @media (max-width: $breakpoint-lg) {
.nav-toggle { .nav-toggle {
display: block; display: block;
} }
// Hide links by default on mobile and show only when toggled
.nav-links { .nav-links {
position: absolute; position: absolute;
top: 100%; top: 100%;