导航栏中的错误?

时间:2017-04-23 07:40:45

标签: javascript jquery html css debugging

我的菜单出了点问题,我无法弄明白为什么。当我在响应式视图中测试我的代码时,它首先从汉堡包图标切换到水平导航栏时工作正常,但是......如果我点击汉堡包图标首先(一次下拉菜单和第二次关闭它)和然后调整屏幕大小,我的水平导航菜单消失。

这是Codepen中链接的代码。只需转到全视图并尝试这些步骤,看看你的想法!谢谢!!

http://codepen.io/sshine2/pen/VbjGaE

 <!DOCTYPE html>

 <html lang="en">

 <head>
 <meta charset="utf-8">
 <script src="http://www.google.com/jsapi" type="text/javascript"></script>
 <script type="text/javascript">google.load("jquery", "1.3.2");</script>
 <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

 <style>
body {
 font-family: 'Noto Sans', sans-serif;
 margin: 0;
 width: 100%;
 height: 100vh;
 background: #ffffff;
 background-color: black;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
  }

header {
 width: 100%;
 background: #ffffff;
 position: fixed;
 height: 4em;
 line-height: 4em;
 display: inline-block;
 padding-left: 1em;
 border-bottom: .1em solid #dddddd;
  }
h2 {
 font-size: 2.1em;
  }
p {
 font-size: 10em;
 color: white;
 padding-top: 1em;
  }

@media only screen and (min-width: 319px) {

 .menu {
    z-index: 1;
    display: none;
    font-weight: bold;
    font-size: 1.2em;
    width: 100%;
    background: #f1f1f1;
    position: fixed;
    text-align: center;
    margin-top: 3.3em;
    color: black;
  }

  .menu ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
    list-style-image: none;
    border-top: #dddddd 1px solid;
  }   

  .menu li {
    display: block;
    padding: 1em 0 1em 0;
    border-bottom: #dddddd 1px solid;
  }

  .menu li:hover {
    display: block;
    background: #585858;
    padding: 1em 0 1em 0;
      cursor: crosshair;
  }

  .menu ul li a {
    text-decoration: none;
    margin: 0px;
    color: black;
  }

  .menu ul li a:hover {
    color: white;
    text-decoration: none;
  }

  .menu a {
    text-decoration: none;
    color: black;
  }

  .menu a:hover {
    text-decoration: none;
    color: white;
  }

  #nav-icon4 {
    width: 35px;
    height: 25px;
    float: right;
    margin-top: -47px;
    margin-right: 30px;
    -webkit-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
    -webkit-transition: .5s ease-in-out;
    -moz-transition: .5s ease-in-out;
    -o-transition: .5s ease-in-out;
    transition: .5s ease-in-out;
    cursor: cell;
  }

  #nav-icon4 span {
    display: block;
    position: absolute;
    height: 5px;
    width: 100%;
    background: darkred;
    border-radius: 7px;
    opacity: 2;
    left: 0;
    -webkit-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
    -webkit-transition: .25s ease-in-out;
    -moz-transition: .25s ease-in-out;
    -o-transition: .25s ease-in-out;
    transition: .25s ease-in-out;
  }

  #nav-icon4 span:nth-child(1) {
    top: 0px;
    -webkit-transform-origin: left center;
    -moz-transform-origin: left center;
    -o-transform-origin: left center;
    transform-origin: left center;
  }

  #nav-icon4 span:nth-child(2) {
    top: 10px;
    -webkit-transform-origin: left center;
    -moz-transform-origin: left center;
    -o-transform-origin: left center;
    transform-origin: left center;
  }

  #nav-icon4 span:nth-child(3) {
    top: 20px;
     -webkit-transform-origin: left center;
    -moz-transform-origin: left center;
    -o-transform-origin: left center;
    transform-origin: left center;
  }

  #nav-icon4.open span:nth-child(1) {
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    transform: rotate(45deg);
    top: 0;
    left: 6px;
  }

  #nav-icon4.open span:nth-child(2) {
    width: 0%;
    opacity: 0;
  }

  #nav-icon4.open span:nth-child(3) {
    -webkit-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    -o-transform: rotate(-45deg);
    transform: rotate(-45deg);
    top: 25px;
    left: 6px;
  }   
  }


