views:

45

answers:

3

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?

+1  A: 

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 :

The usefulness of the .stop() method is evident when we need to animate an element on mouseenter and mouseleave:

<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:

$('#hoverme-stop-2').hover(function() {
  $(this).find('img').stop(true, true).fadeOut();
}, function() {
  $(this).find('img').stop(true, true).fadeIn();
});
Hugo Migneron
is this the same behavior as clearQueue?
xaddict
the .stop() method is meant to be used only with animations, .clearQueue() can also be used to remove any function that has been added to a generic jQuery queue with the .queue() method.
Hugo Migneron
after checking the Jquery docs I'd say that clearQueue, in the case of animate(), is the same as stop(true,false). Thanks!
xaddict
by the way, I think the example given in the JQuery docs works but is also a pretty weird implementation as the user is able to reposition the square in substeps. This way you can start at 0px and never return there because you've clicked the STOP-button somewhere in between moves
xaddict
A: 

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. Let me know if I'm not making sense so I can clarify..

Jeriko
isn't this very cpu consuming? given that polling even works when another tab is viewed or the browser is minimized.
xaddict
Depends how often you do it. You could poll every few seconds, and if there are tasks to be managed you temporarily increase polling rate.. I dunno, it seemed like the best way to handle the animation-queuing problem, and it would end up pretty responsive. Maybe calling `stop()` on the animations might be better - just my 2 cents :P
Jeriko
A: 

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.

xaddict