导航栏在JQuery中切换时消失

时间:2018-01-03 22:03:33

标签: javascript jquery html css

我试图在jQuery中创建一个响应式导航栏菜单,但有一个问题是当你调整窗口大小,单击菜单按钮,然后再次单击它,然后将窗口调整为全屏,导航酒吧会消失。我不知道如何解决这个问题。

这是一个JSFiddle:https://jsfiddle.net/tqu1edez/我也在这里发布了代码。

HTML:

    <!DOCTYPE html>
    <html>
    <head>
    <meta name = "viewport" content = "width=device-width, initial-scale=1.0">
    <link rel = "stylesheet" type ="text/css" href = "nav.css">
    <script 
    src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
    </script>
    </head>
    <body>
    <nav class = "navigationBar">
    <img src = "https://i.imgur.com/nfbKl0W.png" class = "menuIcon">
    <ul class = "linkBar">

      <li><a href = "#">Home</a></li>
      <li><a href = "#">Contact</a></li>
      <li><a href = "#">About</a></li>
      <li><a href = "#">Media</a></li>
      <li><a href = "#">Miscellaneous</a></li>

    </ul>
    </nav>

    <script>

    $('.menuIcon').on('click', function() {
      $('nav ul').fadeToggle('slow');
    });

    </script>
    </body>
    </html>

CSS:

    @import url('https://fonts.googleapis.com/css?family=Asap');

    body
    {
  background-color: #598392;
  margin: 0;
  padding: 0;
  font-family: 'Asap', sans-serif;
}

.menuIcon
{
  display:none;
}

.navigationBar
{
  background-color: #124559;
  width:100%;
  overflow:hidden;
}
.navigationBar li
{
  padding:20px;
  display: inline;
  list-style-type:none;

}

a
{
  color: #EFF6E0;
  text-decoration: none;
}
a:hover
{
  color: #AEC3B0;
}
nav ul
{

  padding: 20px;
  margin: 0;
}

.show
{
  display:block;
  transition: fadeIn 2s
}
@media only screen and (max-width: 768px)
{
  .menuIcon
  {
    display:block;
  }
  .navigationBar li
  {
    display:block;
    padding: 0px;
    text-align: center;
  }
  nav ul
  {
    display:none;
  }
}

1 个答案:

答案 0 :(得分:0)

只需要强制导航显示屏幕是否足够大

@media only screen and (min-width: 768px)
{
  nav ul
  {
    display: block !important;
  }
}