mirror of
https://github.com/kevinveenbirkenbach/homepage.veen.world.git
synced 2025-01-15 19:23:58 +01:00
Refactored code
This commit is contained in:
parent
f9d5a90f94
commit
ac0b1e9a14
@ -2,13 +2,14 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
const menuItems = document.querySelectorAll('.nav-item.dropdown');
|
const menuItems = document.querySelectorAll('.nav-item.dropdown');
|
||||||
const subMenuItems = document.querySelectorAll('.dropdown-submenu');
|
const subMenuItems = document.querySelectorAll('.dropdown-submenu');
|
||||||
|
|
||||||
menuItems.forEach(item => {
|
function addMenuEventListeners(items, isTopLevel) {
|
||||||
|
items.forEach(item => {
|
||||||
let timeout;
|
let timeout;
|
||||||
|
|
||||||
// Öffnen beim Hovern
|
// Öffnen beim Hovern
|
||||||
item.addEventListener('mouseenter', () => {
|
item.addEventListener('mouseenter', () => {
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
openMenu(item, true);
|
openMenu(item, isTopLevel);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Verzögertes Schließen beim Verlassen
|
// Verzögertes Schließen beim Verlassen
|
||||||
@ -23,36 +24,14 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
if (item.classList.contains('open')) {
|
if (item.classList.contains('open')) {
|
||||||
closeMenu(item);
|
closeMenu(item);
|
||||||
} else {
|
} else {
|
||||||
openMenu(item, true);
|
openMenu(item, isTopLevel);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
subMenuItems.forEach(item => {
|
|
||||||
let timeout;
|
|
||||||
|
|
||||||
// Öffnen beim Hovern
|
|
||||||
item.addEventListener('mouseenter', () => {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
openMenu(item, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Verzögertes Schließen beim Verlassen
|
|
||||||
item.addEventListener('mouseleave', () => {
|
|
||||||
timeout = setTimeout(() => closeMenu(item), 500);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Öffnen und Position anpassen beim Klicken
|
|
||||||
item.addEventListener('click', (e) => {
|
|
||||||
e.preventDefault(); // Verhindert die Standardaktion
|
|
||||||
e.stopPropagation(); // Verhindert das Schließen von Menüs bei Klick
|
|
||||||
if (item.classList.contains('open')) {
|
|
||||||
closeMenu(item);
|
|
||||||
} else {
|
|
||||||
openMenu(item, false);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
addMenuEventListeners(menuItems, true);
|
||||||
|
addMenuEventListeners(subMenuItems, false);
|
||||||
|
|
||||||
// Globale Klick-Listener, um Menüs zu schließen, wenn außerhalb geklickt wird
|
// Globale Klick-Listener, um Menüs zu schließen, wenn außerhalb geklickt wird
|
||||||
document.addEventListener('click', () => {
|
document.addEventListener('click', () => {
|
||||||
@ -114,7 +93,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
// Öffnen nach oben, wenn unten kein Platz ist
|
// Öffnen nach oben, wenn unten kein Platz ist
|
||||||
if (spaceBelow < rect.height && spaceAbove > rect.height) {
|
if (spaceBelow < rect.height && spaceAbove > rect.height) {
|
||||||
submenu.style.top = 'auto';
|
submenu.style.top = 'auto';
|
||||||
submenu.style.bottom = `${parentRect.bottom - parentRect.top}px`;
|
submenu.style.bottom = `${parentRect.bottom - parentRect.top - rect.height}px`; // Höhe des Submenüs wird berücksichtigt
|
||||||
} else {
|
} else {
|
||||||
submenu.style.top = '0';
|
submenu.style.top = '0';
|
||||||
submenu.style.bottom = 'auto';
|
submenu.style.bottom = 'auto';
|
||||||
|
Loading…
Reference in New Issue
Block a user