jQuery fadeTo() Method

Last Updated:

jQuery fadeTo() Method

The jQuery fadeTo() method will adjust the opacity of the matched elements.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery fadeTo() Method</h1> <button id="myFadeTo">Fade To</button> <script> $(document).ready(function(){ $("#myFadeTo").click(function(){ $("p").fadeTo(500, 0.3); }); }); </script> </body> </html>

Syntax

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

Parameter Values

ValueTypeExplanation
speed Required Specifies the speed of the fading effect.
Possible values are
  • "slow"
  • "fast"
  • milliseconds
opacity Required Specifies the opacity to fade to. Possible values are a number between 0 and 1.
easing Optional Specifies the easing function to use for the transition.
Default value is "swing"
Possible values are
  • "swing"
  • "linear"
callback Optional Specifies a function to be called once the animation is completed.

Browser Support

Devicedesktopmobile
Browsergoogle chromesafarifirefoxinternet Exploreredgeoperagoogle chromesafarifirefoxedgeoperaandroid webviewsamsung internet
fadeTo()YesYesYesYesYesYesYesYesYesYesYesYesYes

fadeTo() with Easing

In the following example, we will specify the easing function for fadeTo() method to linear.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery fadeTo() Method</h1> <button id="myFadeTo">Fade To</button> <script> $(document).ready(function(){ $("#myFadeTo").click(function(){ $("p").fadeTo(500, 0.3, "linear"); }); }); </script> </body> </html>

fadeTo() with Callback

In the following example, we will call a function (myFunction) once the fading effect for fadeTo() method is finished.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery fadeTo() Method</h1> <p>fadeTo() with callback.</p> <button id="myFadeTo">Fade To</button> <script> $(document).ready(function(){ $("#myFadeTo").click(function(){ $("p").fadeTo(500, 0.3, "linear", myFunction); }); }); function myFunction(){ alert("fadeTo is completed"); } </script> </body> </html>

Share this Page

Meet the Author