gpt4 book ai didi

javascript - 导航栏 jQuery 不工作

转载 作者:行者123 更新时间:2023-11-27 23:50:42 25 4
gpt4 key购买 nike

我是一名学习一些非常基本的 HTML 编码的学生,我决定使用一个简单的 JavaScript 导航栏,以便下拉菜单具有一定的动画速度。

导航栏以前可以工作,但在添加我的图像 slider 后它停止工作了。

感谢您的帮助!

// JavaScript NavBar
$( document ).ready(function() {
$('#navbar ul li ul').hide().removeClass('fallback');

$('#navbar ul li').mouseenter(function () {
$('#navbar ul', this).stop().slideDown(500);
});
$('#navbar ul li').mouseleave(function () {
$('#navbar ul', this).stop().slideUp(100);
});
});
* {
margin:0;
padding:0;
}

body{
background:url(../images/subtle_white_mini_waves.png) repeat;
font-family:Tahoma, Geneva, sans-serif;
color: white;
}

#navbar{
margin-left:-400px;
position:absolute;
left:50%;
}

#navbar a{
text-decoration:none;
}

.button{
background:url(../images/navbarbutton.png);
margin-top: 66px;
width: 170px;
}

.button:hover{
background:#e6e6e6;
}

.button a{
padding: 34px 0px;
}

#navbar ul{
text-align:center;
}

#navbar ul li{
float: left;
display: inline;
font-size:16px;
height:89px;
}

#navbar ul li:hover{
background:#E6E6E6;
}

#navbar ul li a{
display:block;
color: #444;
}

#navbar ul li ul{
position:absolute;
width: 170px;
background:#fff;
}

#navbar ul li ul li{
width: 170px;

}

#navbar ul li ul li a{
display:block;
padding: 15px 0px 15px 0px;
color: #444;
font-size: 14px;
}

#navbar ul li ul li:hover a{
background:#f7f7f7;
}

#navbar ul li ul.fallback{
display:none;
}

#navbar ul li:hover ul.fallback{
display:block;
}

.shadows{
position:absolute;
z-index:10;
}

#shadowtopleft{
margin-left:4.6875%;
margin-right:140px;
float:left;
}

#shadowtopright{
float:left;
}

#shadowbottomleft{
margin-top: 83px;
margin-left:4.6875%;
margin-right:140px;
float:left;
}

#shadowbottomright{
margin-top: 83px;
float:left;
}

.banner {
z-index:-1;
position: relative;
width: 100%;
overflow: auto;
padding: 0px;
margin: 0px;
}

.banner ui{
list-style:none;
padding: 0px;
margin: 0px;
}

.banner ul li {
display:block;
float:left;
padding: 0px;
margin: 0px;
min-height:500px;
}
    <!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Beyond - Home</title>
<link href="css/styles.css" rel="stylesheet" type="text/css">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="scripts/NavBar.js"></script>
<script src="scripts/unslider.min.js"></script>
</head>