@media only screen and (min-width : 768px) {   
     h2 {
      z-index: 1000000;
      font-size: 1.5em;
      }

        p {
            font-size: 20em;
            color: white;
            padding-top: 1em;
  }

        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
  }

  li {
    float: right;
    margin-left: 20px;
    margin-right: 8px;
    margin-top: -10px;
}

li a {
    display: block;
    text-align: center;
    text-decoration: none;
}


.menu {
    display: block;
    position: absolute;
    right: 0px;
    font-size: .9em;
    width: 100%;
    padding-right: 15px;
    margin-top: 10px;
    padding-top: 10px;
    padding-bottom: 10px;
    background: rgba(255, 255, 255, 0);
    }
.menu ul {
    border-bottom: 0;
    border-top: 0;
    }
.menu li {
    border-bottom: 0;
    border-top: 0;
    }
 .menu li:hover {
    cursor: crosshair;
    padding-top: 1em;
    padding-bottom: .4em;
    padding-right: 0em;
    padding-left: 0em;
   }

.menu ul li a:hover {
  color: white;
}

#nav-icon4 {
    display: none;
    }
}

@media only screen and (min-width : 922px) {
    li {
    margin-left: 55px;
    margin-right: 18px;

    }
.menu {
    padding-right: 1px;
    }
}


@media only screen and (min-width : 1400px) {
    header {
       height: 5em;
      line-height: 5em;
    }
    h2 {
        font-size: 2.6em;    
    }
    li {
    margin-left: 55px;
    margin-right: 30px;
    }
    .menu {
        padding-right: 1px;
        font-size: 1.2em;
    }
}

    </style>

 <title>hamburgers</title>
 </head>

 <body>
     <header>

      <span>Shine Design</span>
      <div id="nav-icon4">
       <span></span>
       <span></span>
       <span></span>
      </div>

     </header>

     <div class="menu">
      <ul>
       <a href="#">
        <li>LINK ONE</li>
       </a>
       <a href="#">
        <li>LINK TWO</li>
       </a>
       <a href="#">
        <li>LINK THREE</li>
       </a>
       <a href="#">
        <li>LINK FOUR</li>
       </a>
       <a href="#">
        <li>LINK FIVE</li>
       </a>
      </ul>
     </div>

   </body>
    <script>
    $(document).ready(function(){
      $('#nav-icon4').click(function(){
        $(this).toggleClass('open');
      });
    });
    </script>
  </html>

4 个答案:

答案 0 :(得分:1)

在.menu中放置一个display : block !important;,函数slideToggle()会在元素.menu中内嵌一个样式display:none,所以当你回到高分辨率时,.menu仍会显示:无

答案 1 :(得分:1)

函数slideToggle()切换显示属性,并将其添加到菜单的元素样式中,这使得它比使用类

更优先

因此要解决此问题,请在媒体屏幕display:block;

中将重要添加到min-width : 768px;
@media only screen and (min-width : 768px) {
   .....
   .menu {
    display: block!important;
     .....
   }
}

See updated codepen

答案 2 :(得分:0)

在标记

之前或之前使用jquery和js
 <script src="http://www.google.com/jsapi" type="text/javascript"></script>
 <script type="text/javascript">google.load("jquery", "1.3.2");</script>
 <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

使用此

 <!DOCTYPE html>

 <html lang="en">

 <head>
 <meta charset="utf-8">
<style>
body {
 font-family: 'Noto Sans', sans-serif;
 margin: 0;
 width: 100%;
 height: 100vh;
 background: #ffffff;
 background-color: black;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
  }

header {
 width: 100%;
 background: #ffffff;
 position: fixed;
 height: 4em;
 line-height: 4em;
 display: inline-block;
 padding-left: 1em;
 border-bottom: .1em solid #dddddd;
  }
h2 {
 font-size: 2.1em;
  }
p {
 font-size: 10em;
 color: white;
 padding-top: 1em;
  }

