Javascript汉堡包菜单切换

时间:2016-12-21 00:48:08

标签: javascript html css toggle

我一直在尝试制作一个响应式导航栏,以便在将来处理项目时用作模板。我已经按照了一些教程并将我学到的东西混合在一起,现在当我试图切换我创建的汉堡包菜单时,我似乎无法切换它并且无法弄清楚我做错了什么。还是一个新手,所以如果有人能指出我的方向会很棒。

这是我的html和javascript:

    <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Navigation</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script> 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  </head>
  <body>
    <nav class="nav-main">
        <img class="logo" src="images/melogo.png" alt="logo">
        <span class="nav-button"> </span>
        <ul class="nav">
            <li>
                <a href="#" class="nav-item">Home</a>
            </li>
            <li>
                <a href="#" class="nav-item">Images</a>
            </li>
            <li>
                <a href="#" class="nav-item">Spots</a>
                <div class="nav-content">
                    <div class="nav-sub">
                        <ul>
                            <li><a href="#" class="nav-item">Street spots</a></li>
                            <li><a href="#" class="nav-item">Skateparks</a></li>
                        </ul>
                    </div>
                </div>
            </li>
            <li>
                <a href="#" class="nav-item">About us</a>
            </li>
        </ul>
    </nav>


    <script>
        $('span.nav-button').click(function {
            $('.nav-main ul').toggle();
        })
    </script>

        <div class="content">
            <p>
                Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
            </p>
            <p>
                Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
            </p>

        </div>



    <!-- page content -->
  </body>
</html>

这是我的CSS:

body, html{
    margin: 0;
    padding: 0;
}
.content{
    padding: 30px;
}
.nav-main{
    width: 100%;
    background-color: #222;
    height: 70px;
    color: #fff;
}
.nav-main .logo{
    float: left;
    max-width: 250px;
    padding: 20px 30px; 
}
.nav-main > ul {
    margin: 0;
    padding: 0;
    float: right;
    list-style-type: none;
}
.nav-main > ul > li{
    float: right;
}
.nav-item{
    display: inline-block;
    padding: 25px 10px; 
    color: #fff;
    text-decoration: none;
}
.nav-item:hover{
    background-color: #444;
}
.nav-content{
    position: absolute;
    top: 70px;
    overflow: hidden;
    background-color: #222;
    max-height: 0;
}
.nav-content a{
    text-decoration: none;
    color: #fff;
}
.nav-content a:hover{

}
.nav-sub{
    padding: 20px;
}
.nav-sub ul{
    padding: 0;
    margin: 0;
    list-style-type: none;
}
.nav-sub ul li a{
    padding: 5px 0;
    display: inline-block;
}
.nav-item:focus{
    background-color: #444;
}
.nav-item:focus ~ .nav-content{
    max-height: 400px;
    -webkit-transition: max-height: 0.4s ease-in;
    -moz-transition: max-height: 0.4s ease-in;
    transition: max-height: 0.4s ease-in;
}

@media screen and (max-width: 600px){
    .nav-main{
        width: 100%;
        background-color: #222;
        min-height: 70px;
        color: #fff;
        display: block;
    }
    .nav-main > ul {
        clear:both;
        background-color: #222;
        width: 100%;
    }
    .nav-main > ul > li{
        width: 100%;
    }
    .nav-item{
        text-decoration: none;
        width: 100%;
        text-align: center;
    }
    .nav-button{
        display: block;
        background-color: #222;
        color: red;
        font-size: 40px;
        text-align: center;
        cursor: pointer;
        float: right;
        padding-right: 30px;
    }
    .nav-button:before{
        content: "H";
    }
}

非常感谢任何帮助。

谢谢你们。

2 个答案:

答案 0 :(得分:0)

$('span.nav-button').click(function {
  $('.nav-main ul').toggle();
});

$('span.nav-button').click(function() {
  $('.nav-main ul').toggle();
});

你忘了在功能后放括号....

答案 1 :(得分:0)

如果您想让它向下滑动,您可以使用:

$('span.nav-button').click(function() {
   $('.nav-main ul').slideToggle();
});

Example