使用 HTML、CSS 和 JavaScript 创建可悬停的侧边导航

htmljavascriptweb developmentfront end technology

导航栏是网页的一部分,其中包含指向网站中相应部分/页面的链接,可帮助用户快速轻松地浏览网站。导航栏可以通过多种方式实现,但传统的实现方式是水平和垂直栏。

在本文中,我们将使用 HTML、CSS 和 JavaScript 设计垂直侧边导航栏。

创建可悬停的侧边导航

以下是使用 HTML、CSS 和 JavaScript 设计可悬停侧边导航按钮的步骤 -

  • 步骤 1 - 添加以下 HTML 代码。

<body>
   <div class="sidenav">
      <a href="#" id="home" onclick="showContent('homeContent')">Home</a>
      <a href="#" id="codingground" onclick="showContent('codingGroundContent')">Coding Ground</a>
      <a href="#" id="jobs" onclick="showContent('jobsContent')">Jobs</a>
      <a href="#" id="library" onclick="showContent('libraryContent')">Library</a>
      <a href="#" id="articles" onclick="showContent('articlesContent')">Articles</a>
      <a href="#" id="corporatetraining" onclick="showContent('corporateTrainingContent')">Corporate Training</a>
   </div>
   <div id="homeContent" style="display: none;">
      <h2>Home Content</h2>
      <p>This is the content for the Home page.</p>
   </div>
   <div id="codingGroundContent" style="display: none;">
      <h2>Coding Ground Content</h2>
      <p>This is the content for the Coding Ground page.</p>
   </div>
   <div id="jobsContent" style="display: none;">
      <h2>Jobs Content</h2>
      <p>This is the content for the Jobs page.</p>
   </div>
   <div id="libraryContent" style="display: none;">
      <h2>Library Content</h2>
      <p>This is the content for the Library page.</p>
   </div>
   <div id="articlesContent" style="display: none;">
      <h2>Articles Content</h2>
      <p>This is the content for the Articles page.</p>
   </div>
   <div id="corporateTrainingContent" style="display: none;">
      <h2>Corporate Training Content</h2>
      <p>This is the content for the Corporate Training page.</p>
   </div>
</body>
  • 第 2 步 - 添加以下 CSS 代码。

对于悬停效果,我们使用了 CSS :hover 选择器。 每当我们将鼠标悬停在元素上时,:hover 选择器就会选择它。

<style>
   .sidenav a {
      position: absolute;
      left: -80px;
      transition: 0.1s;
      padding: 14px;
      width: 100px;
      text-decoration: none;
      font-size: 20px;
      color: white;
      border-radius: 0px 25px 25px 0px;
   }
   .sidenav a:hover {
      left: 0;
   }
   #home {
      top: 20px;
      background-color: #003300;
   }
   #codingground {
      top: 80px;
      background-color: #004a00;
   }
   #jobs {
      top: 165px;
      background-color: #006100;
   }
   #library {
      top: 225px;
      background-color: #007800;
   }
   #articles {
      top: 285px;
      background-color: #008f00;
   }
   #corporatetraining {
      top: 345px;
      background-color: #00ad00;
   }
   #homeContent, #codingGroundContent, #jobsContent, #libraryContent, #articlesContent, #corporateTrainingContent{
      margin: auto;
      display: flex;
      width: 60%;
      text-align: center;
      display: none;
   }
</style>
  • 步骤 3 − 包含以下脚本

<script>
   function showContent(contentId) {
      var content = document.getElementById(contentId);
    content.style.display = "block";
   }
</script>

完整示例

现在,让我们将上述所有 HTML、CSS 和 JavaScript 代码块组合起来 -

<!DOCTYPE html>
<html>
<head>
   <title>Hoverable Side Navigation with HTML, CSS and JavaScript</title>
   <link rel="stylesheet" type="text/css" href="style.css">
   <style>
      .sidenav a {
         position: absolute;
         left: -80px;
         transition: 0.1s;
         padding: 14px;
         width: 100px;
         text-decoration: none;
         font-size: 20px;
         color: white;
         border-radius: 0px 25px 25px 0px;
      }
      .sidenav a:hover {
         left: 0;
      }
      #home {
         top: 20px;
         background-color: #003300;
      }
      #codingground {
         top: 80px;
         background-color: #004a00;
      }
      #jobs {
         top: 165px;
         background-color: #006100;
      }
      #library {
         top: 225px;
         background-color: #007800;
      }
      #articles {
         top: 285px;
         background-color: #008f00;
      }
      #corporatetraining {
         top: 345px;
         background-color: #00ad00;
      }
      #homeContent,
      #codingGroundContent,
      #jobsContent,
      #libraryContent,
      #articlesContent,
      #corporateTrainingContent {
         margin: auto;
         display: flex;
         width: 60%;
         text-align: center;
         display: none;
      }
   </style>
   <script>
      function showContent(contentId) {
         var content = document.getElementById(contentId);
         content.style.display = "block";
      }
   </script>
</head>
<body>
   <div class="sidenav">
      <a href="#" id="home" onclick="showContent('homeContent')">Home</a>
      <a href="#" id="codingground" onclick="showContent('codingGroundContent')">Coding Ground</a>
      <a href="#" id="jobs" onclick="showContent('jobsContent')">Jobs</a>
      <a href="#" id="library" onclick="showContent('libraryContent')">Library</a>
      <a href="#" id="articles" onclick="showContent('articlesContent')">Articles</a>
      <a href="#" id="corporatetraining" onclick="showContent('corporateTrainingContent')">Corporate Training</a>
   </div>
   <div id="homeContent">
      <h2>Home Content</h2>
      <p>This is the content for the Home page.</p>
   </div>
   <div id="codingGroundContent">
      <h2>Coding Ground Content</h2>
      <p>This is the content for the Coding Ground page.</p>
   </div>
   <div id="jobsContent">
      <h2>Jobs Content</h2>
      <p>This is the content for the Jobs page.</p>
   </div>
   <div id="libraryContent">
      <h2>Library Content</h2>
      <p>This is the content for the Library page.</p>
   </div>
   <div id="articlesContent">
      <h2>Articles Content</h2>
      <p>This is the content for the Articles page.</p>
   </div>
   <div id="corporateTrainingContent">
      <h2>Corporate Training Content</h2>
      <p>This is the content for the Corporate Training page.</p>
   </div>
</body>
</html>

执行上述程序后,我们可以看到屏幕左上角有六个按钮。如果我们将鼠标悬停在任何按钮上,它都会显示过渡。如果我们尝试单击它,它将在屏幕上显示相关内容。


相关文章