الرئيسية / تعلّم / كيف تنتقل بين مشهدين بتأثير مسح أو تلاشي أو انزلاق؟

كيف تنتقل بين مشهدين بتأثير مسح أو تلاشي أو انزلاق؟

هذه الانتقالات الثلاثة هي أساس أي انتقال أعقد، وتكفي أغلب العروض والصفحات. المشهد الجديد يدخل فوق القديم بأحد ثلاثة أشكال، ونراعي اتجاه القراءة فيدخل من الجهة الصحيحة في العربية.

الخطوات

  1. ضع المشهدين فوق بعض بـ position: absolute، والمشهد الجديد في طبقة أعلى.
  2. التلاشي: حرّك opacity من 0 إلى 1. الانزلاق: حرّك transform من translateX(±100%) إلى 0. المسح: حرّك clip-path من inset(0 100% 0 0) إلى inset(0 0 0 0).
  3. اقرأ direction الصفحة، واعكس إشارة الإزاحة واتجاه المسح في RTL.
  4. شغّل الحركة بـ element.animate() وانتظر وعد finished ثم أخفِ المشهد القديم.
  5. عند 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">
<style>
  body { margin: 0; min-height: 100vh; display: grid; place-content: center; gap: 1rem; justify-items: center; background: #0b1220; color: #fff; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; }
  .stage { position: relative; width: min(90vw, 480px); aspect-ratio: 16/9; overflow: hidden; border-radius: 12px; }
  .scene { position: absolute; inset: 0; display: grid; place-items: center; font-size: 2rem; font-weight: 800; }
  .a { background: #2563eb; } .b { background: #db2777; }
  button { font: inherit; padding: .4rem 1rem; }
</style>
<div class="stage" id="stage">
  <div class="scene a" id="A">المشهد الأول</div>
  <div class="scene b" id="B" hidden>المشهد الثاني</div>
</div>
<div><button data-t="fade">تلاشي</button> <button data-t="slide">انزلاق</button> <button data-t="wipe">مسح</button></div>
<script>
  const A = document.getElementById('A'), B = document.getElementById('B');
  const rtl = getComputedStyle(document.documentElement).direction === 'rtl';
  const sign = rtl ? -1 : 1;                              // the new scene enters from the "next" side of the reading direction
  const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
  let showingB = false, busy = false;
  const kinds = {
    fade:  () => [{ opacity: 0 }, { opacity: 1 }],
    slide: () => [{ transform: `translateX(${sign * 100}%)` }, { transform: 'translateX(0)' }],
    wipe:  () => rtl ? [{ clipPath: 'inset(0 100% 0 0)' }, { clipPath: 'inset(0 0 0 0)' }]
                     : [{ clipPath: 'inset(0 0 0 100%)' }, { clipPath: 'inset(0 0 0 0)' }],
  };
  function go(kind) {
    if (busy) return; busy = true;
    const [from, to] = showingB ? [B, A] : [A, B];         // "to" slides over "from"
    to.hidden = false; to.style.zIndex = 2; from.style.zIndex = 1;
    const frames = reduce ? kinds.fade() : kinds[kind]();   // reduced motion: gentle fade only
    const anim = to.animate(frames, { duration: reduce ? 200 : 600, easing: 'ease-in-out' });
    anim.finished.then(() => { from.hidden = true; showingB = !showingB; busy = false; });
  }
  document.querySelectorAll('button').forEach(b => b.onclick = () => go(b.dataset.t));
</script>
</html>

لم تجد الحركة التي تريدها؟ اطلبها من هنا

مقالات ذات صلة: