jQuery Effects
jQuery effects are built-in methods that show, hide, fade, slide and animate elements with a single line of code. They were one of the main reasons jQuery became popular, because they made smooth visual transitions easy long before CSS transitions were widely supported. You still meet them when maintaining existing sites, and they remain handy for quick interactive touches like collapsible panels and notification messages.
The basic effect methods
| Group | Methods | What changes |
|---|---|---|
| Show and hide | .show(), .hide(), .toggle() | Width, height and opacity together; instant with no duration |
| Fading | .fadeIn(), .fadeOut(), .fadeToggle(), .fadeTo() | Opacity only |
| Sliding | .slideDown(), .slideUp(), .slideToggle() | Height only, like a drawer |
| Custom | .animate() | Any numeric CSS property you choose |
| Control | .stop(), .delay(), .finish() | The animation queue |
Each method accepts an optional duration: a number of milliseconds or the keywords 'slow' (600ms) and 'fast' (200ms). The default is 400ms. You can also pass an easing name and a callback function that runs when the effect finishes.
Effects in a real page
$(function () {
// FAQ accordion: slide the answer open or closed
$('.faq-question').on('click', function () {
$(this).next('.faq-answer').slideToggle(250);
});
// Show a message, wait, then fade it out and remove it
$('#save').on('click', function () {
$('<div class="toast">Saved!</div>')
.appendTo('body')
.hide()
.fadeIn('fast')
.delay(2000)
.fadeOut(400, function () {
$(this).remove(); // callback after fading out
});
});
// Custom animation with chaining
$('#box').on('mouseenter', function () {
$(this).stop(true).animate({ width: 300, opacity: 0.8 }, 300);
}).on('mouseleave', function () {
$(this).stop(true).animate({ width: 150, opacity: 1 }, 300);
});
});
Queues, chaining and callbacks
Effects on the same element are placed in a queue and run one after another, which is why the toast example can chain fadeIn, delay and fadeOut neatly. However, code written after an effect does not wait for it. If you need to do something once an animation ends, such as removing an element, put it in the callback. When animating several elements and waiting for all of them, .promise().done(...) runs once every queued effect has finished.
.animate() works with numeric properties like width, height, left, opacity and font-size. It cannot animate colours on its own; that needs jQuery UI or a colour plugin, or simply a CSS transition.
The pile-up problem and other gotchas
Moving the mouse in and out of an element quickly adds a new animation to the queue each time. The element then keeps bouncing long after the mouse has gone. Calling .stop(true) before starting a new animation clears the queue and stops the current one, as in the hover example. .finish() instead jumps every queued animation straight to its end.
A surprise for some developers is that effects simply do not exist in the jQuery slim build. Slim leaves out the effects and AJAX modules to save size, so calling .fadeIn() throws "is not a function". If you see that error, check whether the script tag loads jquery.slim.min.js and switch to the full build.
Finally, for new projects, many simple fades and slides are smoother as CSS transitions toggled by adding a class, with jQuery or plain JavaScript only switching the class.
Takeaway
Use show/hide, fade* and slide* for common transitions, animate() for numeric properties, callbacks for code that must wait, and stop(true) to prevent queue build-up on repeated events.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.