Kar*_*xdw 5 navigation jquery html5 hover navbar
出于某种原因,我只是无法将悬停效果用于
HTML:
<div id="navbarcontainer">
<ul>
<li id="left" class="current">
<a id="current">Home</a></li>
<li class="dependant1">
<a id="dependant1">Services</a></li>
<li id="right" class="dependant2">
<a id="dependant2">Contact</a></li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
#navbarcontainer {
margin: 0;
padding: 0;
height: 50px;
background: #01216D;
}
#navbarcontainer ul {
clear: both;
margin: 0;
padding: 0;
list-style: none;
}
#navbarcontainer li {
display: inline-block;
height: 50px;
width: 100px;
list-style: none;
text-align: center;
-moz-transition: .5s;
-o-transition: .5s;
-webkit-transition: .5s;
transition: .5s;
/* Firefox 4 */
/* Opera */
/* Safari and Chrome */
}
#navbarcontainer ul li a {
text-decoration: none;
line-height: 50px;
width: 100px;
font-size: 20px;
font-family: Calibri;
cursor: pointer;
}
#left {
margin-right: 40px;
margin-left: 20px;
}
#right {
margin-left: 40px;
}
.current {
background: #fff;
}
#current {
color: #01216D;
font-weight: bold;
}
#dependant1, #dependant2 {
color: #fff;
}
Run Code Online (Sandbox Code Playgroud)
jQuery的:
$("#dependant1").hover(function () {
$('.dependant1').stop().animate({background: '#fff' }, "slow");
$('#dependant1').stop().animate({color: '#01216D', 'font-weight': 'bold'}, "slow");
}, function () {
$('.dependant1').stop().animate({ background: 'none' }, "slow");
$('#dependant1').stop().animate({color: '#fff', 'font-weight': 'normal'}, "slow");
});
Run Code Online (Sandbox Code Playgroud)
我觉得它与jQuery有关,但我在document.load中有它,所以我不明白为什么它不起作用.
您需要在jQuery以下内容之后添加jQuery UI Library:
$("#navbarcontainer li").hover(function () {
$(this).find('a').stop().animate({ color: '#01216D', backgroundColor: '#fff'}, 800);
}, function () {
$(this).find('a').stop().animate({ color: '#fff', backgroundColor: '#01216D'}, 800);
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<div id="navbarcontainer">
<ul>
<li><a id="current">Home</a></li>
<li><a>Services</a></li>
<li><a>Contact</a></li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
#navbarcontainer {
margin: 0;
padding: 0;
background: #01216D;
}
#navbarcontainer ul {
clear: both;
margin: 0;
padding: 0;
list-style: none;
overflow:hidden;
}
#navbarcontainer li {
list-style: none;
text-align: center;
-moz-transition: .5s;
-o-transition: .5s;
-webkit-transition: .5s;
transition: .5s;
}
#navbarcontainer ul li a {
float:left;
color:#fff;
background: #01216D;
padding:10px 30px;
text-decoration: none;
line-height: 50px;
font-size: 20px;
font-family: Calibri;
cursor: pointer;
}
#current {
background: #fff !important;
color: #01216D !important;
font-weight: bold;
}
Run Code Online (Sandbox Code Playgroud)
或者另一个脚本:
$("#navbarcontainer li").on('mouseenter mouseleave',function ( e ) {
var set = e.type=='mouseenter' ? {c:"#01216D", bg:"#fff"} : {c:"#fff", bg:"#01216D"} ;
$('a', this).stop().animate({ color: set.c, backgroundColor: set.bg}, 800);
});
Run Code Online (Sandbox Code Playgroud)