单击汉堡菜单按钮时如何显示网格菜单项?

时间:2019-04-05 08:58:30

标签: javascript html css

我有一个使用网格完成的菜单,它分为3列。首先,我有我的徽标,其次,我有菜单项,最后是按钮位置。我有媒体查询来隐藏菜单项并显示汉堡图标。问题是,如何使菜单项在单击图标时跌至下方?

代码如下:

let menus = document.getElementsByClassName('hammburger-menu');

[].forEach.call(menus, function(m) {
    m.addEventListener('click', function() {
        m.classList.toggle('open');
    })
});
body .navbar {
  display: -ms-grid;
  display: grid;
  -ms-grid-rows: 50px;
      grid-template-rows: 50px;
  -ms-grid-columns: 1fr 1fr 1fr;
      grid-template-columns: 1fr 1fr 1fr;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  text-align: center;
  padding: 10px;
}

body .navbar .brand {
  width: 100%;
  text-align: end;
}

body .navbar .hammburger-menu {
  width: 40px;
  height: 35px;
  border: 0;
  display: none;
  margin: 0 auto;
  cursor: pointer;
}

@media (max-width: 890px) {
  body .navbar .hammburger-menu {
    display: inline;
  }
}

body .navbar .hammburger-menu div {
  background-color: #fea100;
  display: block;
  width: 40px;
  height: 4px;
  margin: 5px auto;
  -webkit-transition: all 0.2s ease-out;
  transition: all 0.2s ease-out;
}

body .navbar .hammburger-menu.open .one {
  -webkit-transform: translateY(9px) rotate(45deg);
          transform: translateY(9px) rotate(45deg);
}

body .navbar .hammburger-menu.open .two {
  opacity: 0;
}

body .navbar .hammburger-menu.open .three {
  -webkit-transform: translateY(-9px) rotate(-45deg);
          transform: translateY(-9px) rotate(-45deg);
}

body .navbar .navbar-nav {
  text-align: center;
}

body .navbar .navbar-nav input {
  position: relative;
  top: 20%;
}

body .navbar .navbar-nav a {
  color: #000000 !important;
  font-family: "Sintony";
  font-weight: 700;
  size: 14px;
  text-decoration: none;
  padding: 10px;
}

@media (max-width: 890px) {
  body .navbar .navbar-nav a {
    display: none;
  }
}

body .navbar .button {
  width: 200px;
  height: 47px;
  background-color: #fea100;
  border: none;
  border-radius: 7px;
  text-align: center;
}

body .navbar .button a {
  border: 1px solid #ffffff;
  color: #fff;
  border-radius: 7px;
  text-decoration: none !important;
  padding: 10px;
  position: relative;
  top: 26%;
}

body .navbar .button img {
  padding-right: 2px;
}
<nav class="navbar">
        <a class="brand" href="#"><img src="https://via.placeholder.com/50

C/O https://placeholder.com/" alt=""></a>
        <div class="navbar-nav slider-down">
            <div for="nav-toggle" class="hammburger-menu">
                <div class="one"></div>
                <div class="two"></div>
                <div class="three"></div>
            </div>
            <a class="nav-item nav-link " href="#">Home</a>
            <a class="nav-item nav-link" href="#">Menu</a>
            <a class="nav-item nav-link" href="#">Reservation</a>
            <a class="nav-item nav-link" href="#">Recipe</a>
            <a class="nav-item nav-link" href="#">Blog</a>
            <a class="nav-item nav-link" href="#">Pages</a>
            <a class="nav-item nav-link" href="#">Contact</a>
        </div>
        <div class="button">
            <a href=""><img src="images/icon_plate_navbar_button.png">BOOK YOUR TABLE</a>
        </div>
    </nav>

3 个答案:

答案 0 :(得分:1)

首先,将链接分组为一个div

<div class="hammburger-links">
    <a class="nav-item nav-link " href="#">Home</a>
    ...
    <a class="nav-item nav-link" href="#">Contact</a>
</div>

然后,当您应用display: none时,页面上将不存在任何内容。因此,相反,我们应用visibility: hidden以使其重新出现。

@media (max-width: 890px) {
  .navbar-nav .hammburger-links {
      visibility: hidden;
      position: absolute;
  }

  .navbar-nav .hammburger-links a
  {
    display: block; /* make the links block element */
    border: 1px solid black;
  }

  .navbar-nav.open .hammburger-links
  {
    visibility: visible; /* when the nav is opened, we make the links visible */
  }
}

