一个更好的方法是使用不显眼的脚本,你想要的主要是.stop()
停止上一个动画的调用,如下所示:
<script type="text/javascript">
function hoverIn(target, speed){
$(target).stop(true, true).fadeIn(speed);
}
function hoverOut(target, speed){
$(target).stop(true, true).fadeOut(speed);
}
</script>
这仍然是一个问题,因为进入/离开孩子时会发生火灾mouseover
。mouseout
但是,.hover()
使用mouseenter
和mouseleave
不会遇到同样的问题,并且会消除您的“振动”问题。
不显眼的版本如下所示:
$(function() {
$("ul > li").hover(function() {
$(this).find("ul").stop(true, true).fadeIn('fast');
}, function() {
$(this).find("ul").stop(true, true).fadeOut('fast');
});
});
你可以在这里看到它,或者更短的版本:
$(function() {
$("ul > li").hover(function() {
$(this).find("ul").stop(true, true).animate({opacity: 'toggle'}, 'fast');
});
});
您可以在此处进行测试,请注意,所有这些不显眼的方法都不使用任何内联 javascript,并且适用于多个子菜单。