效果图
原理
行星沿着自身轨道旋转。
- 设置盒子,宽高均为800px,采用相对定位。
- 太阳绝对定位到盒子中间。
- 地球轨道盒子,宽高250px为圆形,背景色透明,加一个灰色边框,就是地球轨道,
- 绝对定位,left=400px-250px/2=275px,top=400px-250px/2=275px。
- 地球设置为圆形宽高18px,绝对定位,要想地球在轨道上,
- left值就等于轨道的left值减去半径在加上边框半径的一半。275+0.5-9=266.5px
- top值等于400px-9px=391px。
- 旋转中心为transform-origin: 134px 9px;X轴为400-left值,y轴为400-半径。
- 动画设置为逆时针旋转360deg,动画执行时间为依次递增。从内圈到外圈。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.solarsys {
width: 800px;
height: 800px;
position: relative;
margin: 0 auto;
background-color: #000;
transform: scale(1);
}
/*太阳*/
.sun {
position: absolute;
left: 357px;
top: 357px;
height: 90px;
width: 90px;
background-color: rgb(248, 107, 35);
border-radius: 50%;
box-shadow: 5px 5px 10px rgb(248, 107, 35), -5px -5px 10px rgb(248, 107, 35), 5px -5px 10px rgb(248, 107, 35), -5px 5px 10px rgb(248, 107, 35);
}
/*水星轨道*/
.mercuryOrbit {
left: 342.5px;
top: 342.5px;
height: 115px;
width: 115px;
background-color: transparent;
border-radius: 50%;
border-style: dashed;
border-color: gray;
position: absolute;
border-width: 1px;
}
/* 水星 */
.mercury {
position: absolute;
left: 337.5px;
top: 395px;
height: 10px;
width: 10px;
background-color: rgb(166, 138, 56);
border-radius: 50%;
transform-origin: 62.5px 5px;
animation: rotate 1.5s infinite linear;
}
/* 金星轨道 */
.venusOrbit {
left: 320px;
top: 320px;
height: 160px;
width: 160px;
background-color: transparent;
border-radius: 50%;
border-style: dashed;
border-color: gray;
position: absolute;
}
/* 金星 */
.venus {
left: 309px;
top: 389px;
height: 22px;
width: 22px;
background-color: rgb(246, 157, 97);
border-radius: 50%;
position: absolute;
transform-origin: 91px 11px;
animation: rotate 3.84s infinite linear;
}
/* 地球轨道 */
.earthOrbit {
position: absolute;
left: 275px;
top: 275px;
width: 250px;
height: 250px;
background-color: transparent;
border: 1px solid gray;
border-radius: 50%;
}
/* 地球 */
.earth {
left: 266.5px;
top: 391px;
height: 18px;
width: 18px;
background-color: rgb(115, 114, 174);
border-radius: 50%;
position: absolute;
transform-origin: 133.5px 9px;
animation: rotate 6.25s infinite linear;
}
/* 火星轨道 */
.marsOrbit {
position: absolute;
left: 230px;
top: 230px;
height: 340px;
width: 340px;
background-color: transparent;
border-radius: 50%;
border-style: dashed;
border-color: gray;
border-width: 1px;
}
/* 火星 */
.mars {
position: absolute;
left: 222.5px;
top: 397.5px;
height: 15px;
width: 15px;
background-color: rgb(140, 119, 63);
border-radius: 50%;
position: absolute;
transform-origin: 177.5px 7.5px;
animation: rotate 11.75s infinite linear;
}
/* 木星轨道 */
.jupiterOrbit {
position: absolute;
left: 155px;
top: 155px;
height: 490px;
width: 490px;
background-color: transparent;
border-radius: 50%;
border-style: dashed;
border-color: gray;
border-width: 1px;
}
/* 木星 */
.jupiter {
position: absolute;
left: 134px;
top: 379px;
height: 42px;
width: 42px;
background-color: rgb(156, 164, 143);
border-radius: 50%;
position: absolute;
transform-origin: 266px 21px;
animation: rotate 74.04s infinite linear;
}
/* 土星轨道 */
.saturnOrbit {
position: absolute;
left: 105px;
top: 105px;
width: 590px;
height: 590px;
border-radius: 50%;
background-color: transparent;
border: 1px dashed gray;
}
/* 土星 */
.saturn {
position: absolute;
height: 26px;
width: 26px;
left: 92px;
top: 387px;
border-radius: 50%;
background-color: rgb(215, 171, 68);
transform-origin: 308px 13px;
animation: rotate 183.92s infinite linear;
}
/* 天王星天王星轨道 */
.uranusOrbit {
position: absolute;
top: 55px;
left: 55px;
width: 690px;
height: 690px;
border-radius: 50%;
background-color: transparent;
border: 1px dashed gray;
}
/* 天王星 */
.uranus {
position: absolute;
width: 27px;
height: 27px;
left: 41.5px;
top: 386.5px;
background-color: rgb(164, 192, 206);
border-radius: 50%;
transform-origin: 358.5px 13.5px;
animation: rotate 524.46s infinite linear;
}
/*海王星*/
.neptune {
left: 10px;
top: 390px;
height: 20px;
width: 20px;
background-color: rgb(133, 136, 180);
border-radius: 50%;
position: absolute;
transform-origin: 390px 10px;
animation: rotate 1028.76s infinite linear;
}
/*海王星轨道*/
.neptuneOrbit {
position: absolute;
left: 20px;
top: 20px;
height: 760px;
width: 760px;
background-color: transparent;
border-radius: 50%;
border-style: dashed;
border-color: gray;
border-width: 1px;
}
@keyframes rotate {
100% {
transform: rotate(-360deg);
}
}
</style>
</head>
<body>
<div class="solarsys">
<!--太阳-->
<div class='sun'></div>
<!--水星轨道-->
<div class='mercuryOrbit'></div>
<!--水星-->
<div class='mercury'></div>
<!--金星轨道-->
<div class='venusOrbit'></div>
<!--金星-->
<div class='venus'></div>
<!--地球轨道-->
<div class='earthOrbit'></div>
<!--地球-->
<div class='earth'></div>
<!--火星轨道-->
<div class='marsOrbit'></div>
<!--火星-->
<div class='mars'></div>
<!--木星轨道-->
<div class='jupiterOrbit'></div>
<!--木星-->
<div class='jupiter'></div>
<!--土星轨道-->
<div class='saturnOrbit'></div>
<!--土星-->
<div class='saturn'></div>
<!--天王星轨道-->
<div class='uranusOrbit'></div>
<!--天王星-->
<div class='uranus'></div>
<!--海王星轨道-->
<div class='neptuneOrbit'></div>
<!--海王星-->
<div class='neptune'></div>
</div>
</body>
</html>