jQuery Tutorial
In jQuery, fadeTo method is used to gradually change the opacity of the selected element in the web page.
jquery fadeTo syntax
$(selector).fadeTo(speed of animation, opacity, easing, callback function)
In jQuery speed and opacity are the required parameters and remaining other parameters are optional for fadeTo method.
"fast" - shows the element faster.
"normal" - shows the element in normal.
"slow" - shows the element slower.
milliseconds - shows the element animation as based on specified milliseconds.
Opacity changes value to be display the faded element and value should be between 0.00 ( transparent ) to 1.00 ( opaque ).
"swing" - In middle of the animation is faster but slower in beginning and end of animation.
"linear" - Animation is consistent from beginning to end.
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta charset="utf-8" /> <title>jQuery FadeTo method</title> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> <script type="text/javascript"> $(document).ready(function () { $('#btnFadeTo').click(function () { $("#divTitle").fadeTo(5000, 0.3, "linear", function () { alert("Fade To example!") }); }); }); </script> </head> <body> <input type="button" id="btnFadeTo" value="Fade To"/> <div id="divTitle" style="height:200px;width:200px;background-color:blue;color:white;"> div element fadeTo testing! </div> </body> </html>Output:
Hidden div element
Fades div after clicking on Fade To button
callback function is used to display the alert message once fades the div element for required opacity value when clicking on Fade To button.
$(document).ready(function () { $('#btnFadeTo').click(function () { $("div").fadeTo("slow", 0.2, function () { alert('callback function!'); }); }); });
Fades the div element with opacity changes for 10000 milliseconds, animation mode is swing (In middle of animation is faster) and callback function is used to display alert message once displayed faded div with specified opacity value.
$(document).ready(function () { $('#btnFadeTo').click(function () { $("div").fadeTo(10000,0.3,"swing", function () { alert('fadeTo example!') }); }); });« Previous Next »
jQuery Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page