如何使用 CSS 和 JavaScript 建立可摺疊側邊欄選單?


可摺疊面板是一個基本的容器教練檢視,它建立一個可以包含其他控制元件的區域。此區域可以是冗長的,也可以摺疊以顯示或隱藏資料。

要建立可摺疊側邊欄選單,我們需要HTMLCSSjavascript。可摺疊側邊欄將與主頁面一起摺疊。簡單來說,這意味著選單欄及其相應寬度顯示在頁面上;頁面的主要內容採用最小邊距在頁面上顯示。

以下是建立可摺疊側邊欄選單的步驟。在這個例子中,我們正在建立一個顯示“可摺疊側邊欄選單”的網頁。點選後會顯示一個包含 4 個連結的選單。

Example.html

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

<body>
   <div id="mySidebar" class="sidebar">
      <a href="javascript:void(0)" class="closebtn" onclick="hide()">×</a>
      <a href="#">Tutorials</a>
      <a href="#">AboutUs</a>
      <a href="#">Career</a>
      <a href="#">ContactUs</a>
   </div>
   <div id="content">
      <button class="openbtn" id="openbtn" onclick="show()">☰</button>
      <h2>Collapsed Sideber</h2>
      <p>
         Click on the menu/bar icon to open the sidebar, and it will automatically push this content to the right.
      </p>
      <p>
         Lorem ipsum dolor sit amet consectetur adipisicing elit. Repellendus velit fuga accusamus quo placeat cumque iusto. Molestias soluta numquam, dolore debitis consequuntur, magni expedita repellendus amet, eaque exercitationem adipisci. Enim.
      </p>
   </div>

Example.css

新增CSS樣式以在可摺疊側邊欄選單上提供懸停效果,使其看起來更好。在這個例子中,我們正在設定可摺疊側邊欄的樣式,如果懸停在連結上,背景顏色將發生改變。

<style>
   body {
      font-family: "Lato", sans-serif;
   }
   
   .sidebar {
      height: 100%;
      width: 0;
      position: fixed;
      z-index: 1;
      top: 0;
      left: 0;
      background-color: #111;
      overflow-x: hidden;
      transition: 0.5s;
      padding-top: 60px;
   }
   
   .sidebar a {
      padding: 8px 8px 8px 32px;
      text-decoration: none;
      font-size: 25px;
      color: #818181;
      display: block;
      transition: 0.3s;
   }
   
   .sidebar a:hover {
      color: #f1f1f1;
   }
   
   .sidebar .closebtn {
      position: absolute;
      top: 0;
      right: 25px;
      font-size: 36px;
      margin-left: 50px;
   }
   
   .openbtn {
      font-size: 20px;
      cursor: pointer;
      color: black;
      padding: 10px 15px;
      border: none;
   }
   
   .openbtn:hover {
      background-color: #444;
   }
   
   #content {
      transition: margin-left 0.5s;
      padding: 16px;
   }
   
   /* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */
   
   @media screen and (max-height: 450px) {
      .sidebar {
         padding-top: 15px;
      }
      .sidebar a {
         font-size: 18px;
      }
   }
</style>

Example.js

使用 Javascript,我們可以執行驗證並在頁面上處理事件。在這個例子中,建立顯示和隱藏按鈕。導航將透過點選選單欄按鈕開啟,並透過點選關閉按鈕關閉。

讓我們看看 javascript 程式碼以便更好地理解:

<script>
   function show() {
      document.getElementById("mySidebar").style.width = "250px";
      document.getElementById("content").style.marginLeft = "250px";
   }
   function hide() {
      document.getElementById("mySidebar").style.width = "0";
      document.getElementById("content").style.marginLeft = "0";
   }
</script>

完整示例

讓我們看一個完整的示例,說明如何使用CSSHTMLJavascript摺疊側邊欄。

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

   .sidebar {
      height: 100%;
      width: 0;
      position: fixed;
      z-index: 1;
      top: 0;
      left: 0;
      background-color: #111;
      overflow-x: hidden;
      transition: 0.5s;
      padding-top: 60px;
   }

   .sidebar a {
      padding: 8px 8px 8px 32px;
      text-decoration: none;
      font-size: 25px;
      color: #818181;
      display: block;
      transition: 0.3s;
   }

   .sidebar a:hover {
      color: #f1f1f1;
   }

   .sidebar .closebtn {
      position: absolute;
      top: 0;
      right: 25px;
      font-size: 36px;
      margin-left: 50px;
   }

   .openbtn {
      font-size: 20px;
      cursor: pointer;
      color: black;
      padding: 10px 15px;
      border: none;
   }

   .openbtn:hover {
      background-color: #444;
   }

   #content {
      transition: margin-left 0.5s;
      padding: 16px;
   }

   /* On smaller screens, where height is less than 450px, change the style of the sidenav (less padding and a smaller font size) */

   @media screen and (max-height: 450px) {
      .sidebar {
         padding-top: 15px;
      }
      .sidebar a {
         font-size: 18px;
      }
   }
   </style>
</head>
<body>
   <div id="mySidebar" class="sidebar">
      <a href="javascript:void(0)" class="closebtn" onclick="hide()">×</a>
      <a href="#">Tutorials</a>
      <a href="#">AboutUs</a>
      <a href="#">Career</a>
      <a href="#">ContactUs</a>
   </div>
   <div id="content">
      <button class="openbtn" id="openbtn" onclick="show()">☰</button>
      <h2>Collapsed Sideber</h2>
      <p>
         Click on the menu/bar icon to open the sidebar, and it will automatically push this content to the right.
      </p>
      <p>
         Lorem ipsum dolor sit amet consectetur adipisicing elit. Repellendus velit fuga accusamus quo placeat cumque iusto. Molestias soluta numquam, dolore debitis consequuntur, magni expedita repellendus amet, eaque exercitationem adipisci. Enim.
      </p>
   </div>
   <script>
   function show() {
      document.getElementById("mySidebar").style.width = "250px";
      document.getElementById("content").style.marginLeft = "250px";
   }

   function hide() {
      document.getElementById("mySidebar").style.width = "0";
      document.getElementById("content").style.marginLeft = "0";
   }
   </script>
</body>
</html>

更新於:2022年12月19日

7K+ 瀏覽量

啟動你的職業生涯

透過完成課程獲得認證

開始學習
廣告