1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>飞舞的小球</title>
  6. </head>
  7. <style>
  8. *{
  9. margin:0;
  10. padding: 0;
  11. }
  12. html,body{
  13. width: 100%;
  14. height: 100%;
  15. position: relative;
  16. }
  17. .desktop{
  18. width: 1200px;
  19. height: 700px;
  20. position: absolute;
  21. top: calc(50% - 350px);
  22. left: calc(50% - 600px);
  23. /*background-color: wheat;*/
  24. display: flex;
  25. justify-content: space-between;
  26. flex-direction: column;
  27. align-items: center;
  28. }
  29. button{
  30. margin: 0 auto;
  31. background-color: white;
  32. width: 80px;
  33. height: 50px;
  34. border-radius: 5px;
  35. box-shadow: 0 5px 5px #dbdbdb;
  36. }
  37. button:hover{
  38. background-color: darkred;
  39. color: white;
  40. }
  41. .game_desktop{
  42. width: 95%;
  43. height: 95%;
  44. border-radius: 18px;
  45. background-color: wheat;
  46. box-shadow: 0 30px 30px #666666 ;
  47. margin: 10px 0 10px 0;
  48. position: relative;
  49. }
  50. .ball{
  51. position: absolute;
  52. border-radius: 50%;
  53. }
  54. </style>
  55. <body>
  56. <!--
  57. clientWidth 水平方向 width + 左右padding
  58. clientHeight 垂直方向 height + 上下padding
  59. offsetWidth 水平方向 width + 左右padding + 左右border-width
  60. offsetHeight 垂直方向 height + 上下padding + 上下border-width
  61. scrollWidth 元素内容真实的宽度,内容不超出盒子高度时为盒子的clientWidth
  62. scrollHeight 元素内容真实的高度,内容不超出盒子高度时为盒子的clientHeight
  63. offsetTop 获取当前元素到 定位父节点 的top方向的距离
  64. offsetLeft 获取当前元素到 定位父节点 的left方向的距离
  65. -->
  66. <div id="desktop" class="desktop">
  67. <button onclick="begin()">点击开始</button>
  68. <div class="game_desktop" id="game_desktop">
  69. </div>
  70. </div>
  71. </body>
  72. <script>
  73. var desktop= document.getElementById("desktop");
  74. var game_desktop= document.getElementById("game_desktop");
  75. var game_clintWith = game_desktop.clientWidth - 10 ;
  76. var game_clintHeight = game_desktop.clientHeight - 10;
  77. //设置垂直位置
  78. //设置垂直移动速度
  79. var i=0;
  80. //设置执行速度
  81. var z =100;
  82. function begin() {
  83. createBall();
  84. var ballid= "ball"+i;
  85. var ball = document.getElementById(ballid);
  86. var radius = parseInt(ball.style.width.substr(0,ball.style.width.indexOf('px')));
  87. //设置水平/垂直位置 保持和生成泡泡的位置一致
  88. var x = radius + game_clintWith * 0.5;
  89. var y = radius + game_clintHeight * 0.5;
  90. //设置随机方向
  91. var lr = Math.random() < 0.5 ? -1*Math.random() : Math.random() ;
  92. //设置移动速度
  93. var sy=lr * Math.ceil(10*Math.random());
  94. var sx =lr * Math.ceil(10*Math.random());
  95. setInterval(function () {
  96. x += sx;
  97. y += sy;
  98. ball.style.left= x + 'px';
  99. ball.style.top = y + 'px';
  100. var balloffsetTop=ball.offsetTop;
  101. var balloffsetleft=ball.offsetLeft;
  102. if(balloffsetTop >= game_clintHeight- radius || balloffsetTop <=0 ){
  103. sy = -sy;
  104. }
  105. if( balloffsetleft >= game_clintWith- radius || balloffsetleft <=0){
  106. sx = -sx;
  107. }
  108. if(ball.style.marginLeft === '0px'){
  109. sx = -sx;
  110. }
  111. },z);
  112. }
  113. function createBall() {
  114. var ids= 'ball' + ++i ;
  115. var newBall = document.createElement("div");
  116. var clolor=['0','red','green','blue',"#666666","#555222","#252525","pink","yellow","#909090","#121212"];
  117. var ballColor=clolor[Math.ceil(10*Math.random())];
  118. var ballWh=Math.ceil(5*Math.random())*Math.ceil(15*Math.random());
  119. if(ballWh<10){
  120. ballWh *= 10;
  121. }
  122. newBall.className="ball";
  123. newBall.style.backgroundColor=ballColor;
  124. newBall.style.width= ballWh + 'px';
  125. newBall.style.height= ballWh + 'px';
  126. newBall.style.top= ballWh + game_clintHeight * 0.5 + 'px';
  127. newBall.style.left= ballWh + game_clintWith * 0.5 + 'px';
  128. newBall.id=ids;
  129. game_desktop.append(newBall)
  130. }
  131. </script>
  132. </html>

更多相关文章

  1. [业界方案] Yarn的业界解决方案和未来方向
  2. HTML5学习的应用方向是什么
  3. 2020年Service Mesh:三大发展方向
  4. Linux运维方向文章汇总
  5. 机器学习方向企业面试题(三)
  6. css实现垂直居中的方式
  7. 一线大厂在机器学习方向的面试题(一)
  8. 机器学习方向企业面试题(一)
  9. linux运维的发展方向

随机推荐

  1. net. unknownhostexception:无法解析主机
  2. 【Android 开发教程】通过编码实现发送短
  3. Android功能模块化之网络连接状态判断
  4. Android Studio在连接手机时候,出现"Inst
  5. 跟核心虚拟机Dalvik说再见 Android Runti
  6. 实现微信布局的四种方式(一)
  7. Android防止活动被回收而丢失数据
  8. [置顶] Android获取存储卡路径的
  9. 关于Android Studio 3.1.3
  10. Android公钥私钥及代码详细解读