Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

jQuery effect - fade in and out


May 07, 2021 jQuery


Table of contents


jQuery effect - fade in and out

In jQuery, there are four ways to achieve element fade-in and fade-out: fadeIn (), fadeOut (), fadeToggle (), and fadeTo (), and this article provides you with examples of how to use these four methods in jQuery.

With jQuery, you can achieve a fade-in effect for elements.

Click to show fade-in/fade-out panel

Because time is precious, we provide fast and convenient learning methods.

At W3CSchool, you can learn what you need.


jQuery fadeIn()
Demonstrates the jQuery fadeIn() method.

jQuery fadeOut()
Demonstrate the jQuery fadeOut() method.

jQuery fadeToggle()
Demonstrate the jQuery fadeToggle() method.

jQuery fadeTo()
Demonstrate the jQuery fadeTo() method.


jQuery Fading method

With jQuery, you can achieve a fade-in effect for elements.

jQuery has four fade methods:

  • fadeIn()
  • fadeOut()
  • fadeToggle()
  • fadeTo()

jQuery fadeIn() method

jQuery fadeIn() is used to fade in hidden elements.

Grammar:

$( selector ).fadeIn( speed,callback );

The optional speed parameter specifies the length of the effect. It can take the following values: "slow," "fast," or milliseconds.

The optional callback parameter is the name of the function executed after fading is complete.

The following example demonstrates the fadeIn() method with different parameters:

$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});

Try it out . . .


jQuery fadeOut() method

The jQuery fadeOut() method is used to fade out visible elements.

Grammar:

$( selector ).fadeOut( speed,callback );

The optional speed parameter specifies the length of the effect. It can take the following values: "slow," "fast," or milliseconds.

The optional callback parameter is the name of the function executed after fading is complete.

The following example demonstrates the fadeOut() method with different parameters:

$("button").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});

Try it out . . .


jQuery fadeToggle() method

The jQuery fadeToggle() method can be switched between the fadeIn() and fadeOut() methods.

If the element fades out, fadeToggle() adds a fade-in effect to the element.

FadeToggle() adds a fade-out effect to the element if it has faded in.

Grammar:

$( selector ).fadeToggle( speed,callback );

The optional speed parameter specifies the length of the effect. It can take the following values: "slow," "fast," or milliseconds.

The optional callback parameter is the name of the function executed after fading is complete.

The following example demonstrates the fadeToggle() method with different parameters:

$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});

Try it out . . .


jQuery fadeTo() method

The jQuery fadeTo() method allows the fade to become a given opacity (values between 0 and 1).

Grammar:

$( selector ).fadeTo( speed,opacity,callback );

The required speed parameter specifies the length of the effect. It can take the following values: "slow," "fast," or milliseconds.

The opacity parameter required in the fadeTo() method sets the fade-in effect to a given opacity (values between 0 and 1).

The optional callback argument is the name of the function executed after the function is completed.

The following example demonstrates the fadeTo() method with different parameters:

$("button").click(function(){
$("#div1").fadeTo("slow",0.15);
$("#div2").fadeTo("slow",0.4);
$("#div3").fadeTo("slow",0.7);
});

Try it out . . .

Read about it

jQuery Reference Manual - Effects