为什么那个汉堡包菜单不起作用?

时间:2016-06-07 12:17:40

标签: html css html5 css3

你能告诉我为什么汉堡菜单不起作用吗?我真的没有找到答案;(连接HTML / Javascript是可以的。我必须说的是我不懂Javascript,我只是从网上复制代码,也许JavaScript文件有问题?我的所有文件在一个文件夹中,没有子文件夹:

HTML:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="reset.css">
    <link rel="stylesheet" type="text/css" href="style.css">
    <script type="text/javascript" src="nav.js"></script>
</head>

<body>
    <header>
        <h1>Menu</h1>
        <nav>
            <a href="#" class="hamburger">
                <div class="line"></div>
                <div class="line"></div>
                <div class="line"></div>
            </a>
            <ul class="clearfix menu">
                <li><a href="#">Home</a></li>
                <li><a href="#">Service</a></li>
                <li><a href="#">Contact</a></li>
                <li><a href="#">Blog</a></li>
                <li><a href="#">About</a></li>
            </ul>
        </nav>
    </header>
</body>

CSS:

@import url('http://fonts.googleapis.com/css?family=Roboto:400,700');

*, *:before, *:after {
margin: 0;
padding: 0;
box-sizing: border-box;
}

.clearfix:after {
content: "";
display: table;
clear: both;
}

html, body {
height: 100%;
}

body {
font: 1em 'Roboto', sans-serif;
color: #555;
background-color: #fff;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

ul {
list-style: none;
max-width: 800px;
margin: 0 auto;
border-left: 1px solid #ddd;
border-right: 1px solid #ddd;
}

h1 {
padding: 30px 0;
font: 1.5em 'Open Sans', sans-serif;
text-align: center;
}

nav {
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
background-color: #f5f5f5;
}

ul a {
display: block;
padding: 20px;
padding-right: 0 !important; /* important overrides media queries */
font-size: 13px;
font-weight: bold;
text-align: center;
color: #aaa;
cursor: pointer;
text-decoration: none;
}

ul a:hover {
background: #eee;
}

nav li {
float: left;
width: 20%;
border-right: 1px solid #ddd;
}

nav li:last-child {
border-right: none;
}

@media only screen and (max-width: 480px) {

.hamburger {
    padding: 15px;
    display: block;
}

.line {
    border-bottom: 4px solid #bbb;
    width: 35px;
    margin-bottom: 6px;
}

.line:last-child {
    margin-bottom: 0;
}

nav li {
    width: 100%;
}

.menu {
    height: 0;
    overflow: hidden;
    transition: height 0.3s linear;
}

.slide-down {
    height: 262px;
}
}

使用Javascript:

$('.hamburger').on('click', function(e) {
e.preventDefault();
$('.menu').toggleClass('slide-down');
});

2 个答案:

答案 0 :(得分:0)

我刚刚将您的代码复制到JSFiddle,它几​​乎可以使用。 你要么:

顺便说一句,除非您将窗口调整到较小的宽度,否则汉堡图标将不会显示。

尝试添加到<head>

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

答案 1 :(得分:-1)

以下是您的代码的DEMO。 尝试在<head>

下添加jQuery文件
<!DOCTYPE html>
<html>
<head>
     <meta charset="UTF-8">
     <link rel="stylesheet" type="text/css" href="reset.css">
     <link rel="stylesheet" type="text/css" href="style.css">
     <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
     <script type="text/javascript" src="nav.js"></script>

</head>