Jquery动画底层问题

真奇怪。 我有一个动画,当我在DIV上播放时开始。 进入这个div我有另一个绝对位置,我想向上移动,直到鼠标停留在主div上,当我离开主div时,你的位置下降。 简单的hover效果。 我写下了这个function,它可以很好地解决一个奇怪的错误。

var inside = $('.inside') inside.hover(function() { $(this).children(".coll_base").stop().animate({ bottom:'+=30px' },"slow") }, function() { $(this).children(".coll_base").stop().animate({ bottom:'-=30px' },"slow"); }); 

我需要+ = 30和 – = 30,因为我必须将此函数应用于具有不同底部的div范围。 问题在于,如果我将鼠标hover在div之外,那么DIV将会生效,但是当它下降时,他会降低每个动画的底部值。 如果我从主div上下来,我看到动画div甚至超出了主div。 我想我想念一些东西来解决这个bug。 谢谢你的帮助

如果最初的bottom不是0 ,那么您可以存储初始底部值,并在第二个函数中使用它。 (如果初始bottom0 ,那么只需使用它。)

在下面的示例中,使用内部的data()属性存储初始bottom位置,然后在第二个处理程序中检索它以返回到原始位置。

实例: http //jsfiddle.net/VAsZZ/

 var inside = $('.inside'); inside.hover( function() { if(!inside.data('bottom')) { inside.data('bottom',$(this).children(".coll_base").css('bottom')); } $(this).children(".coll_base").stop().animate({ bottom:'+=30px' },"slow") }, function() { $(this).children(".coll_base").stop().animate({bottom:$(this).data('bottom') },"slow"); } );​ 

在这种情况下,我不认为你可以使用相对( – = 30px和+ = 30px)值,因为动画可以在任意点停止,这会抛出跟随它的动画。 最好调用匹配集上的each(),计算正确的偏移量并将值直接传递到hover函数中。