如何使用 CSS 和 JavaScript 建立響應式底部導航選單?


在本文中,我們將討論如何使用 CSS 和 JavaScript 建立響應式底部導航選單。

通常,建立網頁後,當更改頁面大小時,頁面的一些內容將被隱藏。響應式頁面是指在更改頁面尺寸時調整頁面內容的頁面。

要建立響應式導航選單,我們必須使用 CSS 和 JavaScript。響應式選單調整瀏覽器選單,以檢視導航選單在小型和移動裝置顯示屏上的工作方式。

簡而言之,在響應式頁面中,行為會在不同的裝置和螢幕寬度上發生改變。

讓我們看一個響應式按鈕導航的示例。

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-left:16px">
      <h2>Responsive Bottom Navigation </h2>
      <p>Resize the browser window to see how it works.</p>
   </div>

Example.css

新增css 樣式以在底部導航選單欄上提供背景和懸停效果,使其更美觀。在本例中,我們正在設定底部導航選單的樣式,如果我們更改頁面的尺寸,則選單將以垂直方向顯示。

<style>
   body {
      margin: 0;
      font-family: Arial, Helvetica, sans-serif;
   }
   
   .navbar {
      overflow: hidden;
      background-color: rgb(124, 121, 121);
      position: fixed;
      bottom: 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;
         bottom: 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: rgb(124, 121, 121);
      position: fixed;
      bottom: 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;
         bottom: 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-left:16px">
      <h2>Responsive Bottom Navigation </h2>
      <p>Resize the browser window to see how it works.</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日

1K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.