jQuery facilitates with fading effect to fade an element in and out of visibility.
There are various fading methods available in jQuery. These are:
- fadeIn()
- fadeOut()
- fadeToggle()
- fadeTo()
fadeIn() method is used to fade in the hidden elements.
Example:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("#div1").fadeIn(); }); }); </script> </head> <body> <p>Knock Knock. Click to let me In.</p> <button>FadeIn</button><br><br> <div id="div1" style="width:90px;height:45px;display:none;background-color:magenta;"></div><br> </body> </html>
Parameters like speed and callback can also be used in the fadeIn() method.
Syntax:
$(selector).fadeIn(speed, callback);
Speed:
- Speed is an optional parameter that specifies the speed of the delay in reappearing the selected elements.
- It can accept string values: “slow”, “fast”, or in milliseconds.
- The default speed is 400 milliseconds.
- On using “slow” keyword as the value for the speed parameter, the compiler takes it as 600 milliseconds.
- Similarly, on using “fast” keyword as the value for the speed parameter, the compiler takes it as 200 milliseconds.
Callback:
- Callback is also an optional parameter.
- Callback is the function to be executed once the fadeIn() method’s execution is complete.
Example:
<head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("#div1").fadeIn(); $("#div2").fadeIn(3000); $("#div3").fadeIn(6000); }); }); </script> </head> <body> <p>Knock Knock. Click to let us In.</p> <button>FadeIn</button><br><br> <div id="div1" style="width:80px;height:80px;display:none;background-color:red;border-radius: 70px"></div><br> <div id="div2" style="width:80px;height:80px;display:none;background-color:green;border-radius: 70px"></div><br> <div id="div3" style="width:80px;height:80px;display:none;background-color:blue;border-radius: 70px"></div> </body> </html>