<body>
<!--NavBar start-->
<div id="navbar">
<ul>
<li class="button"><a href="#">Programmes</a>
<ul class="fallback">
<li><a href="#">Problem De-esclation</a></li>
<li><a href="#">Family Strengthening</a></li>
<li><a href="#">Community Integration</a></li>
<li><a href="#">Support Programmes</a></li>
</ul>
</li>
<li class="button"><a href="#">How You Can Help</a>
<ul class="fallback">
<li><a href="#">Donate</a></li>
<li><a href="#">Volunteer</a></li>
<li><a href="#">Sponsor</a></li>
<li><a href="#">Partner</a></li>
<li><a href="#">Join The Staff</a></li>
</ul>
</li>
<li>
<div id="logo">
<a href="index.html"><img src="images/logo.png" width="140" height="225" alt="Beyond - Logo"></a>
</div>
</li>
<li class="button"><a href="#">About Beyond</a>
<ul class="fallback">
<li><a href="#">Our board</a></li>
<li><a href="#">News and Views</a></li>
</ul>
</li>
<li class="button"><a href="#">Contact Us</a>
<ul class="fallback">
<li><a href="#">Facilities</a></li>
<li><a href="#">Feedback</a></li>
</ul>
</li>
</ul>
</div>
<!--NavBar end-->
<!--Shadows start-->
<div class="shadows">
<div id="shadowtopleft">
<img src="images/shadowtopleft.png" width="520" height="66">
</div>
<div id="shadowtopright">
<img src="images/shadowtopright.png" width="520" height="66">
</div>
<div id="shadowbottomleft">
<img src="images/shadowbottomleft.png" width="520" height="13">
</div>
<div id="shadowbottomright">
<img src="images/shadowbottomright.png" width="520" height="13">
</div>
</div>
<!--Shadows end-->
<!--Unslider start-->
<div class="banner">
<ul>
<li><img src="http://techkaps.files.wordpress.com/2010/02/1280x720_hd_wallpaper_123_zixpkcom.jpg"></li>
<li><img src="http://techkaps.files.wordpress.com/2010/02/1280x720_hd_wallpaper_123_zixpkcom.jpg"></li>
<li><img src="http://techkaps.files.wordpress.com/2010/02/1280x720_hd_wallpaper_123_zixpkcom.jpg"></li>
</ul>
</div>
<script>
$( document ).ready(function() {
$('.banner').unslider({
speed: 500, // The speed to animate each slide (in milliseconds)
delay: 3000, // The delay between slide animations (in milliseconds)
complete: function() {}, // A function that gets called after every slide animation
keys: true, // Enable keyboard (left, right) arrow shortcuts
dots: true, // Display dot navigation
fluid: false // Support responsive design. May break non-responsive designs
});
});
</script>
<!--Unslider end-->

</body>
</html>

最佳答案

由于您是一名学生,您可能没有听说过 z-index样式属性。 z-index 属性指定元素的堆叠顺序,尤其是当您经常使用 position: absolute; 时。

所以你的菜单菜单 #navbar 就在那里。它只是隐藏在其他元素之下,因此您可能希望将 z-index 样式添加到您的代码中,如下所示:

#navbar {
margin-left: -400px;
position: absolute;
left: 50%;
z-index: 100;
}

我还认为我会在您的 CSS 代码中提到您犯了一个小错误:

.banner ui{
list-style:none;
padding: 0px;
margin: 0px;
}

应该是:

.banner ul {
list-style:none;
padding: 0px;
margin: 0px;
}

我结合了我的答案并将其放在下面的代码片段中。

// JavaScript NavBar
$(document).ready(function() {
$('#navbar ul li ul').hide().removeClass('fallback');

$('#navbar ul li').mouseenter(function() {
$('#navbar ul', this).stop().slideDown(500);
});
$('#navbar ul li').mouseleave(function() {
$('#navbar ul', this).stop().slideUp(100);
});
});
* {
margin: 0;
padding: 0;
}
body {
background: url(../images/subtle_white_mini_waves.png) repeat;
font-family: Tahoma, Geneva, sans-serif;
color: white;
}
#navbar {
margin-left: -400px;
position: absolute;
left: 50%;
z-index: 100;
}
#navbar a {
text-decoration: none;
}
.button {
background: url(../images/navbarbutton.png);
margin-top: 66px;
width: 170px;
}
.button:hover {
background: #e6e6e6;
}
.button a {
padding: 34px 0px;
}
#navbar ul {
text-align: center;
}
#navbar ul li {
float: left;
display: inline;
font-size: 16px;
height: 89px;
}
#navbar ul li:hover {
background: #E6E6E6;
}
#navbar ul li a {
display: block;
color: #444;
}
#navbar ul li ul {
position: absolute;
width: 170px;
background: #fff;
}
#navbar ul li ul li {
width: 170px;
}
#navbar ul li ul li a {
display: block;
padding: 15px 0px 15px 0px;
color: #444;
font-size: 14px;
}
#navbar ul li ul li:hover a {
background: #f7f7f7;
}
#navbar ul li ul.fallback {
display: none;
}
#navbar ul li:hover ul.fallback {
display: block;
}
.shadows {
position: absolute;
z-index: 10;
}
#shadowtopleft {
margin-left: 4.6875%;
margin-right: 140px;
float: left;
}
#shadowtopright {
float: left;
}
#shadowbottomleft {
margin-top: 83px;
margin-left: 4.6875%;
margin-right: 140px;
float: left;
}
#shadowbottomright {
margin-top: 83px;
float: left;
}
.banner {
z-index: -1;
position: relative;
width: 100%;
overflow: auto;
padding: 0px;
margin: 0px;
}
.banner ul {
list-style: none;
padding: 0px;
margin: 0px;
}
.banner ul li {
display: block;
float: left;
padding: 0px;
margin: 0px;
min-height: 500px;
}
<!doctype html>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Beyond - Home</title>
<link href="css/styles.css" rel="stylesheet" type="text/css">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="scripts/NavBar.js"></script>
<script src="scripts/unslider.min.js"></script>
</head>

