Saturday, May 18, 2024
HomeWeb developmentSpeedy CSS Tip! Animated Loader

Speedy CSS Tip! Animated Loader


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">&LT/span>
span model="--index: 1">&LT/span>
span model="--index: 2">&LT/span>
span model="--index: 3">&LT/span>
span model="--index: 4">&LT/span>
span model="--index: 5">&LT/span>
span model="--index: 6">&LT/span>
span model="--index: 7">&LT/span>
span model="--index: 8">&LT/span>
span model="--index: 9">&LT/span>
&LT/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! ʕ •ᴥ•ʔ

Final up to date: Enhance article



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments