CSS 动画
CSS 动画
يمكن للـ CSS تحقيق تأثيرات تحريك العناصر في HTML دون استخدام JavaScript أو Flash!
في هذا الفصل، ستتعلم ما يلي:
@keyframes
animation-name
animation-duration
animation-delay
animation-iteration-count
animation-direction
animation-timing-function
animation-fill-mode
animation
دعم المتصفح للتأثير
الرقم في الجدول يوضح إصدار المتصفح الذي يدعم هذه الخاصية لأول مرة.
tarihin kifanin za a kaiwa. | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
@keyframes | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-name | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-duration | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-delay | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-iteration-count | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-direction | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-timing-function | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation-fill-mode | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
animation | 43.0 | 10.0 | 16.0 | 9.0 | 30.0 |
ما هو تأثير CSS؟
التحريك يجعل العنصر يتحرك من نمط إلى آخر.
يمكنك تغيير أي عدد من خصائص CSS.
لإستخدام تأثير التحريك، يجب أن تخصص بعض المقاطع الرئيسية للتحريك.
المقاطع الرئيسية تحتوي على الأنماط التي يمتلكها العنصر في وقت معين.
@keyframes القاعدة
إذا كنت في @keyframes
في القاعدة، يتم تحديد الأنماط التي سيتم تطبيقها على التحريك.
لجعل التحريك يعمل، يجب ربط التحريك بعنصر معين.
الامثلة التالية ستحمل "example" تحريك <div> العنصر. التحريك سيستمر لمدة 4 ثوانٍ، حيث سيغير لون الخلفية لـ <div> من "أحمر" إلى "أصفر":
tarihin kifanin za a kaiwa.
/* 动画代码 */ @keyframes example { from {background-color: red;} to {background-color: yellow;} } /* تطبيق تأثير التحريك على هذا العنصر */ div { width: 100px; height: 100px; background-color: red; animation-name: example; animation-duration: 4s; }
ملاحظة:animation-duration
property تحدد الوقت الذي يستغرقه التحريك لإنجاز العمل. animation-duration
إذا لم تكن قد خصصت لـ property، فإن التحريك لن يحدث لأن القيمة الافتراضية هي 0s (0 ثانية).
في الأمثلة السابقة، باستخدام الكلمات المفتاحية "من" و "إلى" (تمثل 0% (البداية) و 100% (الإنهاء))، قمنا بضبط وقت التغيير في الأنماط.
يمكنك أيضًا استخدام القيم المئوية. باستخدام القيم المئوية، يمكنك إضافة أي عدد من التغييرات في الأنماط حسب الحاجة.
الامثلة التالية ستغير لون الخلفية لـ <div> عند اكتمال التحريك 25%، 50% وتحريك 100%:
tarihin kifanin za a kaiwa.
/* 动画代码 */ @keyframes example { 0% {background-color: red;} 25% {background-color: yellow;} 50% {background-color: blue;} 100% {background-color: green;} } /* 应用动画的元素 */ div { width: 100px; height: 100px; background-color: red; animation-name: example; animation-duration: 4s; }
下面的例子将在动画完成 25%,完成 50% 以及动画完成 100% 时更改背景颜色和
tarihin kifanin za a kaiwa.
/* 动画代码 */ @keyframes example { 0% {background-color:red; left:0px; top:0px;} 25% {background-color:yellow; left:200px; top:0px;} 50% {background-color:blue; left:200px; top:200px;} 75% {background-color:green; left:0px; top:200px;} 100% {background-color:red; left:0px; top:0px;} } /* 应用动画的元素 */ div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; }
延迟动画
animation-delay
属性规定动画开始的延迟时间。
下面的例子在开始动画前有 2 秒的延迟:
tarihin kifanin za a kaiwa.
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-delay: 2s; }
负值也是允许的。如果使用负值,则动画将开始播放,如同已播放 N 秒。
nuna ce tsa hoto hoto a hoto hoto, kuma a hoto hoto, dali a hoto hoto 2 ki:
tarihin kifanin za a kaiwa.
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-delay: -2s; }
kai kai tsa hoto hoto a hoto hoto:
animation-iteration-count
属性 tsa hoto hoto na ki kai kuma a hoto hoto:
nuna ce tsa hoto hoto 3 ki kai kuma a hoto hoto:
tarihin kifanin za a kaiwa.
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: 3; }
nuna ce tsa ki kai kuma tsa hoto hoto, kuma a hoto hoto:
tarihin kifanin za a kaiwa.
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: infinite; }
a hoto hoto a hoto a hoto, kuma a hoto hoto
animation-direction
属性 tsa hoto hoto na ki kai, a hoto hoto, tsa hoto hoto a hoto hoto, kuma a hoto hoto:
animation-direction
يمكن للصفة قبول القيم التالية:
normal
- tsa hoto hoto a hoto hoto (a hoto hoto). kai kai na kaireverse
- tsa hoto hoto a hoto a hoto (a hoto hoto):alternate
- tsa hoto hoto a hoto a hoto, kuma a hoto hotoalternate-reverse
- tsa hoto hoto a hoto a hoto, kuma a hoto hoto
nuna ce tsa hoto hoto a hoto a hoto:
tarihin kifanin za a kaiwa.
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-direction: reverse; }
nuna ce tsa kai kuma tsa hoto hoto, kuma a hoto hoto:
tarihin kifanin za a kaiwa.
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: 2; animation-direction: alternate; }
nuna ce tsa ki kai kuma tsa hoto, kuma a hoto hoto:
tarihin kifanin za a kaiwa.
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: 2; animation-direction: alternate-reverse; }
تحديد منحنى سرعة الحركة للحركة
animation-timing-function
تعين الصفة تحدد منحنى سرعة الحركة.
animation-timing-function
يمكن للصفة قبول القيم التالية:
ease
- تحديد حركة ببطء في البداية، ثم بسرعة، ثم ببطء في النهاية (الافتراضي)linear
- تحديد حركة بنفس السرعة من البداية إلى النهايةease-in
- تحديد حركة ببطء في البدايةease-out
- تحديد حركة ببطء في النهايةease-in-out
- تحديد حركة ببطء في البداية والانتهاءcubic-bezier(n,n,n,n)
- قم بتعيين القيم الخاصة بك في وظيفة البيسي كوبييرتس
تظهر الامثلة التالية بعض السرعات المختلفة التي يمكن استخدامها:
tarihin kifanin za a kaiwa.
#div1 {animation-timing-function: linear;} #div2 {animation-timing-function: ease;} #div3 {animation-timing-function: ease-in;} #div4 {animation-timing-function: ease-out;} #div5 {animation-timing-function: ease-in-out;}
تحديد نمط التعبئة للحركة
لا تؤثر الحركات في CSS على العنصر قبل تشغيل اول اطار رئيسي أو بعد تشغيل اخر اطار رئيسي.animation-fill-mode
يمكن للصفة تغيير هذا السلوك.
عند عدم تشغيل الحركة (قبل البدء، بعد الانتهاء، أو كلاهما عند الانتهاء)،animation-fill-mode
تعين الصفة تحدد نمط العنصر المستهدف.
يمكن للصفة animation-fill-mode قبول القيم التالية:
none
- القيمة الافتراضية. الحركة لن تطبق أي نمط على العنصر قبل أو بعد تنفيذها.forwards
- العنصر سيحافظ على قيم النمط التي تم تعيينها في اخر اطار رئيسي (يعتمد على animation-direction وanimation-iteration-count).backwards
- العنصر سيحصل على قيم النمط التي تم تعيينها في اخر اطار رئيسي (يعتمد على animation-direction) وسيحافظ على هذه القيم خلال فترة التأخير للحركة.both
- الحركة ستتبع في نفس الوقت القواعد السابقة واللاحقة، مما يوسع خصائص الحركة في كلا الاتجاهين.
الامثلة التالية تجعل عناصر <div> تحافظ على قيم النمط من اخر اطار رئيسي عند انتهاء الحركة:
tarihin kifanin za a kaiwa.
div { width: 100px; height: 100px; background: red; position: relative; animation-name: example; animation-duration: 3s; animation-fill-mode: forwards; }
tarihin kifanin za a kaiwa na a cire a kaiya kafin kaiwa (na wuri na a kaiya) <div> za a samu wuri na a kaiwa na farko na kifanin.
tarihin kifanin za a kaiwa.
div { width: 100px; height: 100px; background: red; position: relative; animation-name: example; animation-duration: 3s; animation-delay: 2s; animation-fill-mode: backwards; }
tarihin kifanin za a kaiwa na a cire a kaiya kafin kaiwa <div> za a samu wuri na a kaiwa na farko na kifanin, kuma a kaiya farko na wuri na kifanin a kaiya.
tarihin kifanin za a kaiwa.
div { width: 100px; height: 100px; background: red; position: relative; animation-name: example; animation-duration: 3s; animation-delay: 2s; animation-fill-mode: both; }
tarihin kifanin za a kaiwa na a raba.
tarihin kifanin za a kaiwa na a raba:
tarihin kifanin za a kaiwa.
div { animation-name: example; animation-duration: 5s; animation-timing-function: linear; animation-delay: 2s; animation-iteration-count: infinite; animation-direction: alternate; }
tarihin kifanin za a kaiwa na a raba. animation
tarihin kifanin za a kaiwa za a cire za a cire za a kaiwa.
tarihin kifanin za a kaiwa.
div { animation: example 5s linear 2s infinite alternate; }
tarihin kifanin za a kaiwa.
duba tabbin a yanzu na kifanin @keyframes da kowace CSS tarihin kifanin:
tarihin kifanin za a kaiwa. | tarihin kifanin za a kaiwa. |
---|---|
@keyframes | tarihin kifanin za a kaiwa. |
animation | tarihin kifanin za a kaiwa na a raba. |
animation-delay | tarihin kifanin za a kaiwa na a kaiya. |
animation-direction | tarihin kifanin za a cire zuwa kudu, zuwa gabas ko zuwa kuduwa ko gabas. |
animation-duration | tarihin kifanin za a kaiwa da wuri da yau cikakken. |
animation-fill-mode | tarihin kifanin za a zama ba cire a kaiwa, kafin kaiwa, tana zama, ama abin da yau. |
animation-iteration-count | tarihin kifanin za a cire. |
animation-name | tarihin kifanin @keyframes. |
animation-play-state | 规定动画是运行还是暂停。 |
animation-timing-function | 规定动画的速度曲线。 |