How to use CSS and process color mode to achieve loader animations (with code)

Source: Internet
Author: User
This article brings you the content is about how to use CSS and color-blending mode to achieve loader animation effect (with code), there is a certain reference value, the need for a friend can refer to, I hope you have some help.

Effect Preview

Source code Download

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

Code interpretation

To define the DOM, there are only 1 elements:

<div class= "Loader" ></div>

Center display:

body {    margin:0;    HEIGHT:100VH;    Display:flex;    Align-items:center;    Justify-content:center;    Background-color:lightyellow;}

Define Container Dimensions:

. loader {    width:30em;    Height:3em;    font-size:10px;}

Use pseudo-elements to draw 2 rounded rectangles, each occupying half the width of the container, placed at the left and right sides of the container, shaded separately:

. loader {    position:relative;}. Loader::before,.loader::after {    content: ';    Position:absolute;    width:50%;    Height:inherit;    Border-radius:1em;}. Loader::before {    left:0;    Background-color:dodgerblue;}. loader::after {    right:0;    Background-color:hotpink;}

Add ' loading ' text to rounded rectangles:

. loader::before,.loader::after {    content: ' Loading ';    font-size:2.5em;    Color:white;    Text-align:center;    Line-height:1em;}

Add animation effects:

. loader::before,.loader::after {    animation:5s move ease-in-out Infinite;} @keyframes Move {    50% {        Transform:translatex (100%);    }}

Set the motion direction variables for the two rounded rectangles to move them relative to each other:

. loader::before {    --direction:1;}. Loader::after {    --direction:-1;} @keyframes Move {    50% {        Transform:translatex (Calc (100% * VAR (--direction));}    }

Finally, set the process color mode so that the part that intersects the two rectangles does not overwrite but causes the color to overlap:

. loader::before,.loader::after {    mix-blend-mode:multiply;}

Done!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.