【发布时间】:2009-12-09 20:20:37
【问题描述】:
我正在为我的网站使用基于 JQuery 的 CSS 菜单。但是,我的问题是,如果我单击菜单项中的一项。我希望它保持某种颜色,在我的情况下是图像的一半。
我的代码来自http://snook.ca/archives/javascript/jquery-bg-image-animations/ 我使用了第二个示例。下面是示例页面:http://snook.ca/technical/jquery-bg/
我的代码如下所示
<script type="text/javascript">
$(function(){
$('#nav a')
.css( {backgroundPosition: "-20px 35px"} )
.mouseover(function(){
$(this).stop().animate({backgroundPosition:"(-20px 94px)"}, {duration:800})
})
.mouseout(function(){
$(this).stop().animate({backgroundPosition:"(40px 35px)"}, {duration:800, complete:function(){
$(this).css({backgroundPosition: "-20px 35px"})
}})
})
});
</script>
如果您将鼠标悬停在菜单上,菜单将变为不同的颜色,这就是我希望菜单在菜单处于活动状态并被点击时保持的颜色。
希望有人可以帮助我。
我按照答案尝试了,还是没有
我修改后的代码
<script type="text/javascript">
$(function(){
$('#nav a')
.css( {backgroundPosition: "-20px 35px"} )
.mouseover(function(){
$(this).stop().animate({backgroundPosition:"(-20px 94px)"}, {duration:800})
})
.mouseout(function(){
$(this).stop().animate({backgroundPosition:"(40px 35px)"}, {duration:800, complete:function(){
$(this).css({backgroundPosition: "-20px 35px"})
}})
})
$('#nav a').click(function() {
$('#nav a:not(.selected');
$('#nav a').removeClass('selected');
$(this).addClass('selected');
})
});
</script>
【问题讨论】: