简体   繁体   English

如何在mouseover和mouseout上使用Jquery进行相对动画处理?

[英]How to do relative animating with Jquery on mouseover and mouseout?

I'm trying to have the following: 我正在尝试以下内容:

There is a fixed positioned div on the bottom of my page. 我的页面底部有一个固定位置的div。 When the mouse enters and exits the div animates its height to resp. 当鼠标进入和退出时,div将对其高度进行动画处理以分别显示。 100px and 50px. 100px和50px。 The default height is 50px. 默认高度为50像素。

I've found that Jquery does this correctly with only one big no-no. 我发现Jquery仅通过一个大的禁忌就能正确地做到这一点。 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. a)堆叠动画,从而完成很多(例如100个)动画,而只需要一个。

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. b)重置当前动画,这会导致意外行为,例如div消失,更改为固定高度,从而在100到50像素之间上下锁定或上下移动。

Any ideas on this? 有什么想法吗?

When you handle your mouseover and mouseout events, you should use the stop() function to clear the animation queue. 处理鼠标悬停和鼠标移出事件时,应使用stop()函数清除动画队列。 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 : 从jQuery API文档中:

The usefulness of the .stop() method is evident when we need to animate an element on mouseenter and mouseleave: 当需要在mouseenter和mouseleave上设置元素动画时,.stop()方法的有用性显而易见:

<div id="hoverme">
  Hover me
  <img id="hoverme" src="book.png" alt="" width="100" height="123" />
</div>

We can create a nice fade effect without the common problem of multiple queued animations by adding .stop(true, true) to the chain: 通过将.stop(true,true)添加到链中,我们可以创建一个不错的淡入淡出效果,而不会出现多个排队动画的常见问题:

$('#hoverme-stop-2').hover(function() {
  $(this).find('img').stop(true, true).fadeOut();
}, function() {
  $(this).find('img').stop(true, true).fadeIn();
});

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 and mouseout events would then modify those variables that are polled, and so you wouldn't have to worry about triggering hundreds of animations. 然后, mouseovermouseout事件将修改被轮询的那些变量,因此您不必担心触发数百个动画。 Let me know if I'm not making sense so I can clarify.. 如果我没有道理,请告诉我,以便澄清。

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. 到目前为止,我找到的唯一可行的解​​决方案是在动画制作之前为mouseenter和mouseexit动画都提供一个ClearQueue(),但是我认为在某些情况下这可能是不需要的。

In the case of using animate() this works quite nicely, but with slideUp and other default JQuery animations it leads to improper behavior. 在使用animate()的情况下,效果很好,但是使用slideUp和其他默认JQuery动画会导致不正确的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM