单击时汉堡在响应菜单中移动

时间:2020-07-15 14:59:53

标签: html css menu responsive hamburger-menu

当我的站点移到一个较小的标志时,菜单分为两个部分:菜单其余部分的“主页”按钮和“汉堡”按钮。一切正常,但是当我单击菜单时,它移动了一点,但是很烦人。我搜索并找到了一个具有相同问题的帖子,但集结完全不同,不适合我的需求。 我希望有人能提供帮助。

<!DOCTYPE html>
<html lang="en">

    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-t0-fit=no">

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
      <link href="https://fonts.googleapis.com/css2?family=Playfair+Display:wght@900&display=swap" rel="stylesheet">
      <link rel="stylesheet" href="title.css">
      <link rel="stylesheet" href="menu.css">
      <title></title>
    </head>

      <!-- title of the website with link -->
      <a href="error404.html">
      <div class="title">B</div>
      </a>
      <!-- vertical navigation menu -->
      <body>

      <div class="topnav" id="myTopnav">
              <a href="#home" class="active">Home</a>
              <a href="error404.html">News</a>
              <a href="error404.html">Shop</a>
              <a href="error404.html">Contact</a>
              <a href="#about">About</a>
              <a href="javascript:void(0);" class="icon" onclick="myFunction()">
                  <i class="fa fa-bars"></i>
              </a>
      </div>

    <br>
    <br>
    &nbsp;

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

      </body>

</html>

menu.css

.topnav {
  width: 100%;
  overflow: auto;
}

.topnav a {
  margin-top: 5px;
  text-align: center;
  text-decoration: none;
  float: left;
  position: relative;
  border: 0;
  padding: 14px 16px;
  font-size: 17px;
  font-family: "Century Gothic";
  color: black;
  width: 20%;
}

.topnav a:hover {
  background-color: black;
  color: white;
}

.topnav a.active {
  background-color: red;
  color: black;
}

.topnav .icon {
  display: none;
}

.icon {
  margin: 1em;
  width: 3px;
}

.icon:after,
.icon:before,
.icon div {
  background-color: red;
  border-radius: 3px;
  content: '';
  display: block;
  height: 5px;
  margin: 7px 0;
  transition: all .2s ease-in-out;
}

.icon:hover:before {
  transform: translateY(12px) rotate(135deg);
}

.icon:hover:after {
  transform: translateY(-12px) rotate(-135deg);
}

.icon:hover div {
  transform: scale(0);
}



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


@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive a.icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;

  }
}

0 个答案:

没有答案