How to fade an element in and out using jQuery

fadeIn

The jQuery fadeIn() method is used to fade in a hidden element.

Syntax

$(selector).fadeIn(speed, callback);
  1. speed is an optional parameter used to specify the duration of any applied effect. It can take on values such as “slow”, “fast”, or any other specified integer value of speed (in milliseconds).

  2. callback is another optional parameter that refers to a function that a user may want to execute at the end of fading.

fadeOut

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

Syntax

$(selector).fadeOut(speed, callback);
  1. speed is an optional parameter used to specify the duration of any applied effect. It can take on values such as “slow”, “fast”, or any other specified integer value of speed (in milliseconds).

  2. callback is another optional parameter that refers to a function a user may want to execute at the end of fading.

Example

Let’s visualize the workings of fadeIn() and fadeOut():

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved