Lesson 39 / الدرس 39
Transitions / الانتقالات
Make a property change smoothly instead of snapping instantly, with transition-property, -duration, -timing-function, -delay, and the shorthand that combines them.
اجعل تغيّر خاصية ما تدريجيًا بدل أن يقفز فجأة، عبر transition-property و -duration و -timing-function و -delay، والاختزال الذي يجمعها معًا.
You already know that a pseudo-class like :hover lets a rule apply only while the pointer sits over an element. By default, the change from the normal state to the :hover state is instant — one frame the button is blue, the next it is dark blue, with nothing in between. A transition fills in that gap: instead of jumping, the property animates smoothly from its old value to its new one over a duration you choose.
:hover يجعل قاعدة تُطبَّق فقط ما دام المؤشر فوق عنصر. وافتراضيًا، الانتقال من الحالة العادية إلى حالة :hover فوري: في لحظة الزر أزرق، وفي التالية أزرق داكن، بلا أي شيء بينهما. والانتقال يملأ هذه الفجوة: بدل أن تقفز الخاصية، تتحرك بسلاسة من قيمتها القديمة إلى الجديدة خلال مدة تختارها.Four properties that control a transition
| Property | Controls | Example |
|---|---|---|
| transition-property | Which property animates | background-color |
| transition-duration | How long it takes | 0.3s |
| transition-timing-function | How the speed changes over that time | ease-in-out |
| transition-delay | How long to wait before starting | 0.1s |
All four together describe one transition. Only transition-property and transition-duration are required for anything visible to happen.
transition-property و transition-duration..btn {
background-color: navy;
transition-property: background-color;
transition-duration: 0.3s;
transition-timing-function: ease-in-out;
}
.btn:hover {
background-color: tomato;
}
The shorthand: transition
transition
Writing all four longhand properties gets repetitive, so almost every real stylesheet uses the shorthand instead: transition: property duration timing-function delay;. The delay is optional and can be left off entirely.
transition: الخاصية المدة دالةالتوقيت التأخير;. والتأخير اختياري ويمكن حذفه كليًا..btn {
background-color: navy;
transition: background-color 0.3s ease-in-out;
}
.btn:hover {
background-color: tomato;
}
0.15s, and the color change would wait that long before it starts.
0.15s، وسينتظر تغيّر اللون تلك المدة قبل أن يبدأ.-
ease— starts slow, speeds up, ends slow (the default)ease— يبدأ بطيئًا، ثم يتسارع، ثم يبطؤ في النهاية (القيمة الافتراضية) -
linear— the same speed from start to finishlinear— السرعة نفسها من البداية إلى النهاية -
ease-in— starts slow, then speeds upease-in— يبدأ بطيئًا، ثم يتسارع -
ease-out— starts fast, then slows downease-out— يبدأ سريعًا، ثم يبطؤ
transition داخل :hover فقط هو الخطأ الكلاسيكي: .btn:hover { transition: background-color 0.3s; background-color: tomato; } تتحرك بسلاسة عند الدخول، لأن قاعدة الانتقال موجودة لحظة بدء hover — لكنها تقفز فجأة للخلف لحظة يبتعد المؤشر، لأن قاعدة .btn الأساسية لم يكن لديها انتقال خاص بها لتتحرك به. ضع transition على المُحدِّد الأساسي، لا على :hover وحده، ليتحرك الاتجاهان كلاهما.transition: background-color 0.3s;، أوضح وغالبًا أرخص على المتصفح من transition: all 0.3s;، التي تراقب كل خاصية في العنصر بحثًا عن تغيّر، حتى ما لم تقصد تحريكه أبدًا.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1. Which property controls how long a transition takes?
transition-duration is the length of the animation itself, such as 0.3s. transition-delay only controls the wait before it starts.
transition-duration هي طول الحركة نفسها، مثل 0.3s. أما transition-delay فتتحكم فقط في الانتظار قبل أن تبدأ.
2.
You wrote transition only inside a :hover rule. What happens?
transition داخل قاعدة :hover فقط. ماذا يحدث؟The base (non-hover) state has no transition of its own, so leaving :hover jumps back instantly. Put transition on the base selector for both directions to animate.
:hover يقفز فورًا. ضع transition على المُحدِّد الأساسي ليتحرك الاتجاهان.
3.
In transition: background-color 0.3s ease-in-out 0.1s;, what does the last value, 0.1s, set?
transition: background-color 0.3s ease-in-out 0.1s;، ماذا تضبط القيمة الأخيرة 0.1s؟The shorthand order is property, duration, timing-function, delay — the fourth value is always the delay, the wait before the animation starts.
Score / النتيجة: 0 / 3
Your task / مهمتك
Style a button with a base background color, and a different background color on :hover. Add a transition to the base selector, with a duration of at least 0.2s, so the color animates smoothly in both directions.
صمّم زرًا بلون خلفية أساسي، ولون خلفية مختلف عند :hover. أضف transition إلى المُحدِّد الأساسي، بمدة 0.2 ثانية على الأقل، ليتحرك اللون بسلاسة في الاتجاهين.
-
transitionis set on the base.btnselector, not only inside:hovertransitionمضبوطة على المُحدِّد الأساسي.btn، لا داخل:hoverوحده -
The duration is at least
0.2sالمدة 0.2 ثانية على الأقل -
The background color visibly animates both entering and leaving
:hoverيتحرك لون الخلفية بوضوح عند الدخول إلى:hoverوعند مغادرته