Lesson 40 / الدرس 40
Transforms & animations / التحويلات والحركات
Move, rotate and scale an element with transform without disturbing the layout around it, then build a looping animation with @keyframes.
حرّك عنصرًا ودوّره وكبّره أو صغّره باستخدام transform دون أن تُخل بالتخطيط من حوله، ثم ابنِ حركة متكررة بواسطة @keyframes.
Properties like margin or top move an element by pushing everything around it too — the box model shifts, and neighboring elements react. transform is different: it moves, rotates or resizes an element visually, on top of the page, without changing the space it reserves in the layout at all. Combined with the transition you just learned, it is how a hovered card lifts or a button grows slightly without anything around it jumping out of the way.
margin أو top تُحرّك عنصرًا عبر دفع كل ما حوله أيضًا — نموذج الصندوق يتزحزح، والعناصر المجاورة تتفاعل. أما transform فمختلفة: تُحرّك العنصر أو تُدوّره أو تُغيّر حجمه بصريًا، فوق الصفحة، دون أن تُغيّر المساحة التي يحجزها في التخطيط على الإطلاق. ومع transition الذي تعلمته للتو، هذه هي الطريقة التي ترتفع بها بطاقة عند hover أو يكبر بها زر قليلًا دون أن يقفز أي شيء حوله جانبًا.
Three transform functions: translate, rotate, scale
translate و rotate و scale.card {
transition: transform 0.2s ease-out;
}
.card:hover {
transform: translateY(-8px);
}
translateY(-8px) shifts the element 8 pixels up. translateX() moves it sideways, and translate(x, y) does both at once. Hover the card — it lifts, and nothing else on the page moves to make room, because as far as layout is concerned it never left.
translateY(-8px) تُزيح العنصر 8 بكسل للأعلى. و translateX() تُحرّكه أفقيًا، و translate(x, y) تفعل الاثنين معًا. مرّر المؤشر فوق البطاقة — ترتفع، ولا يتحرك أي شيء آخر في الصفحة ليفسح لها مكانًا، لأنها من ناحية التخطيط لم تغادر مكانها أبدًا..icon {
transition: transform 0.3s ease-in-out;
}
.icon:hover {
transform: rotate(20deg) scale(1.2);
}
rotate(20deg) turns the element, and scale(1.2) grows it to 120% of its size — scale(0.8) would shrink it instead. Listing more than one function inside transform, separated by spaces, applies all of them together.
rotate(20deg) تُدير العنصر، و scale(1.2) تُكبّره إلى 120% من حجمه — و scale(0.8) تُصغّره بدلًا من ذلك. وسرد أكثر من دالة داخل transform، مفصولة بمسافات، يُطبّقها جميعًا معًا.
A repeating animation with @keyframes
@keyframes
A transition only ever runs in response to something changing, like :hover. An animation plays on its own, with no trigger needed, and can repeat forever. First you describe the stages inside a named @keyframes rule using percentages — 0% is the start, 100% is the end, and you can add stops in between. Then you attach it to an element with animation-name plus at least animation-duration.
:hover. أما الحركة فتُشغَّل من تلقائها، بلا حاجة إلى مُحفِّز، ويمكن أن تتكرر إلى الأبد. تصف أولًا مراحلها داخل قاعدة @keyframes مُسمّاة باستخدام نسب مئوية — 0% هي البداية، و 100% هي النهاية، ويمكنك إضافة محطات بينهما. ثم تربطها بعنصر عبر animation-name مع animation-duration على الأقل.@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.15); }
100% { transform: scale(1); }
}
.badge {
animation-name: pulse;
animation-duration: 1.2s;
animation-iteration-count: infinite;
}
animation-name must match the @keyframes name exactly, and animation-iteration-count: infinite is what makes it loop instead of running once.
animation-name اسم @keyframes تمامًا، و animation-iteration-count: infinite هي ما يجعلها تتكرر بدل أن تعمل مرة واحدة.prefers-reduced-motion. لذا غلّف أي حركة مستمرة بحيث لا تعمل إلا لمن لم يطلب تقليلها: @media (prefers-reduced-motion: no-preference) { … }. فتبقى الشارة أعلاه تنبض لبقية الزوار، وتكفّ عن كونها مشكلة لمن كانت مشكلة له.| Property | Controls |
|---|---|
| animation-name | Which @keyframes rule to play |
| animation-duration | How long one cycle takes |
| animation-timing-function | How the speed changes during a cycle |
| animation-iteration-count | How many times it repeats — a number, or infinite |
The animation properties used in this lesson. They can also be combined into an animation shorthand, in this same order.
animation، بالترتيب نفسه.animation-name — أو نسيان تسمية قاعدة @keyframes بالاسم نفسه — يفشل تمامًا كما يفشل متغيّر CSS بخطأ إملائي: بصمت. لا يظهر خطأ، ولا يتحرك شيء، ويبقى العنصر ساكنًا دون أي إشارة إلى السبب. فإن لم تعمل حركة على الإطلاق، قارن الاسمين حرفًا حرفًا قبل التحقق من أي شيء آخر.transform و opacity يبقى سلسًا حتى على جوال بطيء، لأن المتصفح يستطيع تحريك العنصر المرسوم بالفعل دون إعادة حساب تخطيط بقية الصفحة. أما تحريك width أو top أو margin فيفرض تلك الإعادة في كل إطار، ولهذا غالبًا ما تبدو الصناديق المتحركة متقطعة.Try it live / جرّب بنفسك
Check yourself / اختبر نفسك
1. Which function moves an element visually without affecting the layout of elements around it?
transform: translate() shifts an element on top of the page without changing the space it reserves in the layout. margin and padding are box model properties that do affect layout.
transform: translate() تُزيح العنصر فوق الصفحة دون أن تُغيّر المساحة التي يحجزها في التخطيط. أما margin و padding فخاصيتا نموذج صندوق تؤثران في التخطيط فعلًا.
2.
What connects a @keyframes rule to the element that should play it?
@keyframes بالعنصر الذي يجب أن يشغّلها؟animation-name must exactly match the name written after @keyframes. A mismatch fails silently, with nothing animating.
animation-name الاسم المكتوب بعد @keyframes تمامًا. وأي اختلاف يفشل بصمت، دون أن يتحرك شيء.
3.
Why is animating transform usually smoother than animating top or width?
transform عادة أكثر سلاسة من تحريك top أو width؟transform and opacity can be animated without recalculating layout, so the browser can keep the motion smooth even under load.
Score / النتيجة: 0 / 3
Your task / مهمتك
Build a small notification badge that pulses continuously using @keyframes and animation, and a button that lifts and grows slightly on :hover using transform with a transition.
ابنِ شارة إشعار صغيرة تنبض باستمرار باستخدام @keyframes و animation، وزرًا يرتفع ويكبر قليلًا عند :hover باستخدام transform مع transition.
-
Defines an
@keyframesrule and applies it with a matchinganimation-nameتُعرّف قاعدة@keyframesوتُطبّقها بـanimation-nameمطابق -
The badge repeats forever with
animation-iteration-count: infiniteالشارة تتكرر إلى الأبد بـanimation-iteration-count: infinite -
The button uses
transformon:hover, paired with atransitionon its base selector الزر يستخدمtransformعند:hover، معtransitionعلى مُحدِّده الأساسي