فهرست سرفصل‌های CSS
خانه (Home) CSS مقدمه CSS (Introduction) سینتکس CSS (Syntax) انتخاب گرهای CSS (Selectors) نحوه استفاده از CSS (How To) کامنت ها در CSS (Comments) ارورها در CSS (Errors) رنگ ها در CSS (Colors) رنگ ها - RGB و RGBA رنگ ها - هگز (HEX Colors) رنگ ها - HSL و HSLA پس زمینه (Backgrounds) پس زمینه - تصویر (Backgrounds Image) پس زمینه - تکرار تصویر (Backgrounds Image Repeat) پس زمینه - پیوست (Background Attachment) پس زمینه - کوتاه نویسی (Background Shorthand) کادرها (Borders) کادرها - عرض (Borders Width) کادرها - رنگ (Borders Color) کادرها - کناره ها (Borders Sides) کادرها - کوتاه نویسی (Shorthand Border Property) کادرها - گرد (Rounded Borders) لایه (Padding) ارتفاع، عرض و حداکثر عرض (Height, Width and Max-width) مدل جعبه ای (Box Model) خط پیرامونی (Outline) خط پیرامونی - ضخامت (Outline Width) خط پیرامونی - رنگ (Outline Color) خط پیرامونی - کوتاه نویسی (Outline Shorthand) خط پیرامونی - آفست (Outline Offset) متن - رنگ (Text Color) متن - چینش (Text Alignment) متن - تزئینات (Text Decoration) متن - تبدیل حروف (Text Transformation) متن - فاصله گذاری (Text Spacing) متن - سایه (Text Shadow) فونت ها (Fonts) فونت های وب سیف (Font Web Safe) فونت - جایگزین ها (Font Fallbacks) فونت - سبک (Font Style) فونت - اندازه (Font Size) فونت - گوگل (Font Google) فونت - کوتاه نویسی (Font Shorthand) آیکن ها در CSS (CSS Icons) لینک ها در CSS (CSS Links) فهرست ها در CSS (CSS Lists) جدول - کادرها (Table Borders) جدول - اندازه (Table Size) جدول - تراز (Table Alignment) جدول - استایل دهی (Table Styling) جدول - واکنش گرا (Table Responsive) نمایش/قابلیت دید (Display) حداکثر عرض (Max-width) موقعیت دهی (Position) شاخص Z (Z-index) سرریز (Overflow) شناوری (Float) شناوری - Float (Clear) شناوری - مثال ها (Float Examples) اینلاین-بلاک (Inline-block) تراز کردن (Align) ترکیب گرها (Combinators) کلاس های شبه (Pseudo-classes) عناصر شبه (Pseudo-elements) شفافیت/اپاسیتی (Opacity) نوار ناوبری - مقدمه (Navbar Intro) نوار ناوبری - عمودی (Vertical Navbar) نوار ناوبری - افقی (Horizontal Navbar) منوهای کشویی (Dropdowns) گالری تصاویر (Image Gallery) اسپرایت های تصویر (Image Sprites) انتخاب گرهای ویژگی (Attribute Selectors) فرم ها (Forms) شمارنده ها (Counters) واحدها (Units) اختصاصیت/اهمیت انتخاب گر (Specificity) !important (!important) توابع ریاضی (Math Functions) بهینه سازی/کارایی (Optimization) دسترس پذیری (Accessibility) چیدمان وب سایت (Website Layout) گوشه های گرد (Rounded Corners) تصاویر کادر (Border Images) پس زمینه - چندگانه (Multiple Backgrounds) پس زمینه - اندازه (Background Size) پس زمینه - مبدأ (Background Origin) پس زمینه - کلیپ (Background Clip) رنگ ها - پیشرفته (Colors - Advanced) کلیدواژه های رنگ (Color Keywords) گرادیان خطی (Linear Gradients) گرادیان شعاعی (Radial Gradients) گرادیان مخروطی (Conic Gradients) افکت های سایه (Shadow Effects) سایه جعبه (Box Shadow) افکت های متن (CSS Text Effects) فونت های سفارشی (CSS Custom Fonts) تبدیل های دو بعدی (CSS 2D Transforms) تبدیل های سه بعدی (CSS 3D Transforms) ترنزیشن ها (CSS Transitions) انیمیشن ها (CSS Animations) راهنما/تولتیپ (CSS Tooltips) استایل دهی تصویر (CSS Image Styling) مودال تصویر (CSS Image Modal) مرکزچین کردن تصویر (Image Centering) فیلترهای تصویر (Image Filters) اشکال تصویر (Image Shapes) object-fit (object-fit) object-position (object-position) ماسک گذاری - PNG (Masking - PNG) ماسک گذاری - گرادیان ها (Masking - Gradients) ماسک گذاری - SVG (Masking - SVG) دکمه ها (Buttons) صفحه بندی (Pagination) ستون های چندگانه (Multiple Columns) رابط کاربری (User Interface) تابع var() (The var() Function) بازنویسی متغیرها (Overriding Variables)
CSS

