javascript – 太阳系轨道html中心

前端之家收集整理的这篇文章主要介绍了javascript – 太阳系轨道html中心前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的太阳系系统模型中,当您点击“切换轨道”时,它会显示所有熟悉的行星地球的轨道,但是您注意到,该环不在该星球的中间,仅在其外部,如何使其成为所以会在中间?
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.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线静态地放在.earth-orbit上,所以调整.earth和.moon的边距是一个合乎逻辑的解决方案.

另一方面,让我们开始思考.如果我们把.earth线作为单独的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-line的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>

在这种情况下,它将被切换,并将看起来只是你想要的方式.这是一个小提琴:http://jsfiddle.net/x3ybjd0f/1/

附:奇妙的想法和实现,我喜欢它;)

UPDATE

如何解决太阳

在你的代码中,你有< img class =“sun”src =“5.png”>

根据您的意见,图片链接http://toms-storage.tk/5.png

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

原文链接:https://www.f2er.com/js/152675.html

猜你在找的JavaScript相关文章