jQuery fadeTo() Method

You are Here:

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
speedRequiredSpecifies the speed of the fading effect.
Possible values are
  • "slow"
  • "fast"
  • milliseconds
opacityRequiredSpecifies the opacity to fade to. Possible values are a number between 0 and 1.
easingOptionalSpecifies the easing function to use for the transition.
Default value is "swing"
Possible values are
  • "swing"
  • "linear"
callbackOptionalSpecifies a function to be called once the animation is completed.

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>

Reminder

Hi Developers, we almost covered 99.5% of jQuery Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in jQuery.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author