You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
using CSS `@keyframes` !! (We'll explain more soon...) And the best part- we don't have to use JavaScript for it!
68
-
69
-
## What are animations in CSS
70
-
71
-
Before we get started, it's important to note what animations are in CSS. Animations are made up of a series of frames, or in this case, HTML elements that change over time.
CSS animations have "steps" in them (also known as keyframes) to signify each moment of change in the animation.
78
-
79
-
For example,
80
-
81
-
```
82
-
<div className="box"></div>
83
-
```
84
-
```
85
-
@keyframes colorChange {
86
-
0% { background-color: red; }
87
-
50% { background-color: yellow; }
88
-
100% { background-color: red; }
89
-
}
90
-
91
-
.box {
92
-
width: 100px;
93
-
height: 100px;
94
-
background-color: red;
95
-
animation: colorChange 2s infinite;
96
-
}
97
-
```
98
-
99
-
would look like
100
-
101
-
<RoundedImagelink="https://i.imgur.com/bBzHFra.gif"description="flashing yellow and red box" />
102
-
103
-
So at 0% of the animation, the color is red, at 50% it's yellow, and at 100% or at the end, it's red again, hence the loop!
104
-
105
-
Let's also take a look at this line of code:
106
-
107
-
```
108
-
animation: colorChange 2s infinite;
109
-
```
110
-
111
-
-`colorChange` is the name of our animation
112
-
-`2s` is the length in seconds of how long the animation lasts
113
-
-`infinite` is indicating it will loop forever
114
-
115
-
The line of code above is attached to the `.box` class from our HTML file! Animations can only show up if they are defined in the `animation:`.
116
-
117
-
## Easing
118
-
Easing in CSS animations is essential to understanding the pace in which an animation progresses over time.
119
-
120
-
<RoundedImagelink="https://i.imgur.com/9C7DeEg.gif"description="easing demo from mdn web docs" />
121
-
122
-
Some common ones you'll see:
123
-
124
-
-`linear`: Constant speed
125
-
-`ease`: Starts slow, speeds up, then slows down
126
-
-`ease-in`: Starts slow, then accelerates
127
-
-`ease-out`: Starts fast, then decelerates
128
-
-`ease-in-out`: Combines `ease-in` and `ease-out`
129
-
130
-
These can differ and are added right in the `animation:` of your CSS.
131
-
132
-
```
133
-
.tinker_bell {
134
-
animation: bounce 1s linear infinite;
135
-
}
136
-
```
137
-
138
-
**Note:** Easing can get pretty complicated based on the type of motion you want to create! You can review the open source [MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/CSS/animation-timing-function) for more information on easing and animation examples.
Here, we're using the CSS function `rotate()` to establish which degree we want to start and end at. In this example, we're using the keywords `from` and `to` which are shortcuts for `0%` and `100%`.
177
-
178
-
The result of the club penguin sensei looks like this 🔥🔥
179
-
180
-
<RoundedImagelink="https://i.imgur.com/YVnPHFA.gif"description="spinning club penguin sensei" />
181
-
182
-
183
-
## Hovering Animation
184
-
185
-
Imagine you're playing a video game and you're choosing which character to play as, or you're in an idle mode.
Here, we're translating (aka moving) the object's Y position by 30 pixels halfway through the animation, and returning it to its original spot at the end using a `linear` ease.
To achieve this animation speed, we set the length of time to `1s`, but you can play around with this value to get the effect you want!
224
-
225
-
226
-
## Conclusion
227
-
228
-
Congrats! You've learned the basics of CSS image animation, and as you can tell, it's a world of opportunities to add flair to your website or project! ✨✨✨
Share your projects with the team [@codedex_io](https://www.twitter.com/codedex_io) and me, [@exrlla](https://www.twitter.com/exrlla)! Let us know what you come up with! <spanstyle="font-size: 16px; line-height: 1;"><imgsrc="https://i.imgur.com/FOR1yp5.gif"alt="emoji"style="height: 1em; width: auto; vertical-align: middle;" /></span>
0 commit comments