Thuta Learning
CSS
AdvancedWeb Developmentအခြေခံ

CSS Animations

CSSသင်ခန်းစာ 2630 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • @keyframes ကို from/to နဲ့ percentage တွေသုံးပြီး ရေးနိုင်ဖို့
  • animation shorthand ထဲက duration, delay, iteration-count, direction ကို ခွဲဖတ်နိုင်ဖို့
  • fill-mode (none, forwards, backwards, both) က delay မတိုင်ခင်နဲ့ ပြီးသွားပြီးနောက် ဘာပြမလဲ ကြိုခန့်မှန်းနိုင်ဖို့
  • Animation နဲ့ transition ကြား ရွေးနိုင်ပြီး prefers-reduced-motion မှာ content ပျောက်မသွားအောင် ရေးနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Transition က ခရီးစဉ် တစ်ခုပါ — အခြေအနေ A ကနေ B ကို သွားတာ၊ ပြီးတော့ တစ်ခုခု ပြောင်းမှ စတာပါ။ Animation ကတော့ ကာတွန်းရုပ်ရှင် frame တွေလိုပါ — @keyframes ထဲမှာ 0%, 60%, 100% လို အချိန်မှတ်တိုင်းမှာ ဘယ်လို ဖြစ်နေရမလဲ ရေးထားပြီး page load ဖြစ်တာနဲ့ ကိုယ်တိုင် စ run ပါတယ်၊ ထပ်ခါထပ်ခါ ပြန်ဖွင့်တာ၊ ပြောင်းပြန် ဖွင့်တာလည်း လုပ်နိုင်ပါတယ်။

Notification ဝင်လာတာ၊ loading spinner လည်နေတာ၊ badge တစ်ခု ခုန်ပေါ်လာတာ စတာတွေက animation ရဲ့ နယ်မြေပါ။ ဒါပေမယ့် animation ပြီးဆုံးလို့ပဲ ဖြစ်ဖြစ်၊ ပိတ်ထားလို့ပဲ ဖြစ်ဖြစ် content က အမြဲ မြင်ရ၊ အသုံးပြုလို့ ရနေရပါမယ်။

Problem → Why → Solution

ပြဿနာ: delay ပါတဲ့ fade-in က အရင် ပေါ်ပြီးမှ ပျောက်တာ

Hero ခေါင်းစဉ်ကို page load ပြီး 500ms နေမှ fade-in ဖြစ်စေချင်လို့ animation: fade-in 400ms ease-out 500ms လို့ ရေးထားပါတယ်။ Page ဖွင့်လိုက်တော့ ခေါင်းစဉ်က ချက်ချင်း အပြည့် ပေါ်နေပြီး 500ms ရောက်တော့ ရုတ်တရက် ပျောက်သွားကာ ပြန် fade-in ဖြစ်ပါတယ်။ Design team က "flash" ဖြစ်နေတယ်လို့ bug ဖွင့်လာပါတယ်။

.hero-title {
  animation: fade-in 400ms ease-out 500ms; /* no fill-mode */
}

@keyframes fade-in {
  from { opacity: 0; transform: translateY(12px); }
  to   { opacity: 1; transform: translateY(0); }
}
/* 0-500ms: opacity 1 (base style), then jumps to 0 and fades in */

ဘာကြောင့် flash ဖြစ်တာလဲ

Default fill-mode က none ပါ — delay ကာလအတွင်းမှာ keyframe တွေ အသက်မဝင်သေးလို့ element က ကိုယ်ပိုင် base style (opacity: 1) ကို ပြပါတယ်။ Animation စတာနဲ့ from keyframe (opacity: 0) ကို ခုန်သွားလို့ flash ဖြစ်ပါတယ်။ backwards (ဒါမှမဟုတ် both) ထည့်လိုက်ရင် delay အတွင်းမှာ from keyframe ကို ကြိုသုံးပြီး both က ပြီးသွားတဲ့နောက်လည်း to keyframe ကို ထိန်းထားပါတယ်။

Core concept: keyframes timeline နဲ့ animation property များ