最后,我们没有将.open应用于汉堡包,而是将其应用于.navbar-nav

[].forEach.call(menus, function(m) {
  m.addEventListener('click', function() {
    m.parentElement.classList.toggle('open');
  });
});

Jsfiddle:https://jsfiddle.net/re9nu31k/

P.S。样式是您的工作;)
P.S.2我认为您不需要在每个CSS选择器中都包含主体

答案 1 :(得分:0)

在CSS中您提到显示:没有,这就是为什么不显示

@media (max-width: 890px)
body .navbar .navbar-nav a {
    display: none;
}

答案 2 :(得分:-1)

我没有碰到您已经创建的代码,只是添加了一些内容:

HTML:

<nav class="navbar">
        <a class="brand" href="#"><img src="https://via.placeholder.com/50

C/O https://placeholder.com/" alt=""></a>
        <div class="navbar-nav slider-down">
            <div for="nav-toggle" class="hammburger-menu">
                <div class="one item" style="display: none;"></div>
                <div class="two item" style="display: none;"></div>
                <div class="three item" style="display: none;"></div>
            </div>
            <a class="nav-item nav-link " href="#">Home</a>
            <a class="nav-item nav-link" href="#">Menu</a>
            <a class="nav-item nav-link" href="#">Reservation</a>
            <a class="nav-item nav-link" href="#">Recipe</a>
            <a class="nav-item nav-link" href="#">Blog</a>
            <a class="nav-item nav-link" href="#">Pages</a>
            <a class="nav-item nav-link" href="#">Contact</a>
        </div>
        <div class="button">
            <a href="#"><img src="images/icon_plate_navbar_button.png">BOOK YOUR TABLE</a>
        </div>
    </nav>

CSS:

body .navbar {
  display: -ms-grid;
  display: grid;
  -ms-grid-rows: 50px;
      grid-template-rows: 50px;
  -ms-grid-columns: 1fr 1fr 1fr;
      grid-template-columns: 1fr 1fr 1fr;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  text-align: center;
  padding: 10px;
}

body .navbar .brand {
  width: 100%;
  text-align: end;
}

body .navbar .hammburger-menu {
  width: 40px;
  height: 35px;
  border: 0;
  display: none;
  margin: 0 auto;
  cursor: pointer;
}

@media (max-width: 890px) {
  body .navbar .hammburger-menu {
    display: inline;
  }
}

body .navbar .hammburger-menu div {
  background-color: #fea100;
  display: block;
  width: 40px;
  height: 4px;
  margin: 5px auto;
  -webkit-transition: all 0.2s ease-out;
  transition: all 0.2s ease-out;
}

body .navbar .hammburger-menu.open .one {
  -webkit-transform: translateY(9px) rotate(45deg);
          transform: translateY(9px) rotate(45deg);
}

body .navbar .hammburger-menu.open .two {
  opacity: 0;
}

body .navbar .hammburger-menu.open .three {
  -webkit-transform: translateY(-9px) rotate(-45deg);
          transform: translateY(-9px) rotate(-45deg);
}

body .navbar .navbar-nav {
  text-align: center;
}

body .navbar .navbar-nav input {
  position: relative;
  top: 20%;
}

body .navbar .navbar-nav a {
  color: #000000 !important;
  font-family: "Sintony";
  font-weight: 700;
  size: 14px;
  text-decoration: none;
  padding: 10px;
}

@media (max-width: 890px) {
  body .navbar .navbar-nav a {
    display: none;
  }
}

body .navbar .button {
  width: 200px;
  height: 47px;
  background-color: #fea100;
  border: none;
  border-radius: 7px;
  text-align: center;
}

body .navbar .button a {
  border: 1px solid #ffffff;
  color: #fff;
  border-radius: 7px;
  text-decoration: none !important;
  padding: 10px;
  position: relative;
  top: 26%;
}

body .navbar .button img {
  padding-right: 2px;
}

.open {
  display: block !important;
}

JAVASCRIPT:

const menus = document.querySelectorAll('.hammburger-menu .item ');
const btn = document.querySelector('.button');

console.log(menus)
menus.forEach(function(elem) {
console.log(elem);
    btn.addEventListener('click', function() {
        elem.classList.toggle('open');
    })
});

最好是这样