javascript – 太阳系轨道html中心

前端之家收集整理的这篇文章主要介绍了javascript – 太阳系轨道html中心前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的太阳系系统模型中,当您点击“切换轨道”时,它会显示所有熟悉的行星地球的轨道,但是您注意到,该环不在该星球的中间,仅在其外部,如何使其成为所以会在中间?
  1. function myFunction() {
  2. for (var i = 0; i < 500; i++) {
  3. var x = Math.random() * screen.width;
  4. var y = Math.random() * screen.height;
  5. var star = document.createElement('div');
  6. star.className = 'star';
  7. star.style.left = x + 'px';
  8. star.style.top = y + 'px';
  9. document.body.appendChild(star);
  10. }
  11. }
  1. html {
  2. background-color: #000;
  3. overflow-x: hidden;
  4. overflow-y: hidden;
  5. }
  6. .star {
  7. position: absolute;
  8. width: 1px;
  9. height: 1px;
  10. background: white;
  11. z-index: -1;
  12. }
  13. .sun {
  14. position: absolute;
  15. height: 100px;
  16. width: 100px;
  17. top: 50%;
  18. left: 50%;
  19. margin-left: -50px;
  20. margin-top: -50px;
  21. border-radius: 50%;
  22. /*Box-shadow: rgb(204,153,0) 0px 0px 50px 0px;*/
  23. }
  24. #button-change {
  25. position: absolute;
  26. top: 2px;
  27. left: 2px;
  28. }
  29. .earth {
  30. position: absolute;
  31. height: 25px;
  32. width: 25px;
  33. border-radius: 50%;
  34. Box-shadow: green 0 0 25px;
  35. }
  36. .earth-orbit {
  37. position: absolute;
  38. height: 200px;
  39. width: 200px;
  40. top: 50%;
  41. left: 50%;
  42. margin-left: -100px;
  43. margin-top: -100px;
  44. -webkit-animation: spin-right 15s linear infinite;
  45. }
  46. .earth-lines {
  47. border-width: 1px;
  48. border-style: solid;
  49. border-color: white;
  50. border-radius: 50%;
  51. position: absolute;
  52. }
  53. .moon {
  54. height: 10px;
  55. width: 10px;
  56. }
  57. .moon-orbit {
  58. top: 50%;
  59. left: 50%;
  60. height: 50px;
  61. width: 50px;
  62. margin-left: -12.5px;
  63. margin-bottom: -37px;
  64. border: 1px solid rgba(255,0.1);
  65. border-radius: 50%;
  66. -webkit-animation: spin-right 4s linear infinite;
  67. }
  68. @-webkit-keyframes spin-right {
  69. 100% {
  70. -webkit-transform: rotate(360deg);
  71. }
  72. }
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <title>Vanishing Act</title>
  6. <link rel='stylesheet' type='text/css' href='stylesheet.css' />
  7. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  8. <script type='text/javascript' src='script.js'></script>
  9. <script>
  10. $(document).ready(function() {
  11. $("button").click(function() {
  12. $('.earth-orbit').toggleClass('earth-lines');
  13. });
  14. });
  15. </script>
  16. </head>
  17.  
  18. <body onload="myFunction()">
  19. <img class="sun" src="5.png">
  20. </div>
  21. <div class="earth-orbit">
  22. <div class='moon-orbit'>
  23. <img class="moon" src="http://space-facts.com/wp-content/uploads/moon-transparent.png" />
  24. </div>
  25.  
  26. <img class="earth" src="http://www.polyvore.com/cgi/img-thing?.out=jpg&size=l&tid=74422923" />
  27. </div>
  28. <button id="button-change">Toggle Orbits</button>
  29. </body>
  30.  
  31. </html>

解决方法

您将.earth线静态地放在.earth-orbit上,所以调整.earth和.moon的边距是一个合乎逻辑的解决方案.

另一方面,让我们开始思考.如果我们把.earth线作为单独的div呢?喜欢这个:

  1. <div class="earth-lines">
  2. </div>
  3.  
  4. <div class="earth-orbit ">
  5. <div class='moon-orbit'>
  6. <img class="moon" src="http://space-facts.com/wp-content/uploads/moon-transparent.png" />
  7. </div>
  8. </div>

而.earth-line的CSS将如下所示:

  1. .earth-lines {
  2. display: none;
  3. border-width: 1px;
  4. border-style: solid;
  5. border-color: white;
  6. border-radius: 50%;
  7. position: absolute;
  8. height: 226px;
  9. width: 226px;
  10. top: 50%;
  11. left: 50%;
  12. margin-left: -113px;
  13. margin-top: -113px;
  14. }

最后一件事是调整JavaScript:

  1. <script>
  2. $(document).ready(function() {
  3. $("button").click(function() {
  4. $('.earth-lines').toggle();
  5. });
  6. });
  7. </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”>

猜你在找的JavaScript相关文章