Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fadeOut() and slideUp() at the same time?

I have found jQuery: FadeOut then SlideUp and it's good, but it's not the one.

How can I fadeOut() and slideUp() at the same time? I tried two separate setTimeout() calls with the same delay but the slideUp() happened as soon as the page loaded.

Has anyone done this?

like image 290
Matthew Avatar asked Mar 05 '10 14:03

Matthew


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 does jQuery fadeOut do?

jQuery fadeOut() method is used to fade out the selected elements of the html page. It changes the opacity of the selected element to 0 (zero) and then changes the display to none for that element.

What is fadeOut in Javascript?

The fadeOut() method gradually changes the opacity, for selected elements, from visible to hidden (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 fadeIn() method.

What is slideUp in jQuery?

The slideUp() is an inbuilt method in jQuery which is used to hide the selected elements. Syntax: $(selector). slideUp(speed); Parameter: It accepts an optional parameter “speed” which specifies the speed of the duration of the effect.


2 Answers

You can do something like this, this is a full toggle version:

$("#mySelector").animate({ height: 'toggle', opacity: 'toggle' }, 'slow'); 

For strictly a fadeout:

$("#mySelector").animate({ height: 0, opacity: 0 }, 'slow'); 
like image 96
Nick Craver Avatar answered Sep 20 '22 11:09

Nick Craver


Directly animating height results in a jerky motion on some web pages. However, combining a CSS transition with jQuery's slideUp() makes for a smooth disappearing act.

const slideFade = (elem) => {    const fade = { opacity: 0, transition: 'opacity 400ms' };    elem.css(fade).slideUp();    };  slideFade($('#mySelector')); 

Fiddle with the code:
https://jsfiddle.net/00Lodcqf/435

bye

In some situations, a very quick 100 millisecond pause to allow more fading creates a slightly smoother experience:

   elem.css(fade).delay(100).slideUp(); 

This is the solution I used in the dna.js project where you can view the code (github.com/dnajs/dna.js) for the dna.ui.slideFade() function to see additional support for toggling and callbacks.

like image 39
Dem Pilafian Avatar answered Sep 19 '22 11:09

Dem Pilafian