CSS Animations
In this page:
@keyframes animation-name {
from { property: value; }
to { property: value; }
}
selector {
animation: animation-name duration timing-function iteration-count;
}
@keyframes Timelines Declare करना
@keyframes rule आपकी animation की states और timeline define करने के लिए इस्तेमाल होता है। आप percentage indicators (0% से 100% तक) या simple keyword hooks (from और to) इस्तेमाल करके keyframes specify करते हैं ताकि यह define हो कि अलग-अलग moments पर कौन-सी CSS properties बदलती हैं।
- complex, multi-state animations बनाने के लिए अपने keyframes में percentage indicators इस्तेमाल करें।
- Accepted values:
- animation-name — एक @keyframes rule का नाम
- animation-duration — time जैसे 2s या 500ms
- animation-timing-function — ease | linear | ease-in | ease-out | ease-in-out | cubic-bezier()
- animation-iteration-count — number | infinite
- animation-direction — normal | reverse | alternate | alternate-reverse
- animation-fill-mode — none | forwards | backwards | both
उदाहरण: Declaring @keyframes Timelines
<style>
@keyframes fade {
0% { opacity: 0; }
100% { opacity: 1; }
}
.box {
background: teal;
color: white;
padding: 10px;
animation: fade 2s;
}
</style>
<div class="box">Fades in using the keyframes above</div>
Custom Animations लागू करना
एक बार जब आप अपनी keyframe timeline declare कर लें, तो आप इसे animation-name और animation-duration properties इस्तेमाल करके किसी element पर लागू करते हैं। यह browser को तुरंत आपकी custom animation timeline चलाने का instruction देता है।
animation-name और animation-duration के साथ animation-timing-function: ease-in-out जैसा timing function जोड़ना motion की pacing control करता है, इसे constant linear speed से चलने के बजाय ease in और out करते हुए।
- हमेशा एक defined duration value specify करें, वरना आपकी animation default रूप से 0 हो जाएगी और चलने में fail हो जाएगी।
- Accepted values:
- animation-name: <keyframes-name> — इस्तेमाल होने वाला @keyframes rule
- animation-duration: <time> — एक cycle की length
- animation-timing-function: ease | linear | cubic-bezier() — pacing
- animation-delay: <time> — शुरू होने से पहले wait
- animation: name 2s ease 0s — shorthand
उदाहरण: Applying custom Animations
<style>
@keyframes fade {
from { opacity: 0; }
to { opacity: 1; }
}
.box {
background: teal;
color: white;
padding: 10px;
animation-name: fade;
animation-duration: 1s;
animation-timing-function: ease-in-out;
}
</style>
<div class="box">Fades in on load</div>
Iteration Counts को Control करना
Default रूप से, animations exactly एक बार चलते हैं और फिर रुक जाते हैं। animation-iteration-count property आपको यह specify करने देती है कि आप animation को कितनी बार repeat करना चाहते हैं, या इसे हमेशा के लिए loop करने के लिए infinite set करें।
animation-play-state property आपको किसी भी point पर एक animation को pause और resume करने देती है, सबसे common रूप से :hover पर paused toggle की जाती है ताकि user किसी looping animation को inspect करने के लिए freeze कर सके।
- उन्हें लगातार spinning रखने के लिए subtle loading spinners पर infinite loops इस्तेमाल करें।
- Accepted values:
- animation-iteration-count: 1 — default, एक बार चलता है
- animation-iteration-count: <number> — उतनी बार चलता है (decimals allowed हैं)
- animation-iteration-count: infinite — हमेशा के लिए loop करता है
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Controlling Iteration Counts
<style>
@keyframes spin {
to { transform: rotate(360deg); }
}
.spinner {
background: coral;
color: white;
width: 60px;
height: 60px;
display: flex;
align-items: center;
justify-content: center;
animation: spin 1s infinite;
}
.spinner:hover {
animation-play-state: paused;
}
</style>
<div class="spinner">Spins forever, hover to pause</div>
Directions को Control करना
animation-direction property specify करती है कि आपकी animation timeline कैसे play होनी चाहिए, normal, reverse (backward चलता है), alternate (forward फिर backward चलता है), और alternate-reverse options देते हुए।
- smooth, natural-looking back-and-forth animations बनाने के लिए alternate direction setting इस्तेमाल करें।
- Accepted values:
- normal — default, हर cycle forwards
- reverse — हर cycle backwards
- alternate — forwards फिर backwards
- alternate-reverse — backwards फिर forwards
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Controlling Directions
<style>
@keyframes slide {
from { transform: translateX(0); }
to { transform: translateX(50px); }
}
.box {
background: royalblue;
color: white;
padding: 10px;
animation: slide 1s alternate infinite;
}
</style>
<div class="box">Plays forward, then backward, then forward again</div>
Fill Modes Manage करना (animation-fill-mode)
Default रूप से, जब कोई animation चलना खत्म कर देती है, तो element instantly अपनी original pre-animated styles पर वापस snap हो जाता है। animation-fill-mode property आपको browser को animation की start या end styles को उसके चलने के बाद permanently लागू करने का instruction देने देती है।
- अपनी fill-mode को forwards set करें ताकि आपके elements animation खत्म होने के बाद अपनी final animated styles बनाए रखें।
- Accepted values:
- none — default, animation के बाहर कोई styles लागू नहीं
- forwards — end के बाद आख़िरी keyframe रखता है
- backwards — delay के दौरान पहली keyframe लागू करता है
- both — दोनों behaviors लागू करता है
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Managing Fill Modes (animation-fill-mode)
<style>
@keyframes slideIn {
from { transform: translateX(-100px); }
to { transform: translateX(0); }
}
.box {
background: royalblue;
color: white;
padding: 10px;
animation: slideIn 1s forwards;
}
</style>
<div class="box">Keeps its final position after the animation ends</div>
- एक defined animation-duration specify करना भूल जाना, animation को 0 पर disabled छोड़ते हुए।
- animation-fill-mode settings की उपेक्षा करके elements को awkwardly वापस snap होने देना।
- तेज़, flashing animations को auto-play करने की कोशिश करना जो users को distract कर सकती हैं या accessibility issues trigger कर सकती हैं।
- @keyframes rule आपकी custom animation की states और timeline define करने के लिए इस्तेमाल होता है।
- अपनी animations को इन properties से configure करें: animation-name, duration, iteration-count, direction, और fill-mode।
- animation खत्म होने के बाद elements को अपनी final animated styles बनाए रखने के लिए अपनी animation-fill-mode को forwards set करें।
Standard CSS keyframe animations और core control properties हर modern web browser द्वारा natively supported हैं।
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Gradients
- CSS Shadows
- CSS Filters
- CSS Blend Modes
- CSS Transforms
- CSS 2D Transforms
- CSS 3D Transforms
- CSS Transitions
- CSS Animations
- CSS Image Styling
- CSS Image Effects
- CSS Hover Overlays
- CSS Image Modal
- CSS Image Centering
- CSS Image Shapes
- CSS Buttons
- CSS Columns
- CSS Clip Path
- CSS Shapes
- CSS Scroll Snap
- CSS Architecture: BEM
- CSS Custom Properties