الرئيسية / تعلّم / متى أستخدم CSS transition أو animation أو WAAPI أو GSAP؟

متى أستخدم CSS transition أو animation أو WAAPI أو GSAP؟

أربع طرق لتحريك الواجهة، ولكل منها مكانها المناسب. نكتب نفس الحركة (ظهور مع انزلاق) بالطرق الأربع لتقارن الشكل والتحكم.

الديمو هنا بيشغّل الطرق الثلاث الأولى بدون تحميل أي ملف خارجي. صندوق GSAP بيشتغل في الكود لما تضيف مكتبة GSAP في صفحتك (الكود تحت فيه رابط المكتبة).

الخطوات

  1. CSS transition: للانتقال بين حالتين (hover، فتح/إغلاق). الأبسط والأقصر ولا يحتاج جافاسكربت إلا لتبديل الصنف.
  2. CSS animation (@keyframes): لحركة من عدة مراحل أو تتكرر أو تبدأ وحدها عند التحميل.
  3. Web Animations API (element.animate): نفس محرك CSS لكن من جافاسكربت، فتحصل على play() و pause() و reverse() وعلى وعد finished دون أي مكتبة.
  4. GSAP: مكتبة خارجية للتسلسلات الطويلة (timelines) والتحكم الدقيق وحركات SVG والتمرير. تضيف حجماً للصفحة، فاستخدمها عندما تحتاجها فعلاً.
  5. القاعدة: ابدأ بـ CSS، انتقل إلى WAAPI عندما تحتاج تحكماً برمجياً، واختر GSAP للتسلسل المعقد. في كل الحالات احترم prefers-reduced-motion.

المصادر

الكود

HTML

<!doctype html>
<html lang="ar" dir="rtl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Four ways to animate</title>
<style>
  body { font-family: system-ui, "Segoe UI", Tahoma, sans-serif; padding: 2rem; display: grid; gap: 1rem; }
  .box { padding: 1rem; border-radius: .6rem; background: #6c5ce7; color: #fff; }
  /* 1. transition: toggle a class */
  .t { opacity: 0; transform: translateX(24px); transition: opacity .4s, transform .4s; }
  .t.in { opacity: 1; transform: none; }
  /* 2. animation */
  .a { animation: in .4s both; }
  @keyframes in { from { opacity: 0; transform: translateX(24px); } }
  @media (prefers-reduced-motion: reduce) { .t { transition: none; } .a { animation: none; } }
</style>
<button id="play" type="button">شغّل الكل</button>
<div class="box t" id="t">1. CSS transition</div>
<div class="box a" id="a">2. CSS animation</div>
<div class="box" id="w">3. WAAPI</div>
<div class="box" id="g">4. GSAP</div>
<script src="https://cdn.jsdelivr.net/npm/gsap@3/dist/gsap.min.js"></script>
<script>
  const reduce = matchMedia("(prefers-reduced-motion: reduce)");
  document.getElementById("play").addEventListener("click", () => {
    // 1. transition
    const t = document.getElementById("t"); t.classList.remove("in"); void t.offsetWidth; t.classList.add("in");
    // 2. animation (restart)
    const a = document.getElementById("a"); a.classList.remove("a"); void a.offsetWidth; a.classList.add("a");
    // 3. WAAPI
    if (!reduce.matches) {
      document.getElementById("w").animate(
        [{ opacity: 0, transform: "translateX(24px)" }, { opacity: 1, transform: "none" }],
        { duration: 400, easing: "ease-out", fill: "both" }
      );
    }
    // 4. GSAP (only if the CDN script loaded)
    if (window.gsap && !reduce.matches) {
      gsap.fromTo("#g", { opacity: 0, x: 24 }, { opacity: 1, x: 0, duration: 0.4, ease: "power2.out" });
    }
  });
</script>
</html>

تصنيفات مرتبطة: انتقالات المشاهد وتكبير الكاميرا · تايبوجرافي حركي وتأثيرات النصوص