@keyframes name { ... } က timeline ကို ဖော်ပြပါတယ်။ from က 0%၊ to က 100% နဲ့ အတူတူဖြစ်ပြီး ကြားမှာ 30%, 60% လို မှတ်တိုင်တွေ ထပ်ထည့်လို့ရပါတယ်။ Keyframe တစ်ခုမှာ မပါတဲ့ property ဆိုရင် browser က element ရဲ့ ကိုယ်ပိုင် value ကို သုံးပါတယ် — ဒါကြောင့် spin animation မှာ to { transform: rotate(360deg) } တစ်ခုတည်း ရေးရင် လက်ရှိ transform ကနေ စပါတယ်။ Timing function ကို element ပေါ်မှာ ရေးရေး၊ keyframe ထဲမှာ ရေးရေး keyframe နှစ်ခုကြား segment တစ်ခုချင်းစီမှာ သက်ရောက်ပါတယ်၊ animation တစ်ခုလုံးအတွက် မဟုတ်ပါဘူး။

Propertyထိန်းတဲ့အရာဥပမာ
animation-name / -durationဘယ် keyframes၊ ဘယ်လောက်ကြာpop, 400ms
animation-timing-function / -delayအမြန်နှုန်း curve၊ စမီ စောင့်ချိန်ease-out, 300ms
animation-iteration-countဘယ်နှစ်ကြိမ် run မလဲ3, infinite
animation-directionရှေ့ / နောက် / အလှည့်ကျalternate
animation-fill-modeRun မနေတဲ့အချိန် ဘာပြမလဲboth
animation-play-stateခဏရပ် / ဆက်paused

Shorthand (animation: pop 400ms ease-out 300ms both) မှာ ပထမ အချိန်တန်ဖိုးက duration၊ ဒုတိယက delay ပါ။ fill-mode none ဆိုရင် animation မစခင်ရော ပြီးသွားပြီးနောက်ပါ element က base style ကို ပြပြီး forwards က နောက်ဆုံး keyframe ကို ထိန်းထား၊ backwards က delay အတွင်း ပထမ keyframe ကို ကြိုပြ၊ both က နှစ်မျိုးလုံး လုပ်ပါတယ်။ Animation run နေတုန်း keyframe value တွေက cascade ထဲမှာ ပုံမှန် declaration တွေကို (specificity ဘယ်လောက် မြင့်မြင့်) နိုင်ပါတယ်၊ !important declaration တွေကိုတော့ မနိုင်ပါဘူး — ဒါကြောင့် animate လုပ်နေတဲ့ property ကို class နဲ့ override လုပ်လို့ မရတာ ဖြစ်တတ်ပါတယ်။

အရေးကြီးဆုံး ဒီဇိုင်းစည်းမျဉ်းက base style ကို final state အဖြစ် ထားဖို့ပါ — keyframe တွေက "ဘယ်ကနေ ရောက်လာလဲ" ကိုပဲ ဖော်ပြပါစေ။ ဒီလိုဆိုရင် reduced motion မှာ animation: none လို့ ရေးလိုက်ရုံနဲ့ content က ချက်ချင်း နောက်ဆုံး ပုံစံအတိုင်း ပေါ်ပါတယ်။ Base style ကို opacity: 0 ထားပြီး animation နဲ့မှ ပေါ်လာအောင် လုပ်ထားရင်တော့ animation ပိတ်တာနဲ့ content ပျောက်သွားပါတယ်။ Transition lesson မှာလိုပဲ transform နဲ့ opacity ကို animate လုပ်တာက layout ကို ထိခိုက်မှု မရှိလို့ ချောမွေ့ပါတယ်။

Animation ရဲ့ timeline
  1. Delay: fill-mode backwards ဆို from ပြ
  2. Run: keyframe တစ်ခုချင်းကြား interpolate
  3. Iteration ပြီး: count ကုန်မှ ရပ်
  4. End: forwards ဆို to ထိန်း၊ မဟုတ်ရင် base

Base style ကို final state အဖြစ် ထားရင် animation ပိတ်လည်း ဘာမှ မပျက်ပါဘူး။

အထွေထွေ ပုံစံ

css
@keyframes name {
  0%   { property: start-value; }
  50%  { property: middle-value; }
  100% { property: end-value; }
}

.element {
  animation: name duration timing-function delay iteration-count direction fill-mode;
}

@media (prefers-reduced-motion: reduce) { .element { animation: none; } }

Timeline ကို @keyframes မှာ တစ်ခါ ရေးပြီး element တွေက animation shorthand နဲ့ နာမည်ကို ခေါ်ပါတယ်။ Keyframes တစ်ခုကို element အများကြီးက duration မတူဘဲ ပြန်သုံးလို့ ရပါတယ်။

Example: reduced motion လေးစားတဲ့ notice entrance

