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)

不言
不言forward
2018-09-28 17:12:512559browse

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>

Centered display:

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

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;
}

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

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

Set the style of the sub-element:

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

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;
    }
}

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; }

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);
    }
}

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!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete