Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: interrupting fadeIn()/fadeOut()

Let's say I've called $element.fadeIn(200). 100 ms later, something happens on that page and I want to interrupt that fade and immediately fadeOut(). How can I do this?

If you call calling $element.fadeIn(200).fadeOut(0), the fadeOut() only happens after the fadeIn() has finished.

Also, is there a way I can examine $element to determine if a fadeIn() or fadeOut() is running? Does $element have any .data() member that changes?

like image 958
JamesBrownIsDead Avatar asked Sep 01 '10 17:09

JamesBrownIsDead


People also ask

What is fadeIn and fadeOut in jQuery?

The jQuery fadeToggle() method toggles between the fadeIn() and fadeOut() methods. If the elements are faded out, fadeToggle() will fade them in. If the elements are faded in, fadeToggle() will fade them out. Syntax: $(selector).

What is the syntax of jQuery fadeOut () method?

The jQuery fadeOut() method is used to fade out the element. Syntax: $(selector). fadeOut();

What is the difference between fadeOut and hide in jQuery?

fadeOut() the place will be removed at once. . show(duration) and . hide(duration) animate the size of element (also the opacity) to 100% and 0% and the place of elements is also animated in that duration.

Which are the jQuery fading methods?

jQuery example: fadeIn(), fadeOut(), and fadeToggle() This is an example using jQuery's fadeIn() , fadeOut() , and fadeToggle() methods to change the visibility of elements on the page with a fading effect.


2 Answers

stop() will only remove animations that are not executed yet.

use stop(true, true) to interrupt and remove the current animation too!

like image 51
Andre Schweighofer Avatar answered Oct 23 '22 19:10

Andre Schweighofer


You will get smooth fadeIn/Out effect by clearing queue but not jumping to the end, using .stop(true,false), but please notice that as FadeIn can be interrupted this way, FadeOut can not. I reported it as a bug like years ago, but noone cared. FadeIn only works if the object is hidden. But there is workaround... use FadeTo instead - it works on hidden as well as partially faded objects:

    $('.a').hover(function(){
    $('.b').stop(true,false).fadeTo(3000,1); // <- fadeTo(), not FadeIn() (!!!)
},function(){
    $('.b').stop(true,false).fadeOut(3000);
});

Here's how it works: http://jsfiddle.net/dJEmB/

like image 21
Flash Thunder Avatar answered Oct 23 '22 17:10

Flash Thunder