我正在尝试在菜单上实现一个非常基本的鼠标效果,因此标签后面会出现动画背景颜色。 它正在工作,但在某些标签上,背景只会在几秒钟内消失,而在其他标签上则表现正常。 我正在粘贴三个文件的来源,以便您复制该问题。
4.php`
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>StarTrackr!</title>
<link rel="stylesheet" href="css/4.css" type="text/css" media="screen" charset="utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/4.js"></script>
</head>
<body>
<div id="navigation">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Buy Now</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Gift Ideas</a></li>
<li><a href="#">FAQ</a></li>
</ul>
</div>
<div id="log"></div>
</body>
</html>
`
4.css
`
#head {
padding-left: 20px;
}
#navigation {
margin: 0 0 10px 0;
padding: 0;
list-style-type: none;
position: relative;
z-index: 1;
/* overwrite base */
float:none;
width:100%;
}
#navigation ul {
margin: 0;
padding: 0;
}
#navigation li {
display: inline;
margin: 0;
padding: 0;
}
#navigation a {
color: #015287;
display: inline-block;
padding: 5px;
text-decoration: none;
}
#navigation-blob {
top: 0;
background-color: #c0ffee;
position: absolute;
z-index: -1;
}
p#intro {
clear: both;
margin-top: 10px;
}
`
4.js
`
$(function(){
$('<div id="navigation-blob"></div>').css({
width: $('#navigation li:first a').width() + 10,
height: $('#navigation li:first a').height() + 10
}).appendTo('#navigation').hide();
$('#log').append('Test.<br/>');
$('#navigation a').mouseover(function(){
$('#log').append('Handler for .mouseover() called.<br/>');
// Mouse over function
$('#navigation-blob')
.show()
.animate(
{width: $(this).width() + 10, left: $(this).position().left},
{duration: 'slow', easing: 'easeOutElastic', queue: false}
);
});
$('#navigation a').mouseout(function(){
$('#log').append('Handler for .mouseout() called.<br/>');
// Mouse out function
var leftPosition = $('#navigation li:first a').position().left;
$('#navigation-blob')
.animate(
{width:'hide'},
{duration:'slow', easing: 'easeOutCirc', queue:false}
).animate({left: leftPosition}, 'fast' )
$('#log').append('Handler for .mouseout()2 called.<br/>');
});
});
`
答案 0 :(得分:0)
参考 LIVE DEMO
我已对您的JS
和CSS
进行了一系列更改。
$(function(){
$('#log').append('Test.<br/>');
$('#navigation a').hover(
function() {
$('#log').append('Handler for .mouseover() called.<br/>');
// Mouse over function
$(this).attr('class', 'navigation');
},
function() {
$('#log').append('Handler for .mouseout() called.<br/>');
// Mouse out function
$(this).removeAttr('class');
$('#log').append('Handler for .mouseout()2 called.<br/>');
}
);
});
#head {
padding-left: 20px;
}
#navigation {
margin: 0 0 10px 0;
padding: 0;
list-style-type: none;
position: relative;
z-index: 1;
/* overwrite base */
float:none;
width:100%;
}
#navigation ul {
margin: 0;
padding: 0;
}
#navigation li {
display: inline;
margin: 0;
padding: 0;
}
#navigation a {
color: #015287;
display: inline-block;
padding: 5px;
text-decoration: none;
}
.navigation {
background-color: #c0ffee;
position: relative;
}
p#intro {
clear: both;
margin-top: 10px;
}
我添加了您预期的动画。请参阅 LIVE DEMO 2
$(function(){
$('#log').append('Test.<br/>');
$('<div id="navigation-blob"></div>').css({
width: $('#navigation li:first a').width() + 10,
height: $('#navigation li:first a').height() + 10
}).appendTo('#navigation').hide();
$('#navigation a').hover(
function() {
$('#log').append('Handler for .mouseover() called.<br/>');
$('#navigation-blob')
.show()
.animate({
width: $(this).width() + 10,
left: $(this).position().left
},
{
duration: 'slow',
queue: false
});
},
function() {
$('#log').append('Handler for .mouseout() called.<br/>');
$('#navigation-blob')
.show()
.animate({
width: $(this).width() + 10,
left: $(this).position().left
}, 'fast');
$('#log').append('Handler for .mouseout()2 called.<br/>');
}
);
});