Let’s make an animated CSS loader with scoped customized properties and animation-timing-function
Jump over to CodePen and create a brand new pen.
Create the markup for our loader. Notice the usage of inline customized properties:
div class="loader" model="--count: 10">
span model="--index: 0"></span>
span model="--index: 1"></span>
span model="--index: 2"></span>
span model="--index: 3"></span>
span model="--index: 4"></span>
span model="--index: 5"></span>
span model="--index: 6"></span>
span model="--index: 7"></span>
span model="--index: 8"></span>
span model="--index: 9"></span>
</div>
You may as well use a generator (Pug) to configure the variety of traces:
- const COUNT = 10
.loader(model=`--count: ${COUNT}`)
- let i = 0
whereas i COUNT
span(model=`--index: ${i}`)
- i++
Give our loader some kinds:
loader {
--size: 10vmin;peak: var(--size);
place: relative;
width: var(--size);
}
Place our traces utilizing absolute positioning and a mix of calc
with rework
:
.loader span {
background: gray;
peak: 25%;
left: 50%;
place: absolute;
high: 50%;
rework: translate(-50%, -50%)
rotate(calc(((360 / var(--count)) * var(--index)) * 1deg))
translate(0, -125%);
width: 10%;
}
Apply an opacity primarily based on the --index
:
.loader span {
opacity: calc(var(--index) / var(--count));
}
Get issues spinning!
.loader {
animation: spin 0.75s infinite steps(var(--count));
}@keyframes spin {
to {
rework: rotate(360deg);
}
}
Notice the usage of steps(var(--count))
to get the precise impact ✨
Executed! 🎉
Want this in tweet kind? 🐦
Keep Superior! ʕ •ᴥ•ʔ