我创建了一个导航栏,所有选项卡都是内联的。减小窗口大小时,会出现一个条形图标,并且选项卡将垂直显示,而不是水平显示。我需要输出,以便在减小窗口大小时出现条形图标,但选项卡消失,并且仅在单击条形图标时才能(垂直)看到和看不见。
我尝试了jquery,但是没有用。我不确定该怎么做,您能帮我吗?



body {
  margin: 0;
  padding: 0;
  font-family: Sans-Serif;
}

nav {
  margin-left: 45px;
  ;
  width: 90%;
  background: #D1D0D3;
}

ul {
  list-style-type: none;
  width: 80%;
  margin: 0 auto;
  color: black;
  padding: 0;
}

ul a {
  color: black;
  text-decoration: none;
}

ul li {
  display: inline-block;
  padding: 20px;
}

ul li:hover {
  background-color: yellow;
}

.toggle {
  width: 100%;
  padding: 10px 20px 0px 20px;
  background-color: #C2ABF1;
  text-align: right;
  box-sizing: border-box;
  color: white;
  font-size: 30px;
  display: none;
}

@media (max-width: 800px) {
  .toggle {
    display: block;
  }
  .block {
    display: block;
  }
}

<div class="toggle">
  <i class="fa fa-bars menu" id="menu"></i>
</div>
<nav>
  <ul class="ul">
    <li class="block"><a href="#">Home</a></li>
    <li class="block"><a href="#">About</a></li>
    <li class="block"><a href="#">Makeup</a></li>
    <li class="block"><a href="#">Hairstyle</a></li>
    <li class="block"><a href="#">Contact</a></li>
  </ul>
</nav>

最佳答案

添加此jQuery代码

$(".menu").on("click", function() {
  $("nav .ul").toggle(500);
})


而这个CSS在媒体查询中

.ul {
    display: none;
  }
  nav {
      margin-left: 0;
      width: 100%;
  }




$(".menu").on("click", function() {
  $("nav .ul").toggle(500);
})

body {
  margin: 0;
  padding: 0;
  font-family: Sans-Serif;
}

nav {
  margin-left: 45px;
  ;
  width: 90%;
  background: #D1D0D3;
}

ul {
  list-style-type: none;
  width: 80%;
  margin: 0 auto;
  color: black;
  padding: 0;
}

ul a {
  color: black;
  text-decoration: none;
}

ul li {
  display: inline-block;
  padding: 20px;
}

ul li:hover {
  background-color: yellow;
}

.toggle {
  width: 100%;
  padding: 10px 20px 0px 20px;
  background-color: #C2ABF1;
  text-align: right;
  box-sizing: border-box;
  color: white;
  font-size: 30px;
  display: none;
}

@media (max-width: 800px) {
  .toggle {
    display: block;
  }
  .block {
    display: block;
  }
  .ul {
    display: none;
  }
  nav {
    margin-left: 0;
    width: 100%;
  }
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://use.fontawesome.com/eed659c9d4.js"></script>
<div class="toggle">
  <i class="fa fa-bars menu" id="menu"></i>
</div>
<nav>
  <ul class="ul">
    <li class="block"><a href="#">Home</a></li>
    <li class="block"><a href="#">About</a></li>
    <li class="block"><a href="#">Makeup</a></li>
    <li class="block"><a href="#">Hairstyle</a></li>
    <li class="block"><a href="#">Contact</a></li>
  </ul>
</nav>

关于html - 单击条形图标时显示标签(响应式导航栏),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51154981/

10-11 05:28