English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery fadeTo() Effect Method

jQuery Effect Methods

The fadeTo() method gradually changes the opacity of the selected element to the specified opacity.

This method is similar tofadeIn()The method is similar, but this method cancels the hiding of the element and always fades in100% opacity.

Note: The fadeTo() method does not change the page layout (the selected element will still occupy the same space as before).

Syntax:

$(selector).fadeTo(duration, opacity, easing, callback)

Example

Gradually decrease the opacity of the <div> element:

$("button").click(function(){
  $("div").fadeTo(500, 0.2);
});
Test to see‹/›

Gradually change the opacity of the <div> element:

$("#btn1.click(function(){
  $("div").fadeTo(1000, 0);
});
$("#btn2.click(function(){
  $("div").fadeTo(1000, 1);
});
Test to see‹/›

UsingcallbackParameters:

$("button").click(function(){
  $("div").fadeTo(500, 0, function(){
    alert("Opacity decreased!!!");
  });
});
Test to see‹/›

Parameter Value

ParameterDescription
durationA string or number specifying how long the fade in effect will last

Possible values:

  • milliseconds (for example100,500,2000, etc.

  • "fast"

  • "slow"

opacityspecifies the opacity of the fade in. It must be between 0.00 and1a number between .00
easing(Optional) A string that specifies the speed of the element at different points in the animation. The default value is "swing"

Possible values:

  • "swing"-at the beginning/moves slower at the end and faster in the middle

  • "linear"-moves at a constant speed

callback(Optional) The fadeTo() method is completed, and the function called

jQuery Effect Methods