Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery sequence fadeOut and then remove

Tags:

jquery

I try $('somediv').fadeOut.remove(); but it only remove it, bang... it dont wait for the nice fadeOut, and THEN remove

why.. how to respect fadeout, and then remove..

like image 726
menardmam Avatar asked Jan 26 '11 23:01

menardmam


People also ask

What is fadeIn and fadeOut in jQuery?

jQuery fadeIn() Method The fadeIn() method gradually changes the opacity, for selected elements, from hidden to visible (fading effect). Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: This method is often used together with the fadeOut() method.

How many methods are available in jQuery to give fade effect to the elements?

JQuery offers four fading methods that allow you to change the transparency of elements. These methods include, fadeIn(), fadeOut(), fadeToggle(), and fadeTo().

How do you fadeOut an element in jQuery?

The jQuery fadeOut() method is used to fade out a visible element. Syntax: $(selector). fadeOut(speed,callback);

How does jQuery fadeOut work?

The . fadeOut() method animates the opacity of the matched elements. Once the opacity reaches 0, the display style property is set to none , so the element no longer affects the layout of the page. Durations are given in milliseconds; higher values indicate slower animations, not faster ones.


1 Answers

Use a callback:

$('somediv').fadeOut( function() { $(this).remove(); });

The code in the callback function you're passing to fadeOut()(docs) will not execute until the animation is complete.

Example: http://jsfiddle.net/p2LWE/

An alternative would be to queue()(docs) the remove()(docs) , but I think the callback is better.

$('somediv').fadeOut()
            .queue(function(nxt) { 
                $(this).remove();
                nxt();
            });
like image 85
user113716 Avatar answered Nov 15 '22 23:11

user113716