CSS — انیمیشن ها (CSS Animations)

آخرین بروزرسانی: 1404/07/19

انیمیشن های CSS (CSS Animations)

«انیمیشن های CSS» به شما اجازه می دهد بدون جاوااسکریپت، استایل عناصر را به صورت تدریجی تغییر دهید. برای این کار، کی فریم ها (@keyframes) را تعریف کرده و سپس آن را به عنصر متصل می کنید.

مقدمه ای بر انیمیشن های CSS

یک انیمیشن می تواند یک یا چند ویژگی CSS را در زمان های مختلف تغییر دهد. برای اجرا، نام انیمیشن و مدت زمان را با animation-name و animation-duration مشخص کنید.

قانون @keyframes و اتصال به عنصر

درون @keyframes می توانید از from/to یا درصدها استفاده کنید و سپس آن را به عنصر ببندید.

نمونه ساده انیمیشن های CSS

/* The animation code */
@keyframes myAnimation { from {background-color: red;} to {background-color: yellow;} }

/* The element to apply the animation to */
div { width: 100px; height: 100px; background-color: red; animation-name: myAnimation; animation-duration: 4s; }

می توانید از درصدها برای چندین تغییر پیاپی استفاده کنید:

@keyframes myAnimation { 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: myAnimation; animation-duration: 4s; }

همچنین می توان ویژگی های دیگر مانند موقعیت را نیز تغییر داد:

@keyframes myAnimation { 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: myAnimation; animation-duration: 4s; }

تأخیر شروع با animation-delay

برای تعیین تأخیر شروع، از animation-delay با ثانیه یا میلی ثانیه استفاده کنید. مقدار منفی یعنی انیمیشن انگار از قبل آغاز شده است.

div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: myAnimation; animation-duration: 4s; animation-delay: 2s; }
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: myAnimation; animation-duration: 4s; animation-delay: -2s; }

تکرار با animation-iteration-count

با animation-iteration-count تعداد تکرار را تعیین کنید؛ مقدار infinite یعنی بی نهایت.

div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: myAnimation; animation-duration: 4s; animation-iteration-count: 3; }
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: myAnimation; animation-duration: 4s; animation-iteration-count: infinite; }

جهت اجرا با animation-direction

مقادیر normal، reverse، alternate و alternate-reverse جهت پخش را کنترل می کنند.

div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: myAnimation; animation-duration: 4s; animation-direction: reverse; }
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: myAnimation; animation-duration: 4s; animation-iteration-count: 2; animation-direction: alternate; }
div { width: 100px; height: 100px; position: relative; background-color: red; animation-name: myAnimation; animation-duration: 4s; animation-iteration-count: 2; animation-direction: alternate-reverse; }

منحنی سرعت با animation-timing-function

برای کنترل سرعت از ease، linear، ease-in، ease-out، ease-in-out یا cubic-bezier() استفاده کنید.

#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;}

حالت پرشدن با animation-fill-mode

برای اعمال استایل قبل از شروع یا بعد از پایان انیمیشن، از none، forwards، backwards یا both استفاده کنید.

div { width: 100px; height: 100px; background: red; position: relative; animation-name: myAnimation; animation-duration: 3s; animation-fill-mode: forwards; }
div { width: 100px; height: 100px; background: red; position: relative; animation-name: myAnimation; animation-duration: 3s; animation-delay: 2s; animation-fill-mode: backwards; }
div { width: 100px; height: 100px; background: red; position: relative; animation-name: myAnimation; animation-duration: 3s; animation-delay: 2s; animation-fill-mode: both; }

میان بر animation در انیمیشن های CSS

می توانید همه مقادیر را به ترتیب در ویژگی animation بنویسید.

div { animation-name: myAnimation; animation-duration: 5s; animation-timing-function: linear; animation-delay: 2s; animation-iteration-count: infinite; animation-direction: alternate; }
div { animation: myAnimation 5s linear 2s infinite alternate; }

نکته: برای شروع ساده، از نمونه های بالا کپی کرده و سپس نام، زمان و منحنی را مطابق نیاز تنظیم کنید.

مطالب مرتبط: ترنزیشن های CSS، CSS Tooltip و راهنمای انیمیشن های CSS.

بیشتر بدانید: استاندارد CSS Animations Level 1 و مرجع انیمیشن های CSS. همچنین برای جستجو از عبارت انیمیشن های CSS استفاده کنید.