Wie wird es erstellt: Mobile Navigationsleiste
- Vorherige Seite Super-Menü
- Nächste Seite Vorhang-Menü
Lernen Sie, wie Sie eine obere Navigationsleiste für Smartphones/Tablets mit CSS und JavaScript erstellen.
Mobile Navigationsleiste
Vertikal (empfohlen):
Horizontal:
Erstellen Sie eine mobile Navigationsleiste
Schritt 1 - Fügen Sie HTML hinzu:
<!-- 加载图标库以在小屏幕上显示汉堡菜单(三横杠) --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <!-- Top Navigation Menu --> <div class="topnav"> <a href="#home" class="active">Logo</a> <!-- Navigation links (hidden by default) --> <div id="myLinks"> <a href="#news">News</a> <a href="#contact">Contact</a> <a href="#about">Über</a> </div> <!-- "Hamburger-Menü" / "Waagerechtes Symbol" zum Umschalten der Navigationsverknüpfungen --> <a href="javascript:void(0);" class="icon" onclick="myFunction()"> <i class="fa fa-bars"></i> </a> </div>
Zweiter Schritt - Fügen Sie CSS hinzu:
/* Stile für das Navigationsmenü einstellen */ .topnav { overflow: hidden; background-color: #333; position: relative; } /* Verberge die Links im Navigationsmenü (außer dem Logo / Startseite) */ .topnav #myLinks { display: none; } /* Stile für die Navigationsverknüpfungen im Menü einstellen */ .topnav a { color: white; padding: 14px 16px; text-decoration: none; font-size: 17px; display: block; } /* Stile für das Hamburger-Menü einstellen */ .topnav a.icon { background: black; display: block; position: absolute; right: 0; top: 0; } /* Bei der Mausüberlagerung wird eine graue Hintergrundfarbe hinzugefügt */ .topnav a:hover { background-color: #ddd; color: black; } /* Stile für aktive Links (oder Startseite / Logo) einstellen */ .active { background-color: #04AA6D; color: white; }
Dritter Schritt - Fügen Sie JavaScript hinzu:
/* Wenn der Benutzer auf das Hamburger-Menü / das waagerechte Symbol klickt, wechselt der Anzeigestatus der Navigationsverknüpfungen zwischen Anzeigen und Verbergen */ function myFunction() { var x = document.getElementById("myLinks"); if (x.style.display === "block") { x.style.display = "none"; } else { x.style.display = "block"; } }
Verwandte Seiten
Tutorials:CSS-Navigationsleiste
Tutorials:Wie kann man erstellen: Reaktive obere Navigationsleiste
- Vorherige Seite Super-Menü
- Nächste Seite Vorhang-Menü