单击条形图标时显示标签(响应式导航栏)

时间:2018-07-03 12:44:57

标签: html css

我创建了一个导航栏,所有选项卡都是内联的。减小窗口大小时,会出现条形图标,并且选项卡将垂直显示,而不是水平显示。我需要输出,以便在减小窗口大小时出现条形图标,但选项卡消失,并且仅在单击条形图标时才能(垂直)看到和看不见。 我尝试了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>

2 个答案:

答案 0 :(得分:5)

添加此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>

答案 1 :(得分:1)

Your code should be like this:

<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel='stylesheet' href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
     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;
      }

      nav ul{
        display:none;
      }

      ul li{
        display:block;
      }
    }
</style>

</head>

<body>
    <div class="toggle">
      <i class="fa fa-bars menu" id="menu"></i>
    </div>

    <nav>
      <ul class="ul">
        <li><a href="#">Home</a></li>
        <li><a href="#" >About</a></li>
        <li><a href="#">Makeup</a></li>
        <li><a href="#">Hairstyle</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </nav>

</body>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
    $(".menu").on("click", function() {
      $("nav .ul").toggle();
    })
</script>

</html>