[英]jQuery .slideToggle() not working
我正在嘗試為我的網站構建一個移動友好的導航,我想使用一個在按下菜單鏈接后向下滑動的菜單。 我以為這很簡單,但是沒有用。 當我單擊鏈接“菜單”時,沒有任何反應。 它應該使用jQuery的.slideToggle功能顯示一個菜單。 如果您可以提供修復程序或替代方法來制作響應式和移動友好菜單。
<script>
$(document).ready(function(){
$("#burger").click(function(){
$("#menu").slideToggle();
});
});
</script>
<!-- END OF SCRIPTS -->
<!-- HEADER -->
<header>
<div id="top">
<a href="#" id="logo"><img src="root/logo.png"></a>
<div id="burger">
<a>Menu</a>
</div>
</div>
<nav id="navbar">
<ul id="menu">
<li><a href="#">Option 1</a></li>
<li><a href="#">Option 2</a></li>
<li><a href="#">Option 3</a></li>
<li><a href="#">Option 4</a></li>
<li><a href="#">Option 5</a></li>
</ul>
</nav>
</header>
<!-- END HEADER -->
</body>
</head>
這是我的CSS:
/* HEADER */
header {
width: 100%;
background-color: #012d5a;
height: 150px;
}
#top {
width: 80%;
margin-left: auto;
margin-right: auto;
}
#top img{
height: 110px;
margin-left: 0px;
}
#navbar {
width: 100%;
height: 40px;
background-color: #B9E0F6;
display: block;
color: #000;
font-family: helvetica;
font-size: 16px;
font-weight: 600;
}
#menu {
display: block;
width: 600px;
margin: auto;
height: 40px;
}
#menu li {
float: left;
width: 120px;
display: inline;
padding-top: 10px;
height: 40px;
border-right: 2px solid #000;
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
text-align: center;
}
#menu li:last-child {
border-right: 0;
}
#burger {
display: none;
float: right;
}
/* Responsive Menu */
@media only screen and (max-device-width: 767px) {
/* define mobile specific styles here */
#burger {
display: block;
}
#navbar {
height: auto;
position: relative;
z-index: 1;
}
#menu {
display: none;
height: 200px;
width: 100%;
}
#menu li {
display: block;
float: none;
width: 50%;
border: none;
margin: auto;
}
#top {
width: 100%;
}
}
您的菜單隱藏在CSS中,請嘗試以下操作:
$("#burger").click(function(){ $("#navbar").slideToggle(); });
/* HEADER */ header { width: 100%; background-color: #012d5a; height: 150px; } #top { width: 80%; margin-left: auto; margin-right: auto; } #top img{ height: 110px; margin-left: 0px; } #navbar { width: 100%; height: 40px; background-color: #B9E0F6; display: block; color: #000; font-family: helvetica; font-size: 16px; font-weight: 600; } #menu { display: block; width: 600px; margin: auto; height: 40px; } #menu li { float: left; width: 120px; display: inline; padding-top: 10px; height: 40px; border-right: 2px solid #000; box-sizing:border-box; -moz-box-sizing:border-box; -webkit-box-sizing:border-box; text-align: center; } #menu li:last-child { border-right: 0; } #burger { float: right; color: #B9E0F6; margin-top: 24px; font-size: 24px; cursor: pointer; text-decoration: underline; } /* Responsive Menu */ @media only screen and (max-device-width: 767px) { /* define mobile specific styles here */ #burger { display: inline-block; } #navbar { height: auto; position: relative; z-index: 1; } #menu { display: none; height: 200px; width: 100%; } #menu li { display: block; float: none; width: 50%; border: none; margin: auto; } #top { width: 100%; } }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <html> <body> <!-- Scripts Here --> <script type = "text/javascript" src="script/jquery-2.1.4.min.js"></script> <script type = "text/javascript" src="script/bootstrap.min.js"></script> <!-- END OF SCRIPTS --> <!-- HEADER --> <header> <div id="top"> <div style="display:inline-block"> <a href="#" id="logo"><img src="http://doc.jsfiddle.net/_images/jsfiddle-logo-thumb.png" /></a> </div> <div id="burger"> <a>Menu</a> </div> </div> <nav id="navbar"> <ul id="menu"> <li><a href="#">Option 1</a></li> <li><a href="#">Option 2</a></li> <li><a href="#">Option 3</a></li> <li><a href="#">Option 4</a></li> <li><a href="#">Option 5</a></li> </ul> </nav> </header> <!-- END HEADER -->
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.