Speedy CSS Tip! Animated Loader

Hop over to CodePen and create a new pen.
Create the markup for our loader. Note the use of inline custom properties:
<div class=”loader” style=”–count: 10″> <span style=”–index: 0″></span> <span style=”–index: 1″></span> <span …


This content originally appeared on web.dev and was authored by Jhey Tompkins

Hop over to CodePen and create a new pen.

Create the markup for our loader. Note the use of inline custom properties:

<div class="loader" style="--count: 10">
<span style="--index: 0"></span>
<span style="--index: 1"></span>
<span style="--index: 2"></span>
<span style="--index: 3"></span>
<span style="--index: 4"></span>
<span style="--index: 5"></span>
<span style="--index: 6"></span>
<span style="--index: 7"></span>
<span style="--index: 8"></span>
<span style="--index: 9"></span>
</div>

You can also use a generator (Pug) to configure the number of lines:

- const COUNT = 10
.loader(style=`--count: ${COUNT}`)
- let i = 0
while i < COUNT
span(style=`--index: ${i}`)
- i++

Give our loader some styles:

loader {
--size: 10vmin;

height: var(--size);
position: relative;
width: var(--size);
}

Position our lines using absolute positioning and a combination of calc with transform:

.loader span {
background: grey;
height: 25%;
left: 50%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%)
rotate(calc(((360 / var(--count)) * var(--index)) * 1deg))
translate(0, -125%);
width: 10%;
}

Apply an opacity based on the --index:

.loader span {
opacity: calc(var(--index) / var(--count));
}

Get things spinning!

.loader {
animation: spin 0.75s infinite steps(var(--count));
}

@keyframes spin {
to {
transform: rotate(360deg);
}
}

Note the use of steps(var(--count)) to get the right effect ✨

Done! 🎉

Prefer this in tweet form? 🐦

Stay Awesome! ʕ •ᴥ•ʔ


This content originally appeared on web.dev and was authored by Jhey Tompkins


Print Share Comment Cite Upload Translate Updates
APA

Jhey Tompkins | Sciencx (2022-09-22T00:00:00+00:00) Speedy CSS Tip! Animated Loader. Retrieved from https://www.scien.cx/2022/09/22/speedy-css-tip-animated-loader/

MLA
" » Speedy CSS Tip! Animated Loader." Jhey Tompkins | Sciencx - Thursday September 22, 2022, https://www.scien.cx/2022/09/22/speedy-css-tip-animated-loader/
HARVARD
Jhey Tompkins | Sciencx Thursday September 22, 2022 » Speedy CSS Tip! Animated Loader., viewed ,<https://www.scien.cx/2022/09/22/speedy-css-tip-animated-loader/>
VANCOUVER
Jhey Tompkins | Sciencx - » Speedy CSS Tip! Animated Loader. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/22/speedy-css-tip-animated-loader/
CHICAGO
" » Speedy CSS Tip! Animated Loader." Jhey Tompkins | Sciencx - Accessed . https://www.scien.cx/2022/09/22/speedy-css-tip-animated-loader/
IEEE
" » Speedy CSS Tip! Animated Loader." Jhey Tompkins | Sciencx [Online]. Available: https://www.scien.cx/2022/09/22/speedy-css-tip-animated-loader/. [Accessed: ]
rf:citation
» Speedy CSS Tip! Animated Loader | Jhey Tompkins | Sciencx | https://www.scien.cx/2022/09/22/speedy-css-tip-animated-loader/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.