为什么汉堡子菜单导航无法打开?

时间:2019-01-11 02:36:48

标签: javascript html css

下面是相关代码,完整代码是here。导航的代码遵循这支笔:https://codepen.io/taniarascia/pen/dYvvYv,所有样式/脚本/ html都是正确的。我不知道是什么原因导致服务下拉列表没有在移动视图中按下以下li元素。下拉菜单在我第一次放在网站上时起作用,但是在弄乱了一些看起来无关的东西后,它不起作用了,此时我不知道发生了什么。检查器显示所有正确的样式都已应用,并且没有多余的样式被应用。

HTML

<div class="main_nav">
            <div class="navigation">
                <div class="nav-container">
                    <div class="brand">
                        <a href="/"><img src="assets/images/logo.svg" style="height: 100%;"></a>
                    </div>
                    <nav>
                        <div class="nav-mobile"><a id="nav-toggle" href="#!"><span></span></a></div>
                        <ul class="nav-list">
                            <li>
                                <a href="/">Home</a>
                            </li>
                            <li>
                                <a href="/#about">About</a>
                            </li>
                            <li>
                                <a href="#!">Services</a>
                                <ul class="nav-dropdown">
                                    <li>
                                        <a href="/web-design">Web Design</a>
                                    </li>
                                    <!--<li>-->
                                    <!--    <a href="#!">Web Development</a>-->
                                    <!--</li>-->
                                    <!--<li>-->
                                    <!--    <a href="#!">Graphic Design</a>-->
                                    <!--</li>-->
                                </ul>
                            </li>
                            <li>
                                <a href="/portfolio">Portfolio</a>
                            </li>
                            <li>
                                <a href="/#contact">Contact</a>
                            </li>
                            <li>
                                <a href="/blog">Blog</a>
                            </li>
                        </ul>
                    </nav>
                </div>
            </div>
        </div>

CSS

@charset "UTF-8";
.main_nav {
    position: fixed;
    top: 0px;
    max-height: 70px;
    z-index: 10;
    width: 100vw;
    /* padding-top: 17px; */
    background: none;
    -webkit-transition: all 0.3s;
    transition: all 0.3s;
    opacity: 0;
    /* padding-bottom: 6px; */
    left: 0;
    right: 0;
    height: 70px;
    padding: 10px 4px;
    box-sizing: border-box;
}

@media only screen and (max-width: 500px) {
    .main_nav {
        padding-top: 25px;
    }
}

.sticky {
    background-color: #ffffff;
    opacity: 1;
    top: 0px;
}

.navigation {
  height: 50px;
  background: #ffffff;
}

.brand {
  position: absolute;
  padding-left: 20px;
  float: left;
  height: 50px;
  line-height: 70px;
  text-transform: uppercase;
  font-size: 1.4em;
  /* width: 40%; */
}
.brand a,
.brand a:visited {
  color: #5fc3e4;
  text-decoration: none;
}

.nav-container {
  max-width: 1000px;
  margin: 0 auto;
}

nav {
  float: right;
  height: 50px;
}
nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  background-color: white;
}
nav ul li {
  float: left;
  position: relative;
  height:  50px;
  line-height: 50px;
}
nav ul li a,
nav ul li a:visited {
  display: block;
  padding: 0 20px;
  line-height: 50px;
  background: #ffffff;
  color: #5fc3e4;
  text-decoration: none;
}
nav ul li a:hover,
nav ul li a:visited:hover {
  color: #cfcfcf;
}
nav ul li a:not(:only-child):after,
nav ul li a:visited:not(:only-child):after {
  padding-left: 4px;
  content: ' ▾';
}
nav ul li ul li {
  min-width: 190px;
}
nav ul li ul li a {
  padding: 15px;
  line-height: 20px;
}

.nav-dropdown {
  position: absolute;
  display: none;
  z-index: 1;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);
}

/* Mobile navigation */
.nav-mobile {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  background: #ffffff;
  height: 70px;
  width: 70px;
}

@media only screen and (max-width: 798px) {
  .nav-mobile {
    display: block;
  }

  nav {
    width: 100%;
    padding: 50px 0 15px;
  }
  nav ul {
    display: none;
  }
  nav ul li {
    float: none;
  }
  nav ul li a {
    padding: 15px;
    line-height: 20px;
  }
  nav ul li ul li a {
    padding-left: 30px;
  }

  .nav-dropdown {
    position: static;
  }
}
@media screen and (min-width: 799px) {
  .nav-list {
    display: block !important;
  }
}
#nav-toggle {
  position: absolute;
  left: 18px;
  top: 22px;
  cursor: pointer;
  padding: 10px 35px 16px 0px;
}
#nav-toggle span,
#nav-toggle span:before,
#nav-toggle span:after {
  cursor: pointer;
  border-radius: 1px;
  height: 5px;
  width: 35px;
  background: #5fc3e4;
  position: absolute;
  display: block;
  content: '';
  transition: all 300ms ease-in-out;
}
#nav-toggle span:before {
  top: -10px;
}
#nav-toggle span:after {
  bottom: -10px;
}
#nav-toggle.active span {
  background-color: transparent;
}
#nav-toggle.active span:before, #nav-toggle.active span:after {
  top: 0;
}
#nav-toggle.active span:before {
  transform: rotate(45deg);
}
#nav-toggle.active span:after {
  transform: rotate(-45deg);
}

JS

 (function($) { // Begin jQuery
  $(function() { // DOM ready
    // If a link has a dropdown, add sub menu toggle.
    $('nav ul li a:not(:only-child)').click(function(e) {
      $(this).siblings('.nav-dropdown').toggle();
      // Close one dropdown when selecting another
      $('.nav-dropdown').not($(this).siblings()).hide();
      e.stopPropagation();
    });
    // Clicking away from dropdown will remove the dropdown class
    $('html').click(function() {
      $('.nav-dropdown').hide();
    });
    // Toggle open and close nav styles on click
    $('#nav-toggle').click(function() {
      $('nav ul').slideToggle();
    });
    // Hamburger to X toggle
    $('#nav-toggle').on('click', function() {
      this.classList.toggle('active');
    });
  }); // end DOM ready
})(jQuery); // end jQuery

1 个答案:

答案 0 :(得分:0)

控制台中还有其他js错误。

例如:

pen.js:11 Uncaught ReferenceError: $ is not defined
at pen.js:11

这将停止执行之后的代码。

一旦我清理了这些东西(有2或3个),汉堡包就会开始工作: https://codepen.io/lennart_rikk/pen/ZVqJVZ

(注意!在CSS中,我将主导航的不透明度更改为1,以查看菜单)