Home > Web Front-end > CSS Tutorial > How to use pure CSS to implement a pulsating loader (source code attached)

How to use pure CSS to implement a pulsating loader (source code attached)

不言
Release: 2018-09-28 17:12:50
forward
2550 people have browsed it

The content of this article is about how to use pure CSS to implement a pulsating loader (source code attached). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

Effect preview

How to use pure CSS to implement a pulsating loader (source code attached)

Source code download

https://github.com/comehope/front-end-daily -challenges

Code interpretation

Define dom, the container contains 10 sub-elements:

<div>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <span></span>
</div>
Copy after login

Centered display:

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(#eee 70%, pink);
}
Copy after login

Set the style of the container, which is pink A circle with background and stroke:

.loader {
    width: 6em;
    height: 6em;
    padding: 3em;
    font-size: 10px;
    background-color: pink;
    border-radius: 50%;
    border: 0.8em solid hotpink;
}
Copy after login

Set the layout mode of the sub-element to horizontal tile:

.loader {
    display: flex;
    align-items: center;
    justify-content: space-between;
}
Copy after login

Set the style of the sub-element:

.loader > span {
    width: 0.5em;
    height: 50%;
    background-color: deeppink;
}
Copy after login

Add the sub-element’s Animation effect:

.loader > span {
    transform: scaleY(0.05) translateX(-0.5em);
    animation: span-animate 1.5s infinite ease-in-out;
}

@keyframes span-animate {
    0%, 100% {
        transform: scaleY(0.05) translateX(-0.5em);
    }
    15% {
        transform: scaleY(1.2) translateX(1em);
    }
    90%, 100% {
        background-color: hotpink;
    }
}
Copy after login

Set the subscript of the sub-element and let the sub-elements play animation in sequence:

.loader > span {
    animation-delay: calc(var(--n) * 0.05s);
}

.loader > span:nth-child(1) { --n: 1; }
.loader > span:nth-child(2) { --n: 2; }
.loader > span:nth-child(3) { --n: 3; }
.loader > span:nth-child(4) { --n: 4; }
.loader > span:nth-child(5) { --n: 5; }
.loader > span:nth-child(6) { --n: 6; }
.loader > span:nth-child(7) { --n: 7; }
.loader > span:nth-child(8) { --n: 8; }
.loader > span:nth-child(9) { --n: 9; }
.loader > span:nth-child(10) { --n: 10; }
Copy after login

Add container animation to enhance the pulsating effect:

.loader {
    animation: loader-animate 1.5s infinite ease-in-out;
}

@keyframes loader-animate {
    45%, 55% {
        transform: scale(1.05);
    }
}
Copy after login

You’re done!

The above is the detailed content of How to use pure CSS to implement a pulsating loader (source code attached). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template