<body>
<!--NavBar start-->
<div id="navbar">
<ul>
<li class="button"><a href="#">Programmes</a>
<ul class="fallback">
<li><a href="#">Problem De-esclation</a>
</li>
<li><a href="#">Family Strengthening</a>
</li>
<li><a href="#">Community Integration</a>
</li>
<li><a href="#">Support Programmes</a>
</li>
</ul>
</li>
<li class="button"><a href="#">How You Can Help</a>
<ul class="fallback">
<li><a href="#">Donate</a>
</li>
<li><a href="#">Volunteer</a>
</li>
<li><a href="#">Sponsor</a>
</li>
<li><a href="#">Partner</a>
</li>
<li><a href="#">Join The Staff</a>
</li>
</ul>
</li>
<li>
<div id="logo">
<a href="index.html">
<img src="images/logo.png" width="140" height="225" alt="Beyond - Logo">
</a>
</div>
</li>
<li class="button"><a href="#">About Beyond</a>
<ul class="fallback">
<li><a href="#">Our board</a>
</li>
<li><a href="#">News and Views</a>
</li>
</ul>
</li>
<li class="button"><a href="#">Contact Us</a>
<ul class="fallback">
<li><a href="#">Facilities</a>
</li>
<li><a href="#">Feedback</a>
</li>
</ul>
</li>
</ul>
</div>
<!--NavBar end-->
<!--Shadows start-->
<div class="shadows">
<div id="shadowtopleft">
<img src="images/shadowtopleft.png" width="520" height="66">
</div>
<div id="shadowtopright">
<img src="images/shadowtopright.png" width="520" height="66">
</div>
<div id="shadowbottomleft">
<img src="images/shadowbottomleft.png" width="520" height="13">
</div>
<div id="shadowbottomright">
<img src="images/shadowbottomright.png" width="520" height="13">
</div>
</div>
<!--Shadows end-->
<!--Unslider start-->
<div class="banner">
<ul>
<li>
<img src="http://techkaps.files.wordpress.com/2010/02/1280x720_hd_wallpaper_123_zixpkcom.jpg">
</li>
<li>
<img src="http://techkaps.files.wordpress.com/2010/02/1280x720_hd_wallpaper_123_zixpkcom.jpg">
</li>
<li>
<img src="http://techkaps.files.wordpress.com/2010/02/1280x720_hd_wallpaper_123_zixpkcom.jpg">
</li>
</ul>
</div>
<script>
$(document).ready(function() {
$('.banner').unslider({
speed: 500, // The speed to animate each slide (in milliseconds)
delay: 3000, // The delay between slide animations (in milliseconds)
complete: function() {}, // A function that gets called after every slide animation
keys: true, // Enable keyboard (left, right) arrow shortcuts
dots: true, // Display dot navigation
fluid: false // Support responsive design. May break non-responsive designs
});
});
</script>
<!--Unslider end-->

</body>

</html>

关于javascript - 导航栏 jQuery 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27537917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com