Toggle between fading in and fading out different boxes:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>/* w ww . j a va 2 s . co m*/ <script> $(document).ready(function(){ $("button").click(function(){ $("#div1").fadeToggle(); $("#div2").fadeToggle("slow"); $("#div3").fadeToggle(3000); }); }); </script> </head> <body> <p>Demonstrate fadeToggle() with different speed parameters.</p> <button>Click to fade in/out boxes</button><br><br> <div id="div1" style="width:80px;height:80px;background-color:red;"></div> <br> <div id="div2" style="width:80px;height:80px;background-color:green;"></div> <br> <div id="div3" style="width:80px;height:80px;background-color:blue;"></div> </body> </html>
The fadeToggle()
method toggles between the fadeIn()
and fadeOut()
methods.
If the elements are faded out, fadeToggle()
will fade them in.
If the elements are faded in, fadeToggle()
will fade them out.
Hidden elements will not be displayed at all.
They no longer affect the layout of the page.
$(selector).fadeToggle(speed,easing,callback)
Parameter | Optional | Description |
---|---|---|
speed | Optional. | speed of the fading effect. Default value is 400 milliseconds Possible values: milliseconds "slow" "fast" |
easing | Optional. | speed of the element in different points of the effect. Default value is "swing" Possible values: "swing" - moves slower at the beginning/end, faster in the middle "linear" - moves in a constant speed |
callback | Optional. | A function to be executed after the fadeToggle() method is completed |