如何使用 CSS 和 JavaScript 為智慧手機/平板電腦建立頂部導航選單?


在本文中,我們將討論如何使用 CSS 和 JavaScript 為智慧手機和平板電腦建立頂部導航選單。

導航欄通常是訪問網站的使用者的第一站,他們需要透過導航欄來瀏覽網站。它包含網站中存在的元素列表;包括用於在網站中導航的連結。

為智慧手機和平板電腦建立頂部導航選單很容易,首先我們必須建立一個導航選單頁面並在 CSS 中新增媒體查詢。如果裝置寬度小於 600px,則新增以下 CSS 屬性。

@media screen and (max-width: 600px) {
   .navbar a:not(:first-child) {display: none;}
   .navbar a.icon {
     float: right;
     display: block;
   }
}
@media screen and (max-width: 600px) {
   .navbar.responsive .icon {
     position: absolute;
     right: 0;
     top: 0;
   }
   .navbar.responsive a {
     float: none;
     display: block;
     text-align: left;
   }
}

示例

以下是建立智慧手機頂部導航選單需要遵循的步驟。在本例中,我們正在建立一個顯示“頂部導航選單”的網頁。一個包含 4 個連結的選單,點選後才會顯示。

Example.html

建立一個 HTML 檔案,在其中我們將定義頁面的結構(檢視)。在本例中,我們使用 HTML 程式碼建立當前頁面,其中包含所需的文字、一個下拉選單和選單的空導航連結。

<body>
   <div class="navbar" id="myNavbar">
      <a href="#home" class="active">Home</a>
      <a href="#tutorials">Tutorials</a>
      <a href="#contact">Contact</a>
      <a href="#about">About</a>
      <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">☰</a>
   </div>

   <div style="padding-top:200px; text-align: center;">
      <h2>Menu for Smart-phone, ipad, and mobiles</h2>
      <p>Resize the browser window to see how it works on mobiles phone.</p>
   </div>

Example.css

新增CSS 樣式以在頂部導航選單上提供背景和懸停效果,以獲得更好的外觀。在本例中,我們正在設定頂部導航選單的樣式,如果我們懸停在連結上,背景顏色將會改變。

<style>
   body {
      margin: 0;
      font-family: Arial, Helvetica, sans-serif;
   }
   
   .navbar {
      overflow: hidden;
      background-color: rgba(43, 194, 106, 0.8);
      position: fixed;
      top: 0;
      width: 100%;
   }
   
   .navbar a {
      float: left;
      display: block;
      color: #ffffff;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
      font-size: 17px;
   }
   
   .navbar a:hover {
      background-color: rgb(214, 39, 39);
      color: black;
   }
   
   .navbar a.active {
      background-color: #045faa;
      color: white;
   }
   
   .navbar .icon {
      display: none;
   }
   
   @media screen and (max-width: 600px) {
      .navbar a:not(:first-child) {
         display: none;
      }
      .navbar a.icon {
         float: right;
         display: block;
      }
   }
   
   @media screen and (max-width: 600px) {
      .navbar.responsive .icon {
         position: absolute;
         right: 0;
         top: 0;
      }
      .navbar.responsive a {
         float: none;
         display: block;
         text-align: left;
      }
   }
</style>

Example.js

使用JavaScript,我們可以執行驗證並在頁面上處理事件。在本例中,我們正在為“首頁”使用 active 類,並新增響應式類以實現響應式設計。

<script>
   function myFunction() {
      var x = document.getElementById("myNavbar");
      if (x.className === "navbar") {
         x.className += " responsive";
      } else {
         x.className = "navbar";
      }
   }
</script>

完整示例

以下是建立手機選單欄/導航欄的完整示例。

<!DOCTYPE html>
<html>
<head>
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <style>
   body {
      margin: 0;
      font-family: Arial, Helvetica, sans-serif;
   }

   .navbar {
      overflow: hidden;
      background-color: rgba(43, 194, 106, 0.8);
      position: fixed;
      top: 0;
      width: 100%;
   }

   .navbar a {
      float: left;
      display: block;
      color: #ffffff;
      text-align: center;
      padding: 14px 16px;
      text-decoration: none;
      font-size: 17px;
   }

   .navbar a:hover {
      background-color: rgb(214, 39, 39);
      color: black;
   }

   .navbar a.active {
      background-color: #045faa;
      color: white;
   }

   .navbar .icon {
      display: none;
   }

   @media screen and (max-width: 600px) {
      .navbar a:not(:first-child) {
         display: none;
      }
      .navbar a.icon {
         float: right;
         display: block;
      }
   }

   @media screen and (max-width: 600px) {
      .navbar.responsive .icon {
         position: absolute;
         right: 0;
         top: 0;
      }
      .navbar.responsive a {
         float: none;
         display: block;
         text-align: left;
      }
   }
   </style>
</head>
<body>
   <div class="navbar" id="myNavbar">
      <a href="#home" class="active">Home</a>
      <a href="#tutorials">Tutorials</a>
      <a href="#contact">Contact</a>
      <a href="#about">About</a>
      <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">☰</a>
   </div>
   <div style="padding-top:200px; text-align: center;">
      <h2>Menu for Smart-phone, ipad, and mobiles</h2>
      <p>Resize the browser window to see how it works on mobiles phone.</p>
   </div>
   <script>
   function myFunction() {
      var x = document.getElementById("myNavbar");
      if (x.className === "navbar") {
         x.className += " responsive";
      } else {
         x.className = "navbar";
      }
   }
   </script>
</body>
</html>

更新於: 2022-12-19

瀏覽量 582 次

開啟你的職業生涯

透過完成課程獲得認證

立即開始
廣告