jQuerybeginner 6 min
Show, hide, fade and slide elements, and animate numeric CSS properties.
How to use .hide(), .show(), .fadeIn(), .slideToggle() and .animate().
Small movements help people notice change: a menu sliding open, a message fading in. jQuery made these a one-liner long before CSS had good animation support.
Built-in effects:
.show(), .hide(), .toggle().fadeIn(), .fadeOut(), .fadeToggle().slideDown(), .slideUp(), .slideToggle()Each takes an optional duration in milliseconds, or the words "fast" and "slow".
.animate({ property: value }, duration) animates numeric CSS properties such as width, height, opacity and left.
animate changes numeric properties gradually. It cannot animate colours without a plugin.Add a button that calls $("#box").fadeToggle().
color with .animate()..stop() first.display: none).show/hide/toggle, fade* and slide* are quick effects..animate() changes numeric CSS properties over time.You finished the jQuery beginner section. Next: AJAX and when to use jQuery versus modern JavaScript.
<style>
#box { width: 80px; height: 80px; background: royalblue; margin-top: 10px; }
</style>
<button id="toggle">Toggle</button>
<button id="grow">Grow</button>
<div id="box"></div>
<script>
$(function () {
$("#toggle").on("click", function () {
$("#box").slideToggle(400);
});
$("#grow").on("click", function () {
$("#box").animate({ width: "240px" }, 600);
});
});
</script>