围绕中心旋转嵌套元素

Revolving nested element around a center

我在关注 codecademy 的网站后制作了这个太阳系动画:http://www.codecademy.com/courses/web-beginner-en-ymqg0/0/1

我可以让地球和月球自转,但经过多次尝试,月球和天体的轴心还是不太对。目前,这是我的代码:

body {
  background: black;
}
#earth,
#moon,
#asteroid,
#sun {
  position: absolute;
  border-radius: 50%;
}
#earth {
  height: 40px;
  width: 40px;
  background: deepskyblue;
  box-shadow: 0 0 10px blue;
  top: 50%;
  left: 50%;
  margin: -200px 0 0 -20px;
  -webkit-animation: Erevolve 12s linear infinite;
  animation: Erevolve 12s linear infinite;
  transform-origin: 20px 200px;
  z-index: 100;
}
#moon {
  top: -30px;
  left: 10px;
  height: 10px;
  width: 10px;
  background: white;
  -webkit-animation: Mrevolve 2s linear infinite;
  animation: Mrevolve 2s linear infinite;
  transform-origin: 5px 30px;
}
#asteroid {
  top: -10px;
  left: 0px;
  height: 6px;
  width: 6px;
  background: #aaa;
  -webkit-animation: Arevolve 2s linear infinite;
  animation: Arevolve 2s linear infinite;
  transform-origin: 3px 10px;
}
#sun {
  height: 100px;
  width: 100px;
  background: gold;
  box-shadow: 0 0 50px gold;
  top: 50%;
  left: 50%;
  margin: -50px 0 0 -50px;
}
@-webkit-keyframes Erevolve {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@keyframes Erevolve {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@-webkit-keyframes Mrevolve {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@keyframes Mrevolve {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@-webkit-keyframes Arevolve {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
@keyframes Arevolve {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<body>
  <div id="earth">
    <div id="moon">
      <div id="asteroid">
      </div>
    </div>
  </div>

  <div id="sun"></div>
</body>

如您所见,月球和小行星正在撞击地球。我不知道为什么会这样,

我也为变换原点 属性 正确计算了边距和高度,但没有任何运气。任何帮助将不胜感激。

已修复。 http://jsbin.com/vemalo/1/

您的 top 利润率有点低。

此处更改https://www.diffchecker.com/3mdga45r

#moon {
  top: -40px;
  left: 10px;
  height: 10px;
  width: 10px;
  background: white;
  -webkit-animation: Mrevolve 2s linear infinite;
  animation: Mrevolve 2s linear infinite;

}
#asteroid {
  top: -20px;
  left: 0px;
  height: 6px;
  width: 6px;
  background: #aaa;
  -webkit-animation: Arevolve 2s linear infinite;
  animation: Arevolve 2s linear infinite;
  transform-origin: 5px;
  overflow:none;
}

http://jsbin.com/luwenocola/2/