jQuery 动画问题
这里 有一个演示,这是我当前的 jQuery
var height1 = $('.spotlight:nth-child(1)').height();
var height2 = $('.spotlight:nth-child(2)').height();
$('.spotlight').each(function() {
var spot = $( this ),
caption = $("<div class='caption'></div>").appendTo(spot);
caption.load( spot.data('who') + '.html' );
spot.hover(function() {
caption.clearQueue().animate({ top : '-=100px' }, 150)
}, function () {
caption.clearQueue().animate({ top : '+=100px' }, 150)
});
});
$('.spotlight:nth-child(1) .caption').css('top', height1 + 'px');
$('.spotlight:nth-child(2) .caption').css('top', height2 + 'px');
,一切都工作得很好,但是有如果你继续看下去,就会出现烦人的动画循环。我尝试使用 .clearQueue() 和 .stop() 但它们也有自己的错误(如果你移动得很快,它会太低或太高)无论如何?
There's a demo over here , and here's my current jQuery is
var height1 = $('.spotlight:nth-child(1)').height();
var height2 = $('.spotlight:nth-child(2)').height();
$('.spotlight').each(function() {
var spot = $( this ),
caption = $("<div class='caption'></div>").appendTo(spot);
caption.load( spot.data('who') + '.html' );
spot.hover(function() {
caption.clearQueue().animate({ top : '-=100px' }, 150)
}, function () {
caption.clearQueue().animate({ top : '+=100px' }, 150)
});
});
$('.spotlight:nth-child(1) .caption').css('top', height1 + 'px');
$('.spotlight:nth-child(2) .caption').css('top', height2 + 'px');
And it all works fine and dandy, but there's the annoying animation loop if you keep going over it. I tried using .clearQueue() and .stop() but they come with bugs of their own ( if you move fast it goes too low or too high ) Anyway around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试
stop(false,true)
而不是clearQueue()
吗?问题是clearQueue删除未运行的项目(但你不应该有一些),并且
top
属性是当前动画的顶部值。stop(false,true) 不会清除队列,而是跳转到动画结束。然后top就OK了
Can you try
stop(false,true)
instead ofclearQueue()
?problem is that the clearQueue remove not run items (but you should not have some), and the
top
property is the top value of the current animation.stop(false,true) will not clear the queue but jump to the animation end. Then top will be OK
也许您应该改用 .animate 函数。
http://api.jquery.com/animate/
Perhap you should be using the .animate function instead.
http://api.jquery.com/animate/
部分问题可能是动画被定义为增量 (-=100px)。如果此动画被激活多次,它将继续爬行您的 div。我了解您正在尝试阻止动画被多次激活。我能够使用绝对位置重新编码。我没有看到问题。在这里查看:
http://jsfiddle.net/waDVX/
这是代码:
HTML
CSS
jQuery
注意:为了简单起见,我删除了字幕的 AJAX 加载。
Part of the issue could be that the animation is defined as a delta (-=100px). If this animation gets activated multiple times, it will continue to crawl up your div. I understand that you are trying to prevent the animation from being activated multiple times. I was able to recode using absolute positions. I am not seeing issues. Check it out here:
http://jsfiddle.net/waDVX/
Here is the code:
HTML
CSS
jQuery
Note: I removed the AJAX loading of the captions for simplicity.