Thuta Learning
CSS
AdvancedWeb Developmentအခြေခံ

Transitions

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

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

  • transition shorthand ကို property, duration, timing function, delay နဲ့ ရေးနိုင်ဖို့
  • Transition ကို base state ပေါ်မှာ ထားရင် နှစ်ဘက်စလုံး ချောမွေ့ပြီး :hover ပေါ်မှာပဲ ထားရင် ပြန်ခုန်ကျတာကို ရှင်းပြနိုင်ဖို့
  • Layout, paint, composite အဆင့်တွေကို သုံးပြီး transform/opacity က width/top ထက် ဘာကြောင့် သက်သာလဲ ရှင်းပြနိုင်ဖို့
  • prefers-reduced-motion: reduce မှာ ရွေ့လျားမှုကို ဖယ်ပြီး feedback ကို ဆက်ထားနိုင်ဖို့

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

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

အခန်းမီးခလုတ်ကို နှိပ်လိုက်ရင် မီးက ချက်ချင်း လင်းပါတယ်။ Dimmer ခလုတ်ကို လှည့်ရင်တော့ အလင်းက တဖြည်းဖြည်း တိုးလာပါတယ်။ CSS မှာ hover, focus, class ပြောင်းတာလို အခြေအနေ ပြောင်းလဲမှု တစ်ခုခု ဖြစ်တိုင်း default အားဖြင့် ခလုတ်လို ချက်ချင်း ခုန်ပြောင်းပါတယ်။ transition က အဲဒါကို dimmer ဖြစ်အောင် လုပ်ပေးတာပါ — "background-color ပြောင်းရင် 0.25 စက္ကန့် ယူပြီး ပြောင်းပါ" လို့ ပြောလိုက်တာပါ။

Transition က အလှတစ်ခုတည်းအတွက် မဟုတ်ပါဘူး — button နှိပ်လိုက်တာ သိရဲ့လား၊ menu ဘယ်ကနေ ပေါ်လာလဲ ဆိုတာကို user ကို ပြောပြတဲ့ feedback ပါ။ ဒါပေမယ့် အရာအားလုံး လှုပ်နေရင် disco floor ဖြစ်သွားပြီး လှုပ်ရှားမှုကြောင့် မူးဝေတတ်တဲ့ user တွေအတွက် ပြဿနာ ဖြစ်နိုင်ပါတယ်။

Problem → Why → Solution

ပြဿနာ: panel ဖွင့်တိုင်း page တစ်ခုလုံး တုန်နေတာ

Side panel တစ်ခုကို ဖွင့်ဖို့ width: 0 ကနေ 320px အထိ၊ left value ကိုပါ transition: all 600ms နဲ့ ပြောင်းထားပါတယ်။ ကိုယ့် laptop ပေါ်မှာ ကောင်းကောင်း ကြည့်ရပေမယ့် ဈေးပေါ် Android phone ပေါ်မှာတော့ panel က တုန်တုန်ယင်ယင် ဖွင့်ပြီး ဘေးက စာတွေက frame တိုင်းမှာ ပြန်စီနေပါတယ်။ ဒါ့အပြင် transition: all ကြောင့် font-size ကို responsive ပြောင်းတဲ့အခါ စာတွေကပါ ကြိုမထင်ထားဘဲ လှုပ်လာပါတယ်။

.panel {
  position: fixed;
  left: -320px;
  width: 0;
  transition: all 600ms;          /* every changing property animates */
}

.panel.open {
  left: 0;
  width: 320px;                    /* layout is recalculated every frame */
}

ဘာကြောင့် တုန်တာလဲ

width, left, height, margin လို property တွေ ပြောင်းတိုင်း browser က layout (element တွေရဲ့ နေရာနဲ့ အရွယ်အစား) ကို ပြန်တွက်ပြီး paint ပြန်လုပ်ရပါတယ်။ 60fps ဆိုရင် frame တစ်ခုအတွက် 16ms လောက်ပဲ ရလို့ phone အားနည်းတွေမှာ မမီပါဘူး။ transform: translateX() နဲ့ opacity ကတော့ layout ကို မထိဘဲ ပုံဆွဲပြီးသား layer ကို ရွှေ့ရုံ၊ မှိန်ရုံပဲ ဖြစ်လို့ browser က များသောအားဖြင့် compositor (GPU) ပေါ်မှာ လုပ်ပေးနိုင်ပါတယ်။ transition: all ကလည်း ကိုယ်မရည်ရွယ်တဲ့ property တွေကိုပါ animate လုပ်ပါတယ်။

