How to use pure CSS to achieve the illusion of a chessboard animation (source code attached)

不言
Release: 2018-10-17 14:17:01
forward
3007 people have browsed it

The content of this article is about how to use pure CSS to realize the illusion animation of the chessboard (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 the illusion of a chessboard animation (source code attached)

Source code download

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

Code Interpretation

Define dom, the container contains 10 sub-elements, each sub-element represents a row:

<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;
}
Copy after login

Define the container size in vmin units, and arrange the child elements vertically:

.container {
    width: 100vmin;
    height: 100vmin;
    display: flex;
    flex-direction: column;
}
Copy after login

Set the background pattern of the child elements to spaced black and white blocks with a thin line at the top:

.container span {
    width: inherit;
    height: 10vmin;
    background: 
        linear-gradient(
            gray, gray 0.5vmin,
            transparent 0.5vmin, transparent
        ),
        repeating-linear-gradient(
            to right,
            black, black 10vmin,
            transparent 10vmin, transparent 20vmin
        )
}
Copy after login

Add a thin line at the bottom of the container:

.container {
    border-bottom: 0.5vmin solid gray;
}
Copy after login

Add an animation effect to move the background of the odd-numbered rows to the right by half a color block. After moving, it will look like the odd-numbered rows are wider on the right. Narrow on the left, wide on the left and narrow on the right of even-numbered rows, this is an illusion:

.container span:nth-child(odd) {
    animation: move 5s linear infinite;
}

@keyframes move {
    0%, 55%, 100% {
        background-position: 0 0;
    }

    5%, 50% {
        background-position: 5vmin 0;
    }
}
Copy after login

Let the background of the even-numbered rows also move, creating the illusion of the opposite direction:

.container span:nth-child(even) {
    animation: move 5s linear infinite reverse;
}
Copy after login

Done!


The above is the detailed content of How to use pure CSS to achieve the illusion of a chessboard animation (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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!