Salah satu alasan jQuery begitu populer di masa jayanya adalah kemudahan membuat animasi dan efek. Dengan beberapa baris kode jQuery, kamu bisa membuat elemen muncul perlahan, slide ke atas/bawah, atau animasi custom yang kompleks — tanpa perlu memahami detail CSS animation secara mendalam.
Show, Hide, dan Toggle
// Langsung (tanpa animasi)
$("#box").show();
$("#box").hide();
$("#box").toggle();
// Dengan durasi
$("#box").show(300); // 300 milidetik
$("#box").hide("slow"); // ~600ms
$("#box").show("fast"); // ~200ms
$("#box").toggle(500);
// Dengan callback — dijalankan setelah animasi selesai
$("#box").hide(400, function() {
console.log("Sudah tersembunyi!");
$(this).remove();
});
Fade Effects
// Fade in dan out (ubah opacity)
$("#box").fadeIn();
$("#box").fadeIn(500);
$("#box").fadeOut();
$("#box").fadeOut(300, function() {
$(this).remove(); // hapus setelah fade selesai
});
$("#box").fadeToggle(400);
// FadeTo — fade ke opacity tertentu
$("#box").fadeTo(500, 0.5); // fade ke 50% opacity dalam 500ms
$("#box").fadeTo("slow", 0.2);
Slide Effects
// Slide up (collapse) dan slide down (expand)
$("#panel").slideDown(); // tampilkan dengan slide dari atas
$("#panel").slideDown(400);
$("#panel").slideUp(); // sembunyikan dengan slide ke atas
$("#panel").slideToggle(300); // toggle antara slideDown dan slideUp
// Contoh praktis: accordion
$(".accordion-header").on("click", function() {
const content = $(this).next(".accordion-content");
content.slideToggle(300);
// Tutup yang lain
$(".accordion-content").not(content).slideUp(300);
});
.animate() — Animasi Custom
animate() memungkinkan animasi pada hampir semua properti CSS numerik:
// Format: .animate(properti, durasi, easing, callback)
$("#box").animate({
width: "300px",
height: "200px",
opacity: 0.8,
marginLeft: "50px"
}, 600);
// Dengan easing dan callback
$("#box").animate({
left: "+=100px", // += berarti relatif dari posisi saat ini
top: "-=50px"
}, {
duration: 800,
easing: "swing", // "linear" atau "swing" (bawaan jQuery)
complete: function() {
console.log("Animasi selesai!");
}
});
// Animasi berurutan (queue)
$("#box")
.animate({ left: "200px" }, 500)
.animate({ top: "100px" }, 500)
.animate({ opacity: 0 }, 300, function() {
$(this).remove();
});
Mengontrol Animasi
// Hentikan animasi yang sedang berjalan
$("#box").stop(); // hentikan, tetap di posisi saat ini
$("#box").stop(true); // hentikan dan kosongkan antrian animasi
$("#box").stop(true, true); // hentikan dan lompat ke akhir animasi
// Contoh: stop sebelum hover animasi baru agar tidak menumpuk
$(".card").hover(
function() {
$(this).stop(true).animate({ marginTop: "-5px" }, 200);
},
function() {
$(this).stop(true).animate({ marginTop: "0px" }, 200);
}
);
// Delay sebelum animasi
$("#box").delay(1000).fadeOut(500);
// Cek apakah sedang animasi
if ($("#box").is(":animated")) {
console.log("Sedang animasi");
}
Easing dengan jQuery UI
jQuery bawaan hanya punya linear dan swing. Untuk easing yang lebih kaya, gunakan jQuery UI atau plugin jQuery Easing:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.min.js"></script>
// Dengan plugin easing
$("#box").animate(
{ left: "300px" },
{ duration: 600, easing: "easeOutBounce" }
);
// Pilihan easing: easeInQuad, easeOutBounce, easeInOutElastic, dll.
Contoh Praktis: Loading Spinner
function tampilkanLoading() {
$("body").append('<div id="overlay"><div class="spinner"></div></div>');
$("#overlay").hide().fadeIn(300);
}
function sembunyikanLoading() {
$("#overlay").fadeOut(300, function() {
$(this).remove();
});
}
// Penggunaan
tampilkanLoading();
$.ajax("/api/data")
.done(data => renderData(data))
.always(() => sembunyikanLoading());
Contoh Praktis: Smooth Scroll ke Anchor
$("a[href^='#']").on("click", function(e) {
e.preventDefault();
const target = $(this.getAttribute("href"));
if (!target.length) return;
$("html, body").animate({
scrollTop: target.offset().top - 80 // offset untuk navbar fixed
}, 700, "swing");
});
Contoh Praktis: Notification Toast
function tampilkanNotif(pesan, tipe = "info") {
const warna = { sukses: "#28a745", error: "#dc3545", info: "#17a2b8" };
const toast = $(`
<div class="toast" style="
background: ${warna[tipe]};
color: white;
padding: 12px 20px;
border-radius: 4px;
margin-bottom: 10px;
">${pesan}</div>
`);
$("#toast-container").append(toast);
toast.hide().slideDown(300);
setTimeout(() => {
toast.slideUp(300, function() { $(this).remove(); });
}, 3000);
}
// Penggunaan
tampilkanNotif("Data berhasil disimpan!", "sukses");
tampilkanNotif("Terjadi kesalahan!", "error");
jQuery Effects vs CSS Transitions
| Aspek | jQuery Effects | CSS Transitions/Animations |
|---|---|---|
| Kemudahan | Sangat mudah | Butuh pengetahuan CSS |
| Performa | Lebih lambat (JS thread) | Lebih cepat (GPU) |
| Kontrol via JS | Penuh | Terbatas |
| Callback | Mudah | Butuh animationend event |
| Rekomendasi | Proyek legacy | Proyek baru |
Untuk proyek modern, CSS transitions/animations lebih direkomendasikan karena berjalan di GPU dan lebih performant. jQuery effects cocok untuk proyek yang sudah menggunakan jQuery.
Kesimpulan
jQuery menyediakan cara yang sangat mudah untuk menambahkan animasi dan efek ke halaman web. Dengan fadeIn, slideToggle, dan animate(), kamu bisa membuat UI yang responsif dan menarik tanpa banyak kode.
Di artikel berikutnya, kita bahas jQuery AJAX — cara mengambil dan mengirim data ke server menggunakan jQuery yang sangat disederhanakan.