在我的太阳系模型上,当您单击“切换轨道”时,它会显示所有熟悉的行星地球的轨道,但您会注意到该环并不位于行星中心,而仅位于行星中心,我将如何制造它这样会在中间吗?

function myFunction() {
  for (var i = 0; i < 500; i++) {
    var x = Math.random() * screen.width;
    var y = Math.random() * screen.height;
    var star = document.createElement('div');
    star.className = 'star';
    star.style.left = x + 'px';
    star.style.top = y + 'px';
    document.body.appendChild(star);
  }
}
html {
  background-color: #000;
  overflow-x: hidden;
  overflow-y: hidden;
}
.star {
  position: absolute;
  width: 1px;
  height: 1px;
  background: white;
  z-index: -1;
}
.sun {
  position: absolute;
  height: 100px;
  width: 100px;
  top: 50%;
  left: 50%;
  margin-left: -50px;
  margin-top: -50px;
  border-radius: 50%;
  /*box-shadow: rgb(204, 153, 0) 0px 0px 50px 0px;*/
}
#button-change {
  position: absolute;
  top: 2px;
  left: 2px;
}
.earth {
  position: absolute;
  height: 25px;
  width: 25px;
  border-radius: 50%;
  box-shadow: green 0 0 25px;
}
.earth-orbit {
  position: absolute;
  height: 200px;
  width: 200px;
  top: 50%;
  left: 50%;
  margin-left: -100px;
  margin-top: -100px;
  -webkit-animation: spin-right 15s linear infinite;
}
.earth-lines {
  border-width: 1px;
  border-style: solid;
  border-color: white;
  border-radius: 50%;
  position: absolute;
}
.moon {
  height: 10px;
  width: 10px;
}
.moon-orbit {
  top: 50%;
  left: 50%;
  height: 50px;
  width: 50px;
  margin-left: -12.5px;
  margin-bottom: -37px;
  border: 1px solid rgba(255, 0, 0, 0.1);
  border-radius: 50%;
  -webkit-animation: spin-right 4s linear infinite;
}
@-webkit-keyframes spin-right {
  100% {
    -webkit-transform: rotate(360deg);
  }
}
<!DOCTYPE html>
<html>

<head>
  <title>Vanishing Act</title>
  <link rel='stylesheet' type='text/css' href='stylesheet.css' />
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script type='text/javascript' src='script.js'></script>
  <script>
    $(document).ready(function() {
      $("button").click(function() {
        $('.earth-orbit').toggleClass('earth-lines');
      });
    });
  </script>
</head>

<body onload="myFunction()">
  <img class="sun" src="5.png">
  </div>
  <div class="earth-orbit">
    <div class='moon-orbit'>
      <img class="moon" src="http://space-facts.com/wp-content/uploads/moon-transparent.png" />
    </div>

    <img class="earth" src="http://www.polyvore.com/cgi/img-thing?.out=jpg&size=l&tid=74422923" />
  </div>
  <button id="button-change">Toggle Orbits</button>
</body>

</html>

最佳答案

您将.earth-lines静态地放置在.earth-orbit上,因此调整.earth.moon的边距是一个合理的解决方案。

另一方面,让我们思考一下。如果我们将.earth-lines作为单独的div怎么办?像这样:

<div class="earth-lines">
</div>

<div class="earth-orbit ">
    <div class='moon-orbit'>
      <img class="moon" src="http://space-facts.com/wp-content/uploads/moon-transparent.png" />
    </div>
</div>

.earth-lines的CSS如下所示:
.earth-lines {
    display: none;
    border-width: 1px;
    border-style: solid;
    border-color: white;
    border-radius: 50%;
    position: absolute;
    height: 226px;
    width: 226px;
    top: 50%;
    left: 50%;
    margin-left: -113px;
    margin-top: -113px;
}

最后一件事是调整JavaScript:
<script>
    $(document).ready(function() {
        $("button").click(function() {
            $('.earth-lines').toggle();
        });
    });
</script>

在这种情况下,它会被切换,并且看起来就像您想要的那样。这是一个 fiddle :http://jsfiddle.net/x3ybjd0f/1/

P.S.很棒的想法和实现,我喜欢它;)

更新

如何修复太阳。

在您的代码中,您有<img class="sun" src="5.png">
根据您的评论,图片的链接是http://toms-storage.tk/5.png

所以正确的代码应该是<img class="sun" src="http://toms-storage.tk/5.png">

10-08 16:54