Front-end daily combat: # 91 video demonstrates how to use the train loader pure CSS creation of a traveling

image description

Results preview

Press the right "click the Preview" button to preview the current page, click on the link to full-screen preview.

https://codepen.io/comehope/pen/RBLWzJ

Interactive video

This video is interactive, you can pause the code video, editing video.

Please use chrome, safari, edge open view.

https://scrimba.com/p/pEgDAM/cawN3f9

Source code download

Daily front-end combat series full source code can be downloaded from github:

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

Code Reading

Defined dom, the container comprising two elements, trainrepresentative of the train, trackon behalf of the tracks, which includes three <span>representatives of three sleepers.

<div class="loader">
    <div class="train"></div>
    <div class="track">
        <span></span>
        <span></span>
        <span></span>
    </div>
</div>

Centered:

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

Is defined container sizes:

.loader {
    width: 8em;
    height: 10em;
    font-size: 20px;
}

First draw a train.
Draw the outline of the train:

.train {
    width: 6em;
    height: 6em;
    color: #444;
    background: #bbb4ab;
    border-radius: 1em;
    position: relative;
    left: 1em;
}

Draw window with :: before pseudo-element:

.train::before {
    content: '';
    position: absolute;
    width: 80%;
    height: 2.3em;
    background-color: currentColor;
    border-radius: 0.4em;
    top: 1.2em;
    left: 10%;
}

:: after pseudo-elements and then draw the lights on the window:

.train::after {
    content: '';
    position: absolute;
    width: 25%;
    height: 0.4em;
    background-color: currentColor;
    border-radius: 0.3em;
    top: 0.4em;
    left: calc((100% - 25%) / 2);
}

Drawn by the radial gradient lamp:

.train {
    background: 
        radial-gradient(circle at 20% 80%, currentColor 0.6em, transparent 0.6em),
        radial-gradient(circle at 80% 80%, currentColor 0.6em, transparent 0.6em),
        #bbb;
}

Next picture rails and sleepers.
Define the width of the tracks, slightly wider than the train:

.track {
    width: 8em;
}

The tracks drawn by pseudo-element:

.track {
    position: relative;
}

.track::before,
.track::after {
    content: '';
    position: absolute;
    width: 0.3em;
    height: 4em;
    background-color: #bbb;
    border-radius: 0.4em;
}

The rails are placed on both sides, and is formed near the far smaller visual effects:

.track::before,
.track::after {
    transform-origin: bottom;
}

.track::before {
    left: 0;
    transform: skewX(-27deg);
}

.track::after {
    right: 0;
    transform: skewX(27deg);
}

Draw sleepers, which is the closest distance between the viewer effect, the current three sleepers overlap together:

.track span {
    width: inherit;
    height: 0.3em;
    background-color: #bbb;
    position: absolute;
    top: 4em;
}

Animate tracks:

.track span {
    animation: track-animate 1s linear infinite;
}

@keyframes track-animate {
    0% {
        transform: translateY(-0.5em) scaleX(0.9);
        filter: opacity(0);
    }

    10%, 90% {
        filter: opacity(1);
    }

    100% {
        transform: translateY(-4em) scaleX(0.5);
        filter: opacity(0);
    }
}

For another two sleepers animate delay, so that the tracks look like forever to go finish the way:

.track span:nth-child(2) {
    animation-delay: -0.33s;
}

.track span:nth-child(3) {
    animation-delay: -0.66s;
}

Finally, to increase train animation looks like shaking slightly in motion:

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

@keyframes train-animate {
    0%, 100% {
        transform: rotate(0deg);
    }

    25%, 75% {
        transform: rotate(0.5deg);
    }

    50% {
        transform: rotate(-0.5deg);
    }
}

We're done!

Guess you like

Origin www.cnblogs.com/jlfw/p/11876285.html