What we're making
We're creating a common spinner animation you've probably seen many times before. This is easy to create in CSS and we'll do just that but for the animation, we're going to make use of Framer Motion. With this technique, we could replace the component that is spinning and/or dynamically update it as it spins.
How to achieve the effect
I've made a quick video tutorial going through the process, it's about 3m 30s.
Because the animation is very simple, we are just making use of two props on the motion
component and some careful setup.
Making the loop
The core props we need to change is animate
, the animation we want to play (in our case we'll use rotate: 360
). This will spin our circle exactly once so the next step is to provide a transition
prop. We'll provide an object with 3 values, like so:
-
loop
is important to ensure the animation plays continuously. -
ease
is set to linear, this is important because by default, the animation will have easing which creates a completely different effect where it's slow to start and slows down again as it completes the rotation. -
duration
at 1 second gives a smooth and consistent rotation that isn't too fast, again, the default is very short which makes for a hectic and stressful loading spinner.
That's all there is to it.
Where do I go from here?
To take this effect further, you could introduce dynamic values. Colour or size changes can easily be achieved by adjusting the animate prop. The transition object can also be fine tuned by passing in different values to the exact parameters you want to change.
If we just provide loop
, ease
and duration
, it affects every property animation.
Resources
- To see the code, take a look at the GitHub repo.
- Check out my playlist of video tutorials covering animation in Framer Motion
- Take a look at the official Framer Motion documentation
Top comments (2)
in pure CSS this would basically just be:
Yep, thanks for adding an example. It’s very easy with CSS, I just thought I’d make an example of a simple step in Framer Motion. The power only comes in when you start adding dynamic changes or scheduling/orchestrating multiple animations.
With that being said, you can do amazing things with only CSS without ever touching JS!