通过icon图标实现汉堡菜单的侧边导航栏

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Responsive Fullscreen Navigation Bar</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
  <style>
    /* 基本样式 */
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: Arial, sans-serif;
    }

    /* 导航栏样式 */
    .navbar {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: #333;
      color: #fff;
      display: flex;
      flex-direction: column;
      justify-content: flex-start;
      align-items: center;
      z-index: 9999;
      transform: translateX(-100%);
      transition: transform 0.3s ease-in;
    }

    .navbar.show {
      transform: translateX(0%);
    }

    .navbar .close-btn {
      position: absolute;
      top: 10px;
      right: 10px;
      font-size: 18px;
      cursor: pointer;
    }

    .navbar ul {
      list-style: none;
      margin-top: 30px;
    }

    .navbar ul li {
      margin-bottom: 20px;
    }

    .navbar ul li a {
      color: #fff;
      text-decoration: none;
      font-size: 16px;
      text-transform: uppercase;
      transition: color 0.3s ease-in;
      display: flex;
      align-items: center;
    }

    .navbar ul li a:hover {
      color: #ff0000;
    }

    /* 导航栏图标样式 */
    .navbar ul li i {
      margin-right: 10px;
    }
    
    /* 汉堡菜单样式 */
    .hamburger-menu {
      position: absolute;
      top: 20px;
      left: 20px;
      font-size: 30px;
      cursor: pointer;
      z-index: 9999;
      display: none;
    }

    /* 媒体查询 */
    @media only screen and (max-width: 768px) {
      .navbar ul li {
        margin-bottom: 10px;
      }

      .navbar ul li a {
        font-size: 14px;
      }

      .hamburger-menu {
        display: block;
      }
    }
  </style>
</head>
<body>
  <!-- 导航栏 -->
  <nav class="navbar" id="navbar">
    <div class="close-btn" id="close-btn">
      <i class="fas fa-times"></i>
    </div>
    <ul>
      <li><i class="fas fa-home"></i>首页</li>
      <li><i class="fas fa-user"></i>关于</li>
      <li><i class="fas fa-cog"></i>设置</li>
      <li><i class="fas fa-envelope"></i>联系</li>
      
    </ul>
  </nav>

  <!-- 汉堡菜单 -->
  <div class="hamburger-menu" id="hamburger-menu">
    <i class="fas fa-bars"></i>
  </div>

  <script>
    const navbar = document.getElementById('navbar');
    const hamburgerMenu = document.getElementById('hamburger-menu');
    const closeBtn = document.getElementById('close-btn');

    hamburgerMenu.addEventListener('click', () => {
      navbar.classList.add('show');
    });

    closeBtn.addEventListener('click', () => {
      navbar.classList.remove('show');
    });
  </script>
</body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值