Animated – SVG Circles Loader
The animated loading icon is made up of four circles. The circles have no fill, but have alternating stroke-colours.
<svg class="loader" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 340 340">
<circle cx="170" cy="170" r="160" stroke="#E2007C"/>
<circle cx="170" cy="170" r="135" stroke="#404041"/>
<circle cx="170" cy="170" r="110" stroke="#E2007C"/>
<circle cx="170" cy="170" r="85" stroke="#404041"/>
</svg>
In our CSS, we can set some basic properties to all of our circles and then use the :nth-of-type selector to apply a different stroke-dasharray to each circle.
circle:nth-of-type(1) {
stroke-dasharray: 550;
}
circle:nth-of-type(2) {
stroke-dasharray: 500;
}
circle:nth-of-type(3) {
stroke-dasharray: 450;}
circle:nth-of-type(4) {
stroke-dasharray: 300;
}
Next, we need to create our keyframe animation. Our animation is really simple: all we need to do is to rotate the circle by 360 degrees. By placing our transformation at the 50% mark of the animation, the circle will also rotate back to its original position.
@keyframes preloader {
50% {
transform: rotate(360deg);
}
}
With our animation created, we now just need to apply it to our circles. We set the animation name; duration; iteration count and timing function. The ‘ease-in-out’ will give the animation a more playful feel.
animation-name: preloader;
animation-duration: 3s;
animation-iteration-count: infinite;
animation-timing-function: ease-in-out;
At the moment, we have our loader, but all of the elements are rotating together at the same time. To fix this, we’ll apply some delays. We’ll create our delays using a Sass for loop.
@for $i from 1 through 4 {
&:nth-of-type(#{$i}) {
animation-delay: #{$i * 0.15}s;
} }
Due to the delays, our circle now animates in turn, creating the illusion of the circles chasing each other. The only problem with this is that when the page first loads, the circles are static, then they start to move, one at a time. We can achieve the same offset effect, but stop the unwanted pause in our animation by simply setting the delays to a negative value.
animation-delay: -#{$i * 0.15}s;