Core concept: computed value ပြောင်းတဲ့အခါ interpolate လုပ်ခြင်း

Transition က property တစ်ခုရဲ့ computed value ပြောင်းသွားတဲ့ အချိန်မှာ စတင်ပါတယ် — :hover ဝင်တာ၊ class ထည့်တာ၊ JavaScript က style ပြောင်းတာ စတာတွေပါ။ Browser က value အဟောင်းနဲ့ အသစ်ကြားမှာ frame တိုင်းအတွက် ကြားတန်ဖိုးကို တွက် (interpolate) ပါတယ်။ Transition ကို longhand လေးခုနဲ့ ဖော်ပြပါတယ်: transition-property (ဘာကို)၊ transition-duration (ဘယ်လောက်ကြာကြာ)၊ transition-timing-function (အမြန်နှုန်း ပုံစံ — ease, linear, ease-out, cubic-bezier())၊ transition-delay (ဘယ်လောက် စောင့်ပြီးမှ)။ Shorthand မှာ ပထမ အချိန်တန်ဖိုးက duration၊ ဒုတိယက delay ဖြစ်ပြီး property အများကြီးကို comma နဲ့ ခြားရေးပါတယ်။

Transition ကို ဘယ် rule ထဲ ရေးလဲ ဆိုတာ အရေးကြီးပါတယ်။ Base state (.button) ပေါ်မှာ ရေးရင် hover ဝင်တဲ့အခါရော ထွက်တဲ့အခါပါ transition ဖြစ်ပါတယ်။ :hover rule ထဲမှာပဲ ရေးရင် hover ဝင်တုန်းမှာ transition ရှိပြီး ထွက်လိုက်တာနဲ့ transition မရှိတော့လို့ ချက်ချင်း ပြန်ခုန်ကျပါတယ်။ Value တွေကြား ကြားတန်ဖိုး တွက်လို့ရတဲ့ property (color, length, transform, opacity) တွေကပဲ ချောမွေ့ပါတယ်၊ display လို discrete property တွေက ချက်ချင်း ပြောင်းပါတယ် — display: none ကနေ block ပြောင်းတဲ့ element ရဲ့ opacity transition က မဖြစ်ဘဲ ချက်ချင်း ပေါ်လာပါတယ်။

ပြောင်းတဲ့ propertyBrowser လုပ်ရတဲ့ အလုပ်Animation အတွက်
width, height, top, left, margin, paddingLayout + paint + compositeရှောင်ပါ
color, background-color, box-shadowPaint + compositeအတိုအတောင်း ရ
transform, opacityComposite only (များသောအားဖြင့်)ဦးစားပေး

Motion ကို လျှော့ချထားတဲ့ user တွေ (vestibular disorder ရှိသူ၊ မူးဝေတတ်သူ) က operating system setting မှာ "Reduce motion" ကို ဖွင့်ထားနိုင်ပြီး CSS က @media (prefers-reduced-motion: reduce) နဲ့ သိနိုင်ပါတယ်။ အဲဒီ media query ထဲမှာ transform လို ရွေ့လျားမှုကို ဖယ်ပြီး အရောင်ပြောင်းတာ၊ fade လုပ်တာလို ရွေ့မနေတဲ့ feedback ကိုတော့ ဆက်ထားလို့ ရပါတယ်။ Reduced motion ဆိုတာ feedback မရှိတာ မဟုတ်ပါဘူး — နှိပ်လိုက်တာကို မြင်သာတဲ့ တုံ့ပြန်မှု မပေးတဲ့ button က သီးခြား usability bug တစ်ခုပါ၊ ဒါကြောင့် signal ကို ဖျက်မပစ်ဘဲ ရွေ့လျားမှုကို ပိုတည်ငြိမ်တဲ့ signal နဲ့ အစားထိုးပါ။

Transition တစ်ခု ဖြစ်ပုံ
  1. State ပြောင်း: hover, focus, class
  2. Computed value အဟောင်း/အသစ် နှိုင်းယှဉ်
  3. transition-property ကိုက်ရင် စတင်
  4. Frame တိုင်း ကြားတန်ဖိုး တွက်ပြီး ဆွဲ

Transition က state နှစ်ခုကြား ခရီးကိုပဲ ဖော်ပြတာပါ — state တွေကိုယ်တိုင်ကို သီးခြား rule တွေက သတ်မှတ်ပါတယ်။

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

css
.element {
  transition: property duration timing-function delay,
              other-property duration timing-function;
}

.element:state { property: new-value; }   /* the change that triggers it */

@media (prefers-reduced-motion: reduce) {
  .element:state { /* drop movement, keep non-moving feedback */ }
}