@media only screen and (min-width: 319px) {

 .menu {
    z-index: 1;
    display: none;
    font-weight: bold;
    font-size: 1.2em;
    width: 100%;
    background: #f1f1f1;
    position: fixed;
    text-align: center;
    margin-top: 3.3em;
    color: black;
  }

  .menu ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
    list-style-image: none;
    border-top: #dddddd 1px solid;
  }   

  .menu li {
    display: block;
    padding: 1em 0 1em 0;
    border-bottom: #dddddd 1px solid;
  }

  .menu li:hover {
    display: block;
    background: #585858;
    padding: 1em 0 1em 0;
      cursor: crosshair;
  }

  .menu ul li a {
    text-decoration: none;
    margin: 0px;
    color: black;
  }

  .menu ul li a:hover {
    color: white;
    text-decoration: none;
  }

  .menu a {
    text-decoration: none;
    color: black;
  }

  .menu a:hover {
    text-decoration: none;
    color: white;
  }

  #nav-icon4 {
    width: 35px;
    height: 25px;
    float: right;
    margin-top: -47px;
    margin-right: 30px;
    -webkit-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
    -webkit-transition: .5s ease-in-out;
    -moz-transition: .5s ease-in-out;
    -o-transition: .5s ease-in-out;
    transition: .5s ease-in-out;
    cursor: cell;
  }

  #nav-icon4 span {
    display: block;
    position: absolute;
    height: 5px;
    width: 100%;
    background: darkred;
    border-radius: 7px;
    opacity: 2;
    left: 0;
    -webkit-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
    -webkit-transition: .25s ease-in-out;
    -moz-transition: .25s ease-in-out;
    -o-transition: .25s ease-in-out;
    transition: .25s ease-in-out;
  }

  #nav-icon4 span:nth-child(1) {
    top: 0px;
    -webkit-transform-origin: left center;
    -moz-transform-origin: left center;
    -o-transform-origin: left center;
    transform-origin: left center;
  }

  #nav-icon4 span:nth-child(2) {
    top: 10px;
    -webkit-transform-origin: left center;
    -moz-transform-origin: left center;
    -o-transform-origin: left center;
    transform-origin: left center;
  }

  #nav-icon4 span:nth-child(3) {
    top: 20px;
     -webkit-transform-origin: left center;
    -moz-transform-origin: left center;
    -o-transform-origin: left center;
    transform-origin: left center;
  }

  #nav-icon4.open span:nth-child(1) {
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    transform: rotate(45deg);
    top: 0;
    left: 6px;
  }

  #nav-icon4.open span:nth-child(2) {
    width: 0%;
    opacity: 0;
  }

  #nav-icon4.open span:nth-child(3) {
    -webkit-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    -o-transform: rotate(-45deg);
    transform: rotate(-45deg);
    top: 25px;
    left: 6px;
  }   
  }


@media only screen and (min-width : 768px) {   
     h2 {
      z-index: 1000000;
      font-size: 1.5em;
      }

        p {
            font-size: 20em;
            color: white;
            padding-top: 1em;
  }

        ul {
            list-style-type: none;
            margin: 0;
            padding: 0;
            overflow: hidden;
  }

  li {
    float: right;
    margin-left: 20px;
    margin-right: 8px;
    margin-top: -10px;
}

li a {
    display: block;
    text-align: center;
    text-decoration: none;
}


.menu {
    display: block;
    position: absolute;
    right: 0px;
    font-size: .9em;
    width: 100%;
    padding-right: 15px;
    margin-top: 10px;
    padding-top: 10px;
    padding-bottom: 10px;
    background: rgba(255, 255, 255, 0);
    }
.menu ul {
    border-bottom: 0;
    border-top: 0;
    }
.menu li {
    border-bottom: 0;
    border-top: 0;
    }
 .menu li:hover {
    cursor: crosshair;
    padding-top: 1em;
    padding-bottom: .4em;
    padding-right: 0em;
    padding-left: 0em;
   }

.menu ul li a:hover {
  color: white;
}

#nav-icon4 {
    display: none;
    }
}

@media only screen and (min-width : 922px) {
    li {
    margin-left: 55px;
    margin-right: 18px;

    }
.menu {
    padding-right: 1px;
    }
}


@media only screen and (min-width : 1400px) {
    header {
       height: 5em;
      line-height: 5em;
    }
    h2 {
        font-size: 2.6em;    
    }
    li {
    margin-left: 55px;
    margin-right: 30px;
    }
    .menu {
        padding-right: 1px;
        font-size: 1.2em;
    }
}

    </style>

 <title>hamburgers</title>
 </head>

 <body>
     <header>

      <span>Shine Design</span>
      <div id="nav-icon4">
       <span></span>
       <span></span>
       <span></span>
      </div>

     </header>

     <div class="menu">
      <ul>
       <a href="#">
        <li>LINK ONE</li>
       </a>
       <a href="#">
        <li>LINK TWO</li>
       </a>
       <a href="#">
        <li>LINK THREE</li>
       </a>
       <a href="#">
        <li>LINK FOUR</li>
       </a>
       <a href="#">
        <li>LINK FIVE</li>
       </a>
      </ul>
     </div>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
 <script type="text/javascript">google.load("jquery", "1.3.2");</script>
 <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
   </body>
    <script>
    $(document).ready(function(){
      $('#nav-icon4').click(function(){
        $(this).toggleClass('open');
      });
    });
    </script>
  </html>

