jQuery fadeOut() method is used to fade out the selected elements of the html page. It changes the opacity of the selected element to 0 (zero) and then changes the display to none for that element. In this guide, you will learn jQuery fadeOut() effect with examples.
jQuery fadeOut() Syntax
selector.fadeOut( speed, callback_function);
selector: It is used to select the html element on which this fade out effect is being applied.
speed: The speed of the fadeout effect in milliseconds. It is an optional parameter and can take values such as “slow” or “fast” or in milliseconds.
callback_function: It is also an optional parameter. This callback function executes when the fade out effect is complete. You can use this to set alert or display a message on the screen when fadeout effect is complete.
jQuery fadeOut() Effect Example
In the following example we are applying the fadeOut effect on the paragraphs by using paragraph selector ($(“p”)), we are calling the fadeOut() method inside button click function which means when the FadeOut button is clicked, the paragraph will fadeout as you can see in the output.
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").fadeOut(); }); }); </script> </head> <body> <h2>jQuery fadeOut effect example</h2> <p>This tutorial is published on beginnersbook.com. This paragraph will gradually fadeout when the FadeOut button is clicked.</p> <button>FadeOut</button> </body> </html>
Output:
Before FadeOut button is clicked:
After FadeOut button is clicked:
jQuery fadeOut() Effect Example with speed parameter
Speed can be specified in milliseconds. For example, here we have specified 2500 milliseconds speed for the fadeout effect.
$(document).ready(function(){ $("button").click(function(){ $("p").fadeOut(2500); }); });
Or you can specify the speed as “slow” or “fast” as shown below.
$(document).ready(function(){ $("button").click(function(){ $("p").fadeOut("slow"); }); });
jQuery fadeOut() Effect Example with speed and callback parameters
Lets take the first example and change the jQuery part. We are setting up an alert message in the callback function so that an alert message is displayed when the paragraph fades out.
$(document).ready(function(){ $("button").click(function(){ $("p").fadeOut("slow", function(){ alert("The paragraph is now hidden"); }); }); });
Output:
Leave a Reply