Transition ကို base selector ပေါ်မှာ ထားပြီး state rule ထဲမှာ value အသစ်ကိုပဲ ရေးပါ။ Property တစ်ခုချင်းကို နာမည်တပ်ရေးပြီး reduced motion အတွက် အပိုင်းတစ်ခု အမြဲ ထည့်ပါ။

Example: hover မှာ အရောင်ပြောင်းပြီး မြောက်တက်တဲ့ button

ဒီ example က transition ကို base state ပေါ်မှာ ထားပြီး property နှစ်ခုကို နာမည်တပ် ရေးထားပါတယ်။ Button ပေါ် mouse တင်ပြီး ပြန်ဖယ်ကြည့်ပါ — ဝင်တဲ့အခါရော ထွက်တဲ့အခါပါ ချောချောမွေ့မွေ့ ဖြစ်ပါတယ်။

.button {
  background-color: #77f5ff;
  transform: translateY(0);
  transition: transform 0.25s ease, background-color 0.25s ease;
}

.button:hover {
  background-color: #ff4dcd;
  transform: translateY(-2px);
}

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

  1. starting valuestransform: translateY(0);

    စမှတ်ကို တိတိကျကျ ရေးထားပါတယ်။ transform: none ကနေလည်း browser က interpolate လုပ်နိုင်ပေမယ့် function တူတာ ရေးထားရင် ဖတ်ရတာ ပိုရှင်းပါတယ်။

  2. transitiontransition: transform 0.25s ease, background-color 0.25s ease;

    Property နှစ်ခုကို comma နဲ့ ခြားပြီး 250ms ease နဲ့ transition လုပ်ပါတယ်။ all မသုံးလို့ နောက်မှ ထည့်တဲ့ padding လို property တွေ မရည်ရွယ်ဘဲ animate မဖြစ်ပါဘူး။ Base rule ထဲမှာ ရှိလို့ နှစ်ဘက်စလုံး အလုပ်လုပ်ပါတယ်။

  3. :hover.button:hover {

    ဒီ rule က value အသစ်တွေကိုပဲ သတ်မှတ်ပါတယ် — transition ကို ဒီမှာ ထပ်မရေးရပါဘူး။ Keyboard user တွေအတွက် .button:focus-visible ကိုလည်း selector ထဲ ထည့်သင့်ပါတယ်။

  4. lifttransform: translateY(-2px);

    2px အပေါ် ရွှေ့တာက layout ကို မထိလို့ ဘေးက element တွေ မရွေ့ပါဘူး၊ ချောမွေ့ဖို့လည်း သက်သာပါတယ်။ background-color ကတော့ paint လိုပေမယ့် 250ms လောက် တိုလို့ ပြဿနာ မရှိပါဘူး။

You should see
Mouse တင်လိုက်ရင် 250ms အတွင်း button နောက်ခံက cyan (#77f5ff) ကနေ pink (#ff4dcd) ဖြစ်ပြီး 2px မြောက်တက်ပါတယ်။ Mouse ဖယ်လိုက်ရင် အဲဒီအတိုင်း ချောချောမွေ့မွေ့ ပြန်ကျပါတယ်။

Timing function နဲ့ ကြားဖြတ်ခံရတဲ့ transition

Timing functionခံစားရပုံသင့်တော်တဲ့ နေရာ
ease (default)မြန်မြန်စ၊ ဖြည်းဖြည်း ဆုံးအထွေထွေ hover
ease-outအမြန်ဆုံးနဲ့ စပြီး နှေးနှေး ရပ်ပေါ်လာတဲ့ element
ease-inနှေးနှေး စပြီး မြန်မြန် ဆုံးထွက်သွားတဲ့ element
linearအမြန်နှုန်း တသမတ်တည်းProgress bar, လည်နေတဲ့ spinner

Hover လုပ်ပြီး transition တစ်ဝက်မှာ mouse ဖယ်လိုက်ရင် element က စမှတ်ကို ခုန်မသွားဘဲ လက်ရှိ ကြားတန်ဖိုးကနေ transition အသစ် စပါတယ်။ စမှတ်ဆီ ပြန်သွားတာ ဖြစ်ရင် browser က ပြန်လမ်းကို တိုစေပါတယ် — 1s transition ရဲ့ 500ms မှာ ပြန်လှည့်ရင် ပြန်ရောက်ဖို့ 500ms လောက်ပဲ ကြာပါတယ်။ ဒါကြောင့် user က mouse ကို မြန်မြန် ဖြတ်ရွှေ့လည်း UI က တုံ့ပြန်မှု မြန်နေပါတယ်။

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

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

Product UI တွေမှာ transition အများစုက 150ms ကနေ 300ms ကြား ရှိပါတယ် — button hover, input focus ring, dropdown fade, toast ပေါ်လာတာ စတာတွေပါ။ 100ms အောက်ဆို မမြင်သလောက် ဖြစ်ပြီး 500ms ကျော်ရင် interface က နှေးနေသလို ခံစားရပါတယ်။ Design system တွေက --duration-fast: 150ms၊ --ease-out: cubic-bezier(...) လို custom property တွေနဲ့ timing ကို ပေါင်းစည်းထားလို့ app တစ်ခုလုံး လှုပ်ရှားပုံ တသမတ်တည်း ဖြစ်ပါတယ်။

Team တော်တော်များများက global stylesheet ထဲမှာ reduced motion အတွက် safety net တစ်ခု ထည့်ကြပါတယ် — @media (prefers-reduced-motion: reduce) ထဲမှာ *, *::before, *::after ကို transition-duration နဲ့ animation-duration အရမ်းတိုအောင် !important နဲ့ ပြောင်းတာမျိုးပါ။ Duration ကို 0 အစား 0.01ms လို တန်ဖိုးငယ်လေး သုံးကြပါတယ်၊ transitionend event ကို စောင့်နေတဲ့ JavaScript ကို ဆက်အလုပ်လုပ်စေချင်လို့ပါ။ Performance စစ်ချင်ရင် DevTools Performance panel မှာ record လုပ်ပြီး frame တိုင်းမှာ Layout အပိုင်း ပေါ်နေလား ကြည့်ပါ။

အမှားများ

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

1. :hover rule ထဲမှာပဲ transition ရေးတာ

.card:hover { transition: transform 300ms; transform: translateY(-4px); } လို ရေးရင် ဝင်တဲ့အခါ ချောမွေ့ပေမယ့် mouse ဖယ်လိုက်တာနဲ့ card က ချက်ချင်း ပြန်ခုန်ကျပါတယ်၊ ထွက်တဲ့အခါမှာ :hover rule မကိုက်တော့လို့ပါ။ Transition ကို .card ပေါ် ရွှေ့ပါ။

2. Layout property တွေကို animate လုပ်တာ

Drawer ကို left ဒါမှမဟုတ် width နဲ့ ဖွင့်ရင် frame တိုင်းမှာ layout ပြန်တွက်ရလို့ phone တွေမှာ တုန်ပါတယ်။ Drawer ကို နောက်ဆုံး အရွယ်အစားနဲ့ အမြဲထားပြီး transform: translateX(-100%) ကနေ translateX(0) ကို transition လုပ်ပါ။

3. display: none ကနေ fade လုပ်ဖို့ ကြိုးစားတာ

display: none; opacity: 0 ကနေ display: block; opacity: 1 ကို class တစ်ခုနဲ့ ပြောင်းရင် opacity transition မဖြစ်ဘဲ element က ချက်ချင်း ပေါ်လာပါတယ်၊ display: none မှာ animate လုပ်စရာ box မရှိလို့ပါ။ Fade လုပ်ချင်ရင် opacity နဲ့ visibility ကို တွဲသုံးပါ။

Best Practices

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

  • transition: all အစား property တစ်ခုချင်းကို နာမည်တပ်ရေးပါ။
  • Movement အတွက် transform၊ ပေါ်/ပျောက်အတွက် opacity ကို ဦးစားပေးပါ။
  • UI feedback ကို 150-300ms အတွင်း ထားပြီး ease-out လို ဆုံးခါနီး နှေးတဲ့ curve သုံးပါ။
  • Transition ရေးတိုင်း prefers-reduced-motion: reduce မှာ ဘာဖြစ်မလဲ တစ်ခါတည်း ဆုံးဖြတ်ပါ။

Mini challenge

Mini challenge: သက်သာပြီး reduced-motion လေးစားတဲ့ card

Hover ဒါမှမဟုတ် keyboard focus ရောက်တဲ့အခါ မြောက်တက်ပြီး shadow နဲ့ action button တွေ fade ဖြစ်လာတဲ့ card တစ်ခု ဆောက်ပါ။ transform နဲ့ opacity ကိုပဲ transition လုပ်ရပါမယ်။

  • .card က :hover နဲ့ :focus-within မှာ 200ms ease-out နဲ့ 4px မြောက်တက်ရမယ်
  • Shadow ကို box-shadow transition မလုပ်ဘဲ ::after layer ရဲ့ opacity နဲ့ fade လုပ်ပါ
  • .actions က opacity 0 ကနေ 1 ဖြစ်ရမယ်၊ Tab နဲ့ ရောက်လာတဲ့ user လည်း မြင်ရမယ်
  • prefers-reduced-motion: reduce မှာ မြောက်တက်တာကို ဖယ်ပြီး fade နှစ်ခုကိုတော့ ဆက်ထားပါ
motion-card.html
<style>
  /* TODO: .card - positioning anchor, 240px wide, padding, border, 12px radius,
     transition for transform only */

  /* TODO: .card::after - a full-size layer carrying the shadow, invisible at
     first, fading with opacity, ignoring the pointer */

  /* TODO: .card .actions - hidden with opacity, fading in */

  /* TODO: hover AND focus-within: lift the card, show the shadow and actions */

  /* TODO: reduced motion: keep the fades, remove the lift */
</style>
<div class="card">
  <h3>Trip to Bagan</h3>
  <div class="actions"><button>Edit</button> <button>Share</button></div>
</div>

ရလဒ်: Hover လုပ်တဲ့အခါ 200ms အတွင်း card က translateY(-4px) ဖြစ်ပြီး shadow layer နဲ့ action တွေရဲ့ opacity က 0 ကနေ 1 ဖြစ်ပါတယ်။ Tab နှိပ်ပြီး Edit ကို focus ရောက်ရင်လည်း အတူတူပါပဲ။ Reduced motion ဖွင့်ထားရင် card က transform: none နဲ့ မရွေ့ဘဲ shadow နဲ့ action တွေ fade ပဲ ဖြစ်ပါတယ်။

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

::after ကို position: absolute; inset: 0; border-radius: inherit; နဲ့ card အရွယ်အတိုင်း ဖုံးပြီး box-shadow ကို အဲဒီ layer ပေါ်မှာ အသေထားပါ — opacity ကိုပဲ ပြောင်းရပါမယ်။ Reduced motion block ထဲမှာ hover/focus-within state ရဲ့ transform ကို none ပြန်ထားရုံပါပဲ။

အဖြေ ကြည့်ရန်
motion-card.html
<style>
  .card {
    position: relative;
    width: 240px;
    padding: 16px;
    border: 1px solid #ddd;
    border-radius: 12px;
    transition: transform 200ms ease-out;
  }
  .card::after {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: inherit;
    box-shadow: 0 8px 20px rgb(0 0 0 / 0.15);
    opacity: 0;
    transition: opacity 200ms ease-out;
    pointer-events: none;
  }
  .card .actions {
    opacity: 0;
    transition: opacity 200ms ease-out;
  }
  .card:hover,
  .card:focus-within { transform: translateY(-4px); }
  .card:hover::after,
  .card:focus-within::after,
  .card:hover .actions,
  .card:focus-within .actions { opacity: 1; }
  @media (prefers-reduced-motion: reduce) {
    .card:hover,
    .card:focus-within { transform: none; }
  }
</style>
<div class="card">
  <h3>Trip to Bagan</h3>
  <div class="actions"><button>Edit</button> <button>Share</button></div>
</div>

Quick Quiz 1 / 3

.box { transition: opacity 300ms ease 100ms; } မှာ 100ms က ဘာလဲ။

Quick Quiz 2 / 3

transition ကို .card:hover rule ထဲမှာပဲ ရေးထားရင် mouse ဖယ်လိုက်တဲ့အခါ ဘာဖြစ်မလဲ။

Quick Quiz 3 / 3

Notification panel ကို ညာဘက်ကနေ ဆွဲထုတ်တဲ့ animation အတွက် ဘယ် transition က phone တွေမှာ အချောမွေ့ဆုံး ဖြစ်နိုင်လဲ။

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

  • Transition က computed value ပြောင်းတဲ့အခါ property, duration, timing function, delay အတိုင်း ကြားတန်ဖိုးတွေကို interpolate လုပ်ပါတယ်။
  • Transition ကို base state ပေါ်မှာ ထားရင် နှစ်ဘက်စလုံး ချောမွေ့ပြီး transition: all ကို ရှောင်ပါ။
  • transform နဲ့ opacity က layout မထိလို့ သက်သာပြီး width, top, margin လို property တွေက frame တိုင်း layout ပြန်တွက်စေပါတယ်။
  • prefers-reduced-motion: reduce မှာ ရွေ့လျားမှုကို ဖယ်ပြီး fade၊ အရောင်ပြောင်းတာလို feedback ကို ဆက်ထားပါ။

CSS transitions — MDN Web Docs

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

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

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

Transitions | Thuta Learning