答案 3 :(得分:0)

我已经创建了一个需要完成的事情的简短示例。添加了一些jquery代码,使其按照您想要的方式工作。

  

第1步 - 在窗口宽度上添加课程hide-nav

      /* logic For window width */
  if ($(window).width() > 768) {
  //  $('.menu').addClass('hide-nav');
    $('.menu').addClass('show-nav');
  } else {
    $('.menu').removeClass('hide-nav');
  }
  

第2步 - 窗口调整大小

上添加课程hide-nav
      function resize() {
    if ($(window).width() < 768) {
      $('.menu').addClass('hide-nav');
      $('.menu').removeClass('show-nav');
    }
  else {

    $('.menu').addClass('show-nav');
}
  }

  $(window).resize(resize)
    .trigger('resize');

&#13;
&#13;
$(document).ready(function() {
  $('#nav-icon4').click(function() {
    $(this).toggleClass('open');
    $(".menu").slideToggle("slow");
  });

  /* logic For window width */
  if ($(window).width() > 768) {
    //  $('.menu').addClass('hide-nav');
    $('.menu').addClass('show-nav');
  } else {
    $('.menu').removeClass('hide-nav');
  }
});

function resize() {
  if ($(window).width() < 768) {
    $('.menu').addClass('hide-nav');
    $('.menu').removeClass('show-nav');
  } else {

    $('.menu').addClass('show-nav');
  }
}

$(window).resize(resize)
  .trigger('resize');
&#13;
body {
  font-family: 'Noto Sans', sans-serif;
  margin: 0;
  width: 100%;
  height: 100vh;
  background: #ffffff;
  background-color: black;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

header {
  width: 100%;
  background: #ffffff;
  position: fixed;
  height: 4em;
  line-height: 1.5em;
  display: inline-block;
  padding-left: 1em;
  border-bottom: .1em solid #dddddd;
}

h2 {
  font-size: 2.1em;
}

p {
  font-size: 10em;
  color: white;
  padding-top: 1em;
}

@media only screen and (min-width: 319px) {
  .menu {
    z-index: 1;
    display: none;
    font-weight: bold;
    font-size: 1.2em;
    width: 100%;
    background: #f1f1f1;
    position: fixed;
    text-align: center;
    margin-top: 3.3em;
    color: black;
  }
  .menu ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
    list-style-image: none;
    border-top: #dddddd 1px solid;
  }
  .menu li {
    display: block;
    padding: 1em 0 1em 0;
    border-bottom: #dddddd 1px solid;
  }
  .menu li:hover {
    display: block;
    background: #585858;
    padding: 1em 0 1em 0;
    cursor: crosshair;
  }
  .menu ul li a {
    text-decoration: none;
    margin: 0px;
    color: black;
  }
  .menu ul li a:hover {
    color: white;
    text-decoration: none;
  }
  .menu a {
    text-decoration: none;
    color: black;
  }
  .menu a:hover {
    text-decoration: none;
    color: white;
  }
  #nav-icon4 {
    width: 35px;
    height: 25px;
    float: right;
    margin-top: -60px;
    margin-right: 30px;
    -webkit-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
    -webkit-transition: .5s ease-in-out;
    -moz-transition: .5s ease-in-out;
    -o-transition: .5s ease-in-out;
    transition: .5s ease-in-out;
    cursor: cell;
  }
  #nav-icon4 span {
    display: block;
    position: absolute;
    height: 5px;
    width: 100%;
    background: darkred;
    border-radius: 7px;
    opacity: 2;
    left: 0;
    -webkit-transform: rotate(0deg);
    -moz-transform: rotate(0deg);
    -o-transform: rotate(0deg);
    transform: rotate(0deg);
    -webkit-transition: .25s ease-in-out;
    -moz-transition: .25s ease-in-out;
    -o-transition: .25s ease-in-out;
    transition: .25s ease-in-out;
  }
  #nav-icon4 span:nth-child(1) {
    top: 0px;
    -webkit-transform-origin: left center;
    -moz-transform-origin: left center;
    -o-transform-origin: left center;
    transform-origin: left center;
  }
  #nav-icon4 span:nth-child(2) {
    top: 10px;
    -webkit-transform-origin: left center;
    -moz-transform-origin: left center;
    -o-transform-origin: left center;
    transform-origin: left center;
  }
  #nav-icon4 span:nth-child(3) {
    top: 20px;
    -webkit-transform-origin: left center;
    -moz-transform-origin: left center;
    -o-transform-origin: left center;
    transform-origin: left center;
  }
  #nav-icon4.open span:nth-child(1) {
    -webkit-transform: rotate(45deg);
    -moz-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    transform: rotate(45deg);
    top: 0;
    left: 6px;
  }
  #nav-icon4.open span:nth-child(2) {
    width: 0%;
    opacity: 0;
  }
  #nav-icon4.open span:nth-child(3) {
    -webkit-transform: rotate(-45deg);
    -moz-transform: rotate(-45deg);
    -o-transform: rotate(-45deg);
    transform: rotate(-45deg);
    top: 25px;
    left: 6px;
  }
}