ဒီ example မှာ .notice က အောက်ကနေ နည်းနည်း တက်လာရင်း fade-in ဖြစ်ပါတယ်။ Base style မှာ opacity နဲ့ transform ကို မထိထားလို့ reduced motion user တွေအတွက် animation: none က notice ကို ချက်ချင်း နောက်ဆုံးပုံစံနဲ့ ပြပါတယ်။ DevTools ရဲ့ Rendering panel မှာ prefers-reduced-motion ကို emulate လုပ်ပြီး စမ်းကြည့်ပါ။

.notice {
  animation: notice-in 240ms ease-out both;
}

@keyframes notice-in {
  from {
    opacity: 0;
    transform: translateY(0.75rem);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (prefers-reduced-motion: reduce) {
  .notice { animation: none; }
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. shorthandanimation: notice-in 240ms ease-out both;

    Keyframes နာမည် notice-in ကို 240ms ကြာ ease-out curve နဲ့ တစ်ကြိမ် run ပါတယ်။ Delay မရှိလို့ both ရဲ့ backwards အပိုင်းက ဖုံးစရာ မရှိပါဘူး၊ forwards အပိုင်းက အဆုံးမှာ to keyframe ကို ထိန်းပေမယ့် ဒီမှာ base style နဲ့ တူနေပါတယ်။ Delay နောက်မှ ထည့်လာရင်လည်း flash မဖြစ်အောင် entrance animation တွေမှာ both ကို အလေ့အထ အဖြစ် ရေးတာ ဘေးကင်းပါတယ်။

  2. @keyframes@keyframes notice-in {

    Timeline ကို နာမည်ပေးပြီး ကြေညာပါတယ်။ ဒီနာမည်ကို စာလုံးပေါင်း မှားရင် animation က error မပြဘဲ ဘာမှ မလုပ်ပါဘူး။

  3. fromtransform: translateY(0.75rem);

    စမှတ်မှာ opacity 0 နဲ့ 0.75rem (default font size မှာ 12px) အောက်မှာ ရှိပါတယ်။ ရွေ့တဲ့ အကွာအဝေး သေးလို့ motion sensitive user တွေအတွက်လည်း သက်သာပါတယ်။

  4. totransform: translateY(0);

    အဆုံးမှတ်က base style နဲ့ တူပါတယ် — opacity 1၊ နေရာ မပြောင်း။ ဒါကြောင့် animation ပြီးတာနဲ့ base style ကို ပြန်ရောက်လည်း ခုန်တာ မရှိပါဘူး။

  5. reduced motion.notice { animation: none; }

    Reduce motion ဖွင့်ထားတဲ့ user တွေအတွက် animation ကို လုံးဝ ဖယ်ပါတယ်။ Base style က final state ဖြစ်လို့ notice က ချက်ချင်း မြင်ရပါတယ်။

You should see
Notice က 240ms အတွင်း opacity 0 ကနေ 1 ဖြစ်ပြီး 12px အောက်ကနေ နေရာမှန်ကို တက်လာပါတယ်။ prefers-reduced-motion: reduce ဆိုရင် animation မပါဘဲ ချက်ချင်း opacity 1 နဲ့ ပေါ်ပါတယ်။

Animation လား transition လား

လိုအပ်ချက်ရွေးရမယ့်ဟာ
Hover/focus/class ကြောင့် state နှစ်ခုကြား ပြောင်းTransition
Page load မှာ ကိုယ်တိုင် စ runAnimation
မှတ်တိုင် သုံးခုနဲ့ အထက် (ခုန်တာ၊ တုန်တာ)Animation
ထပ်ခါထပ်ခါ (spinner, pulse)Animation + iteration-count

JavaScript ကနေ animation ကို ပြန် run ချင်ရင် class ကို ဖယ်၊ browser ကို frame တစ်ခု render လုပ်ခွင့်ပေး (ဥပမာ requestAnimationFrame နဲ့)၊ ပြီးမှ class ကို ပြန်ထည့်ရပါတယ် — animation-name မပြောင်းသရွေ့ browser က အသစ် မစလို့ပါ။ animation-play-state: paused ကို :hover နဲ့ တွဲပြီး marquee လို ရွေ့နေတဲ့ content ကို user ဖတ်နေတုန်း ရပ်ပေးနိုင်ပါတယ်။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Production app တွေမှာ CSS animation ကို အများဆုံး တွေ့ရတာက loading state တွေပါ — လည်နေတဲ့ spinner၊ content မရောက်ခင် ပြတဲ့ skeleton screen ပေါ်က shimmer၊ toast notification ဝင်လာတာ၊ modal dialog ပေါ်လာတာ စတာတွေပါ။ Animation library တော်တော်များများ (ဥပမာ Animate.css) ကလည်း @keyframes တွေကို class နာမည်တွေနဲ့ ထုတ်ပေးထားတာမို့ underlying mechanism က ဒီ lesson ထဲက အတိုင်းပါပဲ။

Accessibility guideline တွေက အလိုအလျောက် စပြီး ငါးစက္ကန့်ထက် ကြာကြာ ရွေ့နေတဲ့ content ကို user ရပ်လို့ရအောင် လုပ်ဖို့ တောင်းဆိုပါတယ်၊ ဒါကြောင့် infinite loop ကို spinner လို တကယ် အလုပ်လုပ်နေကြောင်း ပြတဲ့ နေရာမှာပဲ သုံးကြပါတယ်။ Reduced motion မှာ spinner ကို ဖယ်လိုက်ရင် user က loading ဖြစ်နေတာ မသိတော့လို့ team တွေက အလှည့်ကို နှေးလိုက်တာ ဒါမှမဟုတ် "Loading…" စာ ပြတာနဲ့ အစားထိုးကြပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Content ကို animation နဲ့မှ ပေါ်အောင် လုပ်ထားတာ

.card { opacity: 0; animation: show 300ms forwards; } လို ရေးရင် reduced motion block က animation: none လုပ်လိုက်တာနဲ့ card တွေ လုံးဝ မမြင်ရတော့ပါဘူး၊ keyframes နာမည် စာလုံးပေါင်း မှားရင်လည်း အတူတူပါပဲ။ Base style ကို opacity: 1 ထားပြီး from keyframe ထဲမှာ opacity: 0 ကို ထည့်ကာ fill-mode both သုံးပါ။

2. forwards နဲ့ ဖျောက်ပြီး element ကျန်နေတာ

Toast ကို fade-out keyframes နဲ့ forwards သုံးပြီး opacity 0 ဖြစ်အောင် လုပ်ရုံပဲ ဆိုရင် မမြင်ရပေမယ့် နေရာယူထားဆဲ၊ click လုပ်လို့ရဆဲ၊ screen reader က ဖတ်နိုင်ဆဲ ဖြစ်ပါတယ်။ animationend event မှာ element ကို DOM ကနေ ဖယ်ပါ ဒါမှမဟုတ် hidden attribute ထည့်ပါ။

3. Decorative infinite loop

Call-to-action button ကို animation: pulse 1s infinite နဲ့ အမြဲ ခုန်နေအောင် လုပ်ထားရင် စာဖတ်နေတဲ့ user ရဲ့ အာရုံကို အမြဲ ဆွဲနေပြီး motion sensitive user တွေအတွက် ပြဿနာ ဖြစ်ပါတယ်။ iteration-count ကို 2 ဒါမှမဟုတ် 3 လို ကန့်သတ်ပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • Base style ကို final state အဖြစ် ထားပြီး fill-mode both နဲ့ entrance animation ရေးပါ။
  • Keyframes ထဲမှာ transform နဲ့ opacity ကိုပဲ အဓိက သုံးပါ။
  • Infinite loop ကို status indicator အတွက်ပဲ သုံးပြီး reduced motion မှာ နှေးအောင် ဒါမှမဟုတ် static ဖြစ်အောင် လုပ်ပါ။

Mini challenge

Mini challenge: pop-in badge နဲ့ spinner

Inbox badge တစ်ခု delay နဲ့ ခုန်ပေါ်လာအောင်၊ loading spinner တစ်ခု အမြဲ လည်နေအောင် animate လုပ်ပြီး reduced motion user တွေအတွက် fallback ထည့်ပါ။

  • pop keyframes: 0% မှာ scale(0) နဲ့ opacity 0၊ 60% မှာ scale(1.2)၊ 100% မှာ scale(1)
  • .badge က 400ms ease-out၊ 300ms delay နဲ့ run ပြီး delay အတွင်း မပေါ်ရ (flash မဖြစ်ရ)
  • .spinner က 1 စက္ကန့်တစ်ပတ် linear နဲ့ အဆုံးမဲ့ လည်ရမယ်
  • Reduced motion မှာ badge ကို animation မပါဘဲ ချက်ချင်း ပြပြီး spinner ကို 3 စက္ကန့်တစ်ပတ်အထိ နှေးပါ
badge-spinner.html
<style>
  /* TODO: .badge - pill shape, crimson background, white text,
     plays "pop" for 400ms ease-out after 300ms, hidden during the delay */

  /* TODO: @keyframes pop - grow from nothing, overshoot at 60%, settle */

  /* TODO: .spinner - 24px ring with one blue edge, turning forever */

  /* TODO: @keyframes spin - one full turn */

  /* TODO: reduced motion - no badge animation, a much slower spinner */
</style>
<p>Inbox <span class="badge">3</span></p>
<p><span class="spinner" role="status" aria-label="Loading"></span></p>

ရလဒ်: Page ဖွင့်ပြီး ပထမ 300ms မှာ badge က scale(0)၊ opacity 0 နဲ့ မမြင်ရပါဘူး၊ ပြီးမှ 1.2 အထိ ကြီးပြီး 1 ကို ပြန်ကျ ရပ်ပါတယ်။ Spinner က တစ်စက္ကန့် တစ်ပတ် ဆက်လည်ပါတယ်။ Reduced motion မှာ badge က ချက်ချင်း opacity 1 နဲ့ ပေါ်ပြီး spinner က သုံးဆ နှေးနှေး လည်ပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

Delay အတွင်း မပေါ်စေဖို့ fill-mode both (ဒါမှမဟုတ် backwards) လိုပါတယ်။ Spin keyframes မှာ to { transform: rotate(360deg); } တစ်ခုတည်း ရေးရင် လုံလောက်ပါတယ်။ Spinner ကို reduced motion မှာ animation-duration တစ်ခုတည်း ပြောင်းပါ၊ အမည်နဲ့ iteration ကို ပြန်ရေးစရာ မလိုပါဘူး။

အဖြေ ကြည့်ရန်
badge-spinner.html
<style>
  .badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    background: crimson;
    color: white;
    animation: pop 400ms ease-out 300ms both;
  }
  @keyframes pop {
    0%   { transform: scale(0); opacity: 0; }
    60%  { transform: scale(1.2); opacity: 1; }
    100% { transform: scale(1); }
  }
  .spinner {
    display: inline-block;
    width: 24px;
    height: 24px;
    border: 3px solid #ddd;
    border-top-color: #1a73e8;
    border-radius: 50%;
    animation: spin 1s linear infinite;
  }
  @keyframes spin {
    to { transform: rotate(360deg); }
  }
  @media (prefers-reduced-motion: reduce) {
    .badge { animation: none; }
    .spinner { animation-duration: 3s; }
  }
</style>
<p>Inbox <span class="badge">3</span></p>
<p><span class="spinner" role="status" aria-label="Loading"></span></p>

Quick Quiz 1 / 3

animation: fade 500ms ease 1s; (fill-mode မပါ) မှာ base style က opacity: 1 ဖြစ်ပြီး from keyframe က opacity: 0 ဆိုရင် ပထမ 1 စက္ကန့်အတွင်း element ကို ဘယ်လို မြင်ရမလဲ။

Quick Quiz 2 / 3

animation: bounce 2s ease-in-out 3 alternate; မှာ 3 က ဘာလဲ။

Quick Quiz 3 / 3

Entrance animation တစ်ခုကို reduced motion မှာ animation: none လုပ်လိုက်တော့ card တွေ ပျောက်သွားပါတယ်။ အဖြစ်နိုင်ဆုံး အကြောင်းရင်း ဘာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • @keyframes က from/to နဲ့ percentage မှတ်တိုင်တွေနဲ့ timeline ကို ဖော်ပြပြီး animation shorthand က နာမည်နဲ့ ခေါ်သုံးပါတယ်။
  • Shorthand မှာ ပထမ အချိန်က duration၊ ဒုတိယက delay၊ unit မပါတဲ့ number က iteration-count ပါ။
  • fill-mode both က delay အတွင်း from ကို ပြပြီး ပြီးသွားရင် to ကို ထိန်းလို့ flash မဖြစ်ပါဘူး။
  • Base style ကို final state ထားပြီး reduced motion မှာ animation: none ဒါမှမဟုတ် နှေးတဲ့ version ပေးပါ၊ transform နဲ့ opacity ကို ဦးစားပေးပါ။

CSS reference — MDN Web Docs

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။