رسوم متحركة CSS
- الصفحة السابقة انتقالات CSS
- الصفحة التالية إشارات 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
دعم المتصفح للحركة
الرقم في الجدول يشير إلى إصدار المتصفح الأول الذي يدعم هذه الخاصية بالكامل.
属性 | كروم | IE | فايرفوكس | سفاري | أوبرا |
---|---|---|---|---|---|
@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.
لإستخدام الحركة CSS، يجب أولاً تحديد بعض المحاور النقاط للحركة.
المحاور النقاط تحتوي على النمط الذي يمتلك العنصر في وقت معين.
@keyframes القاعدة
إذا كنت في @keyframes
في القاعدة، يتم تحديد النمط CSS، وستتغير الحركة تدريجيًا من النمط الحالي إلى النمط الجديد في وقت معين.
لجعل الحركة تعمل، يجب ربط الحركة ببعض العناصر.
في المثال التالي، سيتم ربط "example" الحركة بـ <div> العنصر. ستستمر الحركة لمدة 4 ثوانٍ، مع تغيير لون الخلفية لـ <div> من "الأحمر" إلى "الأصفر":
实例
/* 动画代码 */ @keyframes example { من {لون الخلفية: الأحمر;} إلى {لون الخلفية: الأصفر;} } /* تطبيق تأثير الحركة على هذا العنصر */ div { width: 100px; height: 100px; background-color: red; animation-name: example; animation-duration: 4s; }
ملاحظة:animation-duration
تعريف وقت الحركة المطلوب لإنجازها. إذا لم يتم تحديد animation-duration
إذا لم تكن قد قمت بتعريف هذه الخاصية، فإن الحركة لن تحدث لأن القيمة الافتراضية هي 0s (0 ثانية).
في المثال السابق، من خلال استخدام الكلمات المفتاحية "من" و"إلى" (تمثل 0% (البداية) و100% (الانتهاء))، قمنا بضبط وقت التغيير في النمط.
يمكنك أيضًا استخدام النسب المئوية. من خلال استخدام النسب المئوية، يمكنك إضافة أي عدد من التغييرات في النمط حسب الحاجة.
في المثال التالي، سيتم تغيير لون الخلفية لـ <div> عند انتهاء الحركة 25%, و50% والانتهاء من الحركة 100%:
实例
/* 动画代码 */ @keyframes example { 0% {لون الخلفية: الأحمر;} 25% {لون الخلفية: الأصفر;} 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% 时更改背景颜色和 <div> 元素的位置:
实例
/* 动画代码 */ @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 秒的延迟:
实例
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-delay: 2s; }
负值也是允许的。如果使用负值,则动画将开始播放,如同已播放 N 秒。
在下面的例子中,动画将开始播放,就好像它已经播放了 2 秒钟一样:
实例
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-delay: -2s; }
设置动画应运行多少次
animation-iteration-count
属性指定动画应运行的次数。
下面的例子在停止前把动画运行 3 次:
实例
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: 3; }
下面的例子使用值 "infinite" 使动画永远持续下去:
实例
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: infinite; }
反向或交替运行动画
animation-direction
属性指定是向前播放、向后播放还是交替播放动画。
animation-direction
يمكن للخصوص أن يتخذ من القيم التالية:
normal
- 动画正常播放(向前)。默认值reverse
- 动画以反方向播放(向后)alternate
- 动画先向前播放,然后向后alternate-reverse
- 动画先向后播放,然后向前
下例将以相反的方向(向后)运行动画:
实例
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-direction: reverse; }
下面的例子使用值 "alternate" 使动画先向前运行,然后向后运行:
实例
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: example; animation-duration: 4s; animation-iteration-count: 2; animation-direction: alternate; }
下面的例子使用值 "alternate-reverse" 使动画先向后运行,然后向前运行:
实例
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)
- تعريف قيمك الخاصة في دالة البيزيير الثلاثية
تظهر هذه الأمثلة بعض السيناريوهات التي يمكن استخدامها لتحديد منحنيات السرعة المختلفة:
实例
#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) وسيحتفظ بالقيمة خلال فترة التأخير للتحريك.الاثنين
- التحريك سيتبع في نفس الوقت القواعد السابقة واللاحقة، مما يمتد خصائص التحريك في كلا الاتجاهين.
النموذج التالي يتيح لـ <div> أن يحتفظ بالقيم النمطية من الإطار الرئيسي عند انتهاء التحريك:
实例
div { width: 100px; height: 100px; background: red; position: relative; animation-name: example; animation-duration: 3s; animation-fill-mode: forwards; }
下面的例子在动画开始之前(在动画延迟期间)使 <div> 元素获得由第一个关键帧设置的样式值:
实例
div { width: 100px; height: 100px; background: red; position: relative; animation-name: example; animation-duration: 3s; animation-delay: 2s; animation-fill-mode: backwards; }
下面的例子在动画开始之前使 <div> 元素获得第一个关键帧设置的样式值,以及在动画结束时保留最后一个关键帧的样式值:
实例
div { width: 100px; height: 100px; background: red; position: relative; animation-name: example; animation-duration: 3s; animation-delay: 2s; animation-fill-mode: both; }
动画简写属性
下例使用六种动画属性:
实例
div { animation-name: example; animation-duration: 5s; animation-timing-function: linear; animation-delay: 2s; animation-iteration-count: infinite; animation-direction: alternate; }
使用简写的 animation
属性也可以实现与上例相同的动画效果:
实例
div { animation: example 5s linear 2s infinite alternate; }
CSS 动画属性
下表列出了 @keyframes 规则和所有 CSS 动画属性:
属性 | 描述 |
---|---|
@keyframes | 规定动画模式。 |
animation | 设置所有动画属性的简写属性。 |
animation-delay | 规定动画开始的延迟。 |
animation-direction | 定动画是向前播放、向后播放还是交替播放。 |
animation-duration | 规定动画完成一个周期应花费的时间。 |
animation-fill-mode | 规定元素在不播放动画时的样式(在开始前、结束后,或两者同时)。 |
animation-iteration-count | 规定动画应播放的次数。 |
animation-name | 规定 @keyframes 动画的名称。 |
animation-play-state | تحديد ما إذا كانت الرسوم المتحركة تعمل أو تتوقف. |
animation-timing-function | تحديد مسار سرعة الرسوم المتحركة. |
- الصفحة السابقة انتقالات CSS
- الصفحة التالية إشارات CSS