Basics Lesson 5 of 10
jQuery Effects — hide, show, fade, slide and animate
Show and hide elements with jQuery effects — hide, show, toggle, fadeIn, fadeOut, slideUp, slideDown, slideToggle and animate — with speeds and callbacks.
jQuery's built-in effects made smooth show-and-hide animations easy long before CSS transitions were common. They're still handy for quick interactions like FAQs and notifications.
hide, show and toggle
<script src="/vendor/jquery.min.js"></script>
<button id="h">Hide</button> <button id="s">Show</button> <button id="t">Toggle</button>
<p id="box" style="padding:12px;background:#e3f1ec;border-radius:8px">📚 Quarterly exams start Monday.</p>
<script>
$("#h").on("click", () => $("#box").hide(400));
$("#s").on("click", () => $("#box").show(400));
$("#t").on("click", () => $("#box").toggle(400));
</script>
Without a number, hide() and show() are instant. A speed in milliseconds (or "slow" / "fast") animates them.
Fade and slide
<script src="/vendor/jquery.min.js"></script>
<button id="fo">Fade out</button> <button id="fi">Fade in</button> <button id="st">Slide toggle</button>
<div id="card" style="margin-top:10px;padding:12px;background:#fdf3dc;border-radius:8px;width:240px">
🥖 Fresh pandesal available until 9 AM!
</div>
<script>
$("#fo").on("click", () => $("#card").fadeOut("slow"));
$("#fi").on("click", () => $("#card").fadeIn("slow"));
$("#st").on("click", () => $("#card").slideToggle(300));
</script>
| Method | Effect |
|---|---|
fadeIn, fadeOut, fadeToggle | Changes opacity |
fadeTo(speed, 0.5) | Fades to a specific opacity |
slideDown, slideUp, slideToggle | Changes height, like a drawer |
An FAQ with slideToggle
<script src="/vendor/jquery.min.js"></script>
<style>
.q { background: #0f5c4d; color: white; padding: 10px; margin-top: 6px; cursor: pointer; border-radius: 6px; font-family: sans-serif; }
.a { display: none; padding: 10px; border: 1px solid #ddd; font-family: sans-serif; }
</style>
<div class="q">When is enrollment?</div>
<div class="a">June 2 to June 13, 8 AM to 4 PM.</div>
<div class="q">What should I bring?</div>
<div class="a">Report card (SF9), PSA birth certificate and two 1x1 photos.</div>
<script>
$(".q").on("click", function () {
$(".a").not($(this).next()).slideUp(200); // close the others
$(this).next(".a").slideToggle(200);
});
</script>
Callbacks
Code after an effect runs immediately, not after the animation. To run something when the effect finishes, pass a callback:
$("#msg").fadeOut(400, function () {
$(this).text("Saved!").fadeIn(400);
});
animate
<script src="/vendor/jquery.min.js"></script>
<div style="position:relative;height:120px;background:#bde3f5;border-radius:8px;overflow:hidden">
<img id="jeep" src="/img/jeepney.svg" alt="Jeepney" style="position:absolute;left:0;bottom:8px;width:110px">
</div>
<button id="go">Drive!</button> <button id="back">Back</button>
<script>
$("#go").on("click", () => $("#jeep").stop().animate({ left: "65%" }, 1500));
$("#back").on("click", () => $("#jeep").stop().animate({ left: 0 }, 800));
</script>
animate changes numeric CSS properties (left, width, opacity, font-size…) smoothly. .stop() stops the current animation, so fast repeated clicks don't pile up a long queue of animations.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04