How to use pure CSS to animate the sun and the Earth and the moon's motion model

Source: Internet
Author: User
This article brings you to the content is about how to use pure CSS to achieve the sun and the Earth and the moon's running model animation, there is a certain reference value, the need for friends can refer to, I hope to help you.

Effect Preview

Source code Download

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

Code interpretation

To define the DOM, the container contains 3 elements:

<div class= "Container" >    <div class= "Sun" ></div>    <div class= "Earth" >        <div Class= "Moon" ></div>    </div></div>

Center display:

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

Define Container Dimensions:

. container {    font-size:10px;    Width:40em;    Height:40em;    Position:relative;}

Draw the Sun:

. Sun {    position:absolute;    top:15em;    left:15em;    Width:10em;    Height:10em;    Background-color:yellow;    border-radius:50%;    box-shadow:0 0 3em White;}

Draw the path of the Earth and the moon:

. Earth,.moon  {    position:absolute;    Border-style:solid;    Border-color:white transparent transparent transparent;    Border-width:0.1em 0.1em 0 0;    border-radius:50%;}. Earth {    top:5em;    left:5em;    Width:30em;    Height:30em;}. Moon {    top:0;    right:0;    Width:8em;    Height:8em;}

Plot the Earth and the moon with pseudo-elements:

. earth::before,.moon::before {    position:absolute;    border-radius:50%;   Content: ';}. Earth::before {    top:2.8em;    right:2.5em;    Height:3em;    Width:3em;    Background-color:aqua;}. Moon::before {    top:0.8em;    Right:0.2em;     Width:1.2em;    Height:1.2em;    Background-color:silver;}

To set the animation effect of running:

/* Rotation period 365.2422 days */.earth {    animation:orbit 36.5s linear infinite;   } /* Rotation period 27.322 days */.moon {    animation:orbit 2.7s linear Infinite;} @keyframes Orbit {to    {        transform:rotate (360deg);    }}

Finally, hide the parts that might appear outside the container:

body {    Overflow:hidden;}

Done!

Related Article

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.