Frequently used effects are built into jQuery as methods:
Animate the opacity of the selected elements to 100%.
$.fn.fadeOutAnimate the opacity of the selected elements to 0%.
$.fn.slideDownDisplay the selected elements with a vertical sliding motion.
$.fn.slideUpHide the selected elements with a vertical sliding motion.
$.fn.slideToggleExample 6.2. Setting the duration of an effect
$('h1').fadeIn(300); // fade in over 300ms $('h1').fadeOut('slow'); // using a built-in speed definition
speeds: { slow: 600, fast: 200, // Default speed _default: 400 }
Example 6.5. Run a callback even if there were no elements to animate
var $thing = $('#nonexistent'); var cb = function() { console.log('done!'); }; if ($thing.length) { $thing.fadeIn(300, cb); } else { cb(); }
Example 6.6. Custom effects with $.fn.animate
$('div.funtimes').animate( { left : "+=50", opacity : 0.25 }, 300, // duration function() { console.log('done!'); // calback });
Note
Color-related properties cannot be animated with $.fn.animate
using jQuery out of the box. Color animations can easily be accomplished by including the color plugin. We'll discuss using plugins later in the book.
As of jQuery 1.4, it is possible to do per-property easing when using the $.fn.animate method.
jQuery provides several tools for managing animations.
Stop currently running animations on the selected elements.
$.fn.delayWait the specified number of milliseconds before running the next animation.
$('h1').show(300).delay(1000).hide(300);jQuery.fx.off