Home > Web Front-end > CSS Tutorial > How to use pure CSS to achieve a diamond-shaped loader effect (source code attached)

How to use pure CSS to achieve a diamond-shaped loader effect (source code attached)

不言
Release: 2018-09-18 17:42:23
Original
1961 people have browsed it

The content of this article is about how to use pure CSS to achieve the diamond-shaped loader effect (source code attached). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

Effect preview

How to use pure CSS to achieve a diamond-shaped loader effect (source code attached)

Source code download

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

Code Interpretation

Define dom, a container contains 9 sub-elements:

<div>
    <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-color: black;
}
Copy after login

Define container and sub-element size, It is a large square containing 9 small squares:

.loader {
    width: 10em;
    height: 10em;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-gap: 0.5em;
}
Copy after login

Adjust the pattern to a large rhombus containing 9 small rhombuses:

.loader {
    transform: rotate(45deg);
}
Copy after login

Taking the vertical small rhombus as the unit, it is a small rhombus Block coloring:

.loader span {
    background-color: var(--c);
}

.loader span:nth-child(7) {
    --c: tomato;
}

.loader span:nth-child(4),
.loader span:nth-child(8) {
    --c: gold;
}

.loader span:nth-child(1),
.loader span:nth-child(5),
.loader span:nth-child(9) {
    --c: limegreen;
}

.loader span:nth-child(2),
.loader span:nth-child(6) {
    --c: dodgerblue;
}

.loader span:nth-child(3) {
    --c: mediumpurple;
}
Copy after login

Define the animation effect:

.loader span {
    animation: blinking 2s linear infinite;
    animation-delay: var(--d);
    transform: scale(0);
}

@keyframes blinking {
    0%, 100% {
        transform: scale(0);
    }

    40%, 80% {
        transform: scale(1);
    }
}
Copy after login

Finally, set the time delay for the small diamond to enhance the dynamic:

.loader span:nth-child(7) {
    --d: 0s;
}

.loader span:nth-child(4),
.loader span:nth-child(8) {
    --d: 0.2s;
}

.loader span:nth-child(1),
.loader span:nth-child(5),
.loader span:nth-child(9) {
    --d: 0.4s;
}

.loader span:nth-child(2),
.loader span:nth-child(6) {
    --d: 0.6s;
}

.loader span:nth-child(3) {
    --d: 0.8s;
}
Copy after login

You’re done!

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

Related labels:
source:php.cn
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