如何在鼠标悬停和鼠标移出时使用 Jquery 进行相对动画?
我试图做到以下几点:
我的页面底部有一个固定定位的 div。当鼠标进入和退出时,div 会相应地对其高度进行动画处理。 100 像素和 50 像素。默认高度为 50 像素。
我发现 Jquery 可以正确地完成此操作,但只有一个大禁忌。当鼠标在动画过程中退出然后再次进入时,它会:
a)堆叠动画,导致完成大量(例如 100 个)动画,而只需要一个。
b) 重置当前动画,这会导致意外行为,例如 div 消失、更改为锁定高度或在 100 到 50 像素之间上下闪烁。
对此有什么想法吗?
I'm trying to have the following:
There is a fixed positioned div on the bottom of my page. When the mouse enters and exits the div animates its height to resp. 100px and 50px. The default height is 50px.
I've found that Jquery does this correctly with only one big no-no. When the mouse exits while animating and then reenters again it either:
a) stacks the animations leading to a lot (say 100) animations being done, while only one was necessary.
b) resets current animations, which leads to unexpected behavior like the div disappearing, changing to a fixed height which locks down or glitching up and down between 100 and 50 pixels.
Any ideas on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您处理 mouseover 和 mouseout 事件时,应该使用 stop() 函数来清除动画队列。如果需要,它可以让您跳到动画的结尾(在开始另一个动画之前)。您还可以清除整个动画队列。
来自 jQuery API 文档:
When you handle your mouseover and mouseout events, you should use the stop() function to clear the animation queue. It will let you jump to the end of the animation (before you start another one) if needed. You can also clear the whole queue of animations.
From the jQuery API documentation :
与其在每个动作上触发动画事件,不如拥有一个不断轮询特定变量的函数,并通过移动或不移动一定的(变量)量来相应地采取行动。然后,您的
mouseover
和mouseout
事件将修改那些被轮询的变量,因此您不必担心触发数百个动画。如果我不明白,请告诉我,以便我可以澄清..Rather than firing off an animation event on every action, how about having a function that constantly polls specific variables, and acts accordingly - by moving or not moving a certain (variable) amount. Your
mouseover
andmouseout
events would then modify those variables that are polled, and so you wouldn't have to worry about triggering hundreds of animations. Let me know if I'm not making sense so I can clarify..到目前为止,我发现的唯一有效的解决方案是在动画制作之前为 mouseenter 和 mouseexit 动画提供 ClearQueue() ,但我认为这在某些情况下可能是不需要的。
在使用 animate() 的情况下,这工作得很好,但是使用 SlideUp 和其他默认的 JQuery 动画,它会导致不正确的行为。
The only solution I've found so far to work is to give both the mouseenter and mouseexit animation a ClearQueue() before animating, but I think this might be unwanted in some cases.
In the case of using animate() this works quite nicely, but with slideUp and other default JQuery animations it leads to improper behavior.