/* Break at Devices such as Tablets, Desktops */

@media only screen and (min-width: 768px) {
  h2 {
    z-index: 1000000;
    font-size: 1.5em;
  }
  p {
    font-size: 20em;
    color: white;
    padding-top: 1em;
  }
  ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
  }
  li {
    float: right;
    margin-left: 20px;
    margin-right: 8px;
    margin-top: -10px;
  }
  li a {
    display: block;
    text-align: center;
    text-decoration: none;
  }
  .menu {
    display: block;
    position: absolute;
    right: 0px;
    font-size: .9em;
    width: 100%;
    padding-right: 15px;
    margin-top: 10px;
    padding-top: 10px;
    padding-bottom: 10px;
    background: rgba(255, 255, 255, 0);
  }
  .menu ul {
    border-bottom: 0;
    border-top: 0;
  }
  .menu li {
    border-bottom: 0;
    border-top: 0;
  }
  .menu li:hover {
    cursor: crosshair;
    padding-top: 1em;
    padding-bottom: .4em;
    padding-right: 0em;
    padding-left: 0em;
  }
  .menu ul li a:hover {
    color: white;
  }
  #nav-icon4 {
    display: none;
  }
}


/* Medium Devices, Desktops */

@media only screen and (min-width: 922px) {
  li {
    margin-left: 55px;
    margin-right: 18px;
  }
  .menu {
    padding-right: 1px;
  }
}


/* Large Devices, Wide Screens */

@media only screen and (min-width: 1400px) {
  header {
    height: 5em;
    line-height: 5em;
  }
  h2 {
    font-size: 2.6em;
  }
  li {
    margin-left: 55px;
    margin-right: 30px;
  }
  .menu {
    padding-right: 1px;
    font-size: 1.2em;
  }
}

.show-nav {
  display: block !important;
}

.hide-nav {
  display: none
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<head>
  <meta charset="utf-8">

  <link rel="stylesheet" href="reset.css">

  <link rel="stylesheet" href="hamburgers.css">

  <script src="http://www.google.com/jsapi" type="text/javascript"></script>

  <script type="text/javascript">
    google.load("jquery", "1.3.2");
  </script>

  <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

  <script type="text/javascript" src="ham.js"></script>

  <title>hamburgers</title>

</head>

<body>


  <header>
    <h2><span>Shine Design</span></h2>
    <div id="nav-icon4">
      <span></span>
      <span></span>
      <span></span>
    </div>
  </header>

  <div class="menu" id="menuExpand">
    <ul>
      <a href="#">
        <li>LINK ONE</li>
      </a>
      <a href="#">
        <li>LINK TWO</li>
      </a>
      <a href="#">
        <li>LINK THREE</li>
      </a>
      <a href="#">
        <li>LINK FOUR</li>
      </a>
      <a href="#">
        <li>LINK FIVE</li>
      </a>
    </ul>
  </div>

  <main>
  </main>
</body>

</html>
&#13;
&#13;
&#13;