Javascript 汉堡菜单切换

标签 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";
    }
}

如有任何帮助,我们将不胜感激。

谢谢你们。

最佳答案

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

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

你忘了在函数后面加上括号....

关于Javascript 汉堡菜单切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41253505/

相关文章:

javascript - jQuery - 无限滚动

javascript - 在垂直列表中显示 JavaScript 数组

HTML anchor 按钮问题

html - css 文本装饰 :none; in chrome with <a> inside of <div>

javascript - append 到字符串变量

javascript - 无法解析来自 React Firebase 对象的数据

jquery - 工具提示尖箭头饼图 Highcharts

javascript - 按钮内的链接在任何浏览器上都不起作用

javascript - Fusion Tables javascript 图表不工作

css - 为什么会发生奇怪的 CSS 背景图像转换?