Wie man erstellt: Vollbild-Überlagerungs-Navigation

Lernen Sie, wie man ein vollbildendes Überlagerungs-Navigation-Menü erstellt.

Klicken Sie auf die folgenden Schaltflächen, um zu sehen, wie sie funktionieren:

Try it yourself

Erstellen Sie eine vollbildende Überlagerungs-Navigation

Schritt 1 - Fügen Sie HTML hinzu:

<!-- Überlagerungsschicht -->
<div id="myNav" class="overlay">
  <!-- Klickbare Schicht-Navigation schließen -->
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
  <!-- Overlay-Inhalt -->
  <div class="overlay-content">
    <a href="#">Über</a>
    <a href="#">Dienste</a>
    <a href="#">Kunden</a>
    <a href="#">Kontakt</a>
  </div>
</div>
/* Use any element to open/display the overlay navigation menu */
<span onclick="openNav()">open</span>

第二步 - 添加 CSS:

/* Overlay (background) */
.overlay {
  /* Height and width depend on how you want to display the overlay (see the following JS) */   
  height: 100%;
  width: 0;
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  left: 0;
  top: 0;
  background-color: rgb(0,0,0); /* Alternative black */
  background-color: rgba(0,0,0, 0.9); /* Semi-transparent black */
  overflow-x: hidden; /* Disable horizontal scrolling */
  transition: 0.5s; /* 0.5 second transition effect, making the overlay slide in or out (height or width depends on display) */
}
/* Position the content inside the overlay */
.overlay-content {
  position: relative;
  top: 25%; /* 25% from the top */
  width: 100%; /* 100% width */
  text-align: center; /* Centered text/links */
  margin-top: 30px; /* Top outer margin 30 pixels to avoid conflict with the close button on small screens */
}
/* Navigation links within the overlay */
.overlay a {
  padding: 8px;
  text-decoration: none;
  font-size: 36px;
  color: #818181;
  display: block; /* Display block instead of inline */
  transition: 0.3s; /* Transition effect (color) when the mouse hovers */
}
/* Change the color when the mouse hovers over the navigation link */
.overlay a:hover, .overlay a:focus {
  color: #f1f1f1;
}
/* Position the close button (top right corner) */
.overlay .closebtn {
  position: absolute;
  top: 20px;
  right: 45px;
  font-size: 60px;
}
Wenn die Bildschirmhöhe weniger als 450 Pixel beträgt, ändern Sie die Schriftgröße der Links und positionieren Sie den Schließknopf neu, um ein Überlappen zu vermeiden
/* Ändern Sie die Schriftgröße der Links und positionieren Sie den Schließknopf neu, wenn die Bildschirmhöhe weniger als 450 Pixel beträgt, damit sie sich nicht überlappen */
@media screen and (max-height: 450px) {
  .overlay a {font-size: 20px}
  .overlay .closebtn {
    font-size: 40px;
    top: 15px;
    right: 35px;
  }
}

Dritter Schritt - JavaScript hinzufügen:

Im folgenden Beispiel wird das Overlays-Navigationsmenü, wenn es ausgelöst wird, von links nach rechts (von 0 bis 100% Breite) herein geschoben:

von der Seite herein gleiten

/* Öffnen, wenn jemand auf das span-Element klickt */
function openNav() {
  document.getElementById("myNav").style.width = "100%";
}
/* Schließen, wenn jemand auf das "x"-Symbol im Überlagerungsbedeckungsfenster klickt */
function closeNav() {
  document.getElementById("myNav").style.width = "0%";
}

Try it yourself

Im folgenden Beispiel bewegt sich die Overlays-Navigationsleiste von oben nach unten (von 0 bis 100% Höhe).

Hinweis:Bitte beachten Sie in diesem Beispiel, dass das CSS etwas anders ist als im obigen Beispiel (Standardhöhe ist jetzt 0, Breite 100%, overflow-y ist hidden (vertikale Scrollbar deaktiviert, außer auf kleinen Bildschirmen)):

von oben nach unten scrollen

/* 打开 */
function openNav() {
  document.getElementById("myNav").style.height = "100%";
}
/* 关闭 */
function closeNav() {
  document.getElementById("myNav").style.height = "0%";
}

Try it yourself

In diesem Beispiel wird keine Animation verwendet, um das Navigationsmenü zu öffnen:

ohne Animation Menü öffnen

/* 打开 */
function openNav() {
  document.getElementById("myNav").style.display = "block";
}
/* 关闭 */
function closeNav() {
  document.getElementById("myNav").style.display = "none";
}

Try it yourself

Related pages

Tutorial:CSS navigation bar