Open In App

jQuery fadeTo() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The fadeTo() method is an inbuilt method in jQuery that is used to change the opacity of the selected element. 

Syntax:

$(selector).fadeTo(speed, opacity, easing, call_function)

Here selector is the selected element. 

 

Parameter: It accepts four parameters which are specified below-

  • speed: It specifies the speed of the fading effect.
  • opacity: It specifies to fade and this value should be between 0.0 and 1.0.
  • easing: It specifies the speed at different points of animation.
  • call_function: It is an optional parameter and performs a function after performing fadeTo method.

Return Value: It does not return any value.

Example 1: In the below code, no optional function parameter is passed. 

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
    <style>
        body {
            width: 40%;
            height: 100px;
            border: 2px solid green;
            padding: 20px;
        }
    </style>
</head>
  
<body>
    <!-- Click on this paragraph and 
        this paragraph will fade out -->
    <p>
        This paragraph will fade out !
    </p>
    
    <!-- After clicking on this paragraph this 
        paragraph will not fade out -->
    <p>
        This paragraph will not fade !
    </p>
    
    <!-- jQuery code to show working 
        of this method -->
    <script>
        $("p:first").click(function () {
            $(this).fadeTo("slow", 0.33);
        });
    </script>
</body>
  
</html>


Output:

 

Example 2: In the below code, an optional function parameter is passed. 

HTML




<!DOCTYPE html>
<html>
  
<head>
    <script src=
    </script>
    
    <!-- jQuery code to show the working of this method -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $("p").fadeTo(2000, 0.2, function () {
                    alert("The fadeTo effect has finished!");
                });
            });
        });
    </script>
    <style>
        body {
            width: 40%;
            height: 100px;
            border: 2px solid green;
            padding: 20px;
        }
    </style>
</head>
  
<body>
    
    <!-- This paragraph will fade out -->
    <p>This is a paragraph.</p>
  
    <!-- Click on this button and 
        paragraph will fade out -->
    <button>Click me</button>
</body>
  
</html>


Output:

 



Last Updated : 03 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads