jQuery -

Fade

The jQuery .fadeIn(), .fadeOut(), .fadeToggle(), and .fadeTo() methods allow you to create smooth fade effects for your elements. This tutorial covers the different aspects of using these methods with detailed explanations and examples.


1. Basic Fade In

You can use the .fadeIn() method to gradually show an element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .fadeIn() Method - Basic</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeInButton").click(function(){
                $("p").fadeIn();
            });
        });
    </script>
</head>
<body>
    <p style="display:none;">This is a hidden paragraph.</p>
    <button id="fadeInButton">Fade In Paragraph</button>
</body>
</html>
        
    

This example demonstrates how to fade in a paragraph when a button is clicked using the .fadeIn() method.


2. Basic Fade Out

You can use the .fadeOut() method to gradually hide an element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .fadeOut() Method - Basic</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeOutButton").click(function(){
                $("div").fadeOut();
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="fadeOutButton">Fade Out Div</button>
</body>
</html>
        
    

This example demonstrates how to fade out a div when a button is clicked using the .fadeOut() method.


3. Fade Toggle

You can use the .fadeToggle() method to toggle between fading in and fading out an element.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .fadeToggle() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeToggleButton").click(function(){
                $("div").fadeToggle();
            });
        });
    </script>
    <style>
        div {
            background-color: yellow;
            height: 100px;
            width: 100px;
        }
    </style>
</head>
<body>
    <div>This is a div.</div>
    <button id="fadeToggleButton">Fade Toggle</button>
</body>
</html>
        
    

This example demonstrates how to toggle the fade effect on a div using the .fadeToggle() method.



4. Fade To

You can use the .fadeTo() method to adjust the opacity of an element to a specified value.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .fadeTo() Method</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeToButton").click(function(){
                $("div").fadeTo("slow", 0.5);
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="fadeToButton">Fade Div to 50% Opacity</button>
</body>
</html>
        
    

This example shows how to fade an element to 50% opacity using the .fadeTo() method.


5. Fade In with Duration

You can specify the duration for the fade-in effect to make the transition smoother.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .fadeIn() Method - Duration</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeInButton").click(function(){
                $("div").fadeIn(1000);
            });
        });
    </script>
</head>
<body>
    <div style="display:none; background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="fadeInButton">Fade In Div with Duration</button>
</body>
</html>
        
    

This example demonstrates how to fade in a div with a specified duration.


6. Fade Out with Duration

You can specify the duration for the fade-out effect to make the transition smoother.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .fadeOut() Method - Duration</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeOutButton").click(function(){
                $("div").fadeOut(1000);
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="fadeOutButton">Fade Out Div with Duration</button>
</body>
</html>
        
    

This example shows how to fade out a div with a specified duration.


7. Fade Toggle with Duration

You can specify the duration for the fade toggle effect to make the transition smoother.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .fadeToggle() Method - Duration</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeToggleButton").click(function(){
                $("div").fadeToggle(1000);
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="fadeToggleButton">Toggle Fade Div with Duration</button>
</body>
</html>
        
    

This example demonstrates how to toggle the fade effect on a div with a specified duration.



8. Fade To with Duration

You can specify the duration for the fade-to effect to make the transition smoother.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .fadeTo() Method - Duration</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeToButton").click(function(){
                $("div").fadeTo(1000, 0.5);
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="fadeToButton">Fade Div to 50% Opacity with Duration</button>
</body>
</html>
        
    

This example shows how to fade an element to 50% opacity with a specified duration.


9. Fade In with Callback

You can execute a callback function after the fade-in effect is completed.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .fadeIn() Method - Callback</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeInButton").click(function(){
                $("p").fadeIn("slow", function(){
                    alert("The paragraph is now visible.");
                });
            });
        });
    </script>
</head>
<body>
    <p style="display:none;">This is a hidden paragraph.</p>
    <button id="fadeInButton">Fade In Paragraph with Callback</button>
</body>
</html>
        
    

This example demonstrates how to execute a callback function after fading in an element.


10. Fade Out with Callback

You can execute a callback function after the fade-out effect is completed.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .fadeOut() Method - Callback</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeOutButton").click(function(){
                $("div").fadeOut("slow", function(){
                    alert("The div is now hidden.");
                });
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="fadeOutButton">Fade Out Div with Callback</button>
</body>
</html>
        
    

This example shows how to execute a callback function after fading out an element.


11. Fade Toggle with Callback

You can execute a callback function after the fade toggle effect is completed.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .fadeToggle() Method - Callback</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeToggleButton").click(function(){
                $("div").fadeToggle("slow", function(){
                    alert("The div has been toggled.");
                });
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="fadeToggleButton">Toggle Fade Div with Callback</button>
</body>
</html>
        
    

This example demonstrates how to execute a callback function after toggling the fade effect on an element.


12. Fade To with Callback

You can execute a callback function after the fade-to effect is completed.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .fadeTo() Method - Callback</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("#fadeToButton").click(function(){
                $("div").fadeTo("slow", 0.5, function(){
                    alert("The div is now at 50% opacity.");
                });
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">This is a div element.</div>
    <button id="fadeToButton">Fade Div to 50% Opacity with Callback</button>
</body>
</html>
        
    

This example shows how to execute a callback function after fading an element to 50% opacity.



13. Fade In on Hover

You can use the .hover() method to fade in elements when a user hovers over them.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .hover() Method - Fade In</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("div").hover(function(){
                $(this).fadeIn();
            });
        });
    </script>
</head>
<body>
    <div style="display:none; background-color: yellow; padding: 10px;">Hover over this div to fade it in.</div>
</body>
</html>
        
    

This example demonstrates how to fade in an element when the user hovers over it.


14. Fade Out on Hover

You can use the .hover() method to fade out elements when a user hovers over them.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .hover() Method - Fade Out</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("div").hover(function(){
                $(this).fadeOut();
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">Hover over this div to fade it out.</div>
</body>
</html>
        
    

This example shows how to fade out an element when the user hovers over it.


15. Fade Toggle on Click

You can use the .click() method to toggle the fade effect on elements when a user clicks on them.

Try yourself
        
            <!DOCTYPE html>
<html>
<head>
    <title>jQuery .click() Method - Fade Toggle</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
            $("div").click(function(){
                $(this).fadeToggle();
            });
        });
    </script>
</head>
<body>
    <div style="background-color: yellow; padding: 10px;">Click on this div to toggle its fade.</div>
</body>
</html>
        
    

This example demonstrates how to toggle the fade effect on an element when the user clicks on it.