博客列表 >飞舞的泡泡球

飞舞的泡泡球

「flasky」
「flasky」原创
2021年11月26日 19:01:11521浏览

飞舞的泡泡球
效果图如下

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .container{
  8. width: 800px;
  9. height: 800px;
  10. background-color: #eee;
  11. margin: 60px auto;
  12. box-shadow: 5px 8px 2px #bdbdbd inset;
  13. border: 1px solid #666;
  14. position: relative;
  15. }
  16. .but{
  17. margin: 0px auto;
  18. width: 800px;
  19. }
  20. .ball{
  21. border-radius: 50%;
  22. /*background-color: rgb(157 110 233);*/
  23. position: absolute;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="container" id="cont">
  29. </div>
  30. <div class="but"><button id="btn" onclick="pball()">弹球</button></div>
  31. <script>
  32. var comobj=document.getElementById('cont');
  33. function pball(){
  34. var myWidth = Math.floor(Math.random()*30+10);
  35. var speY = Math.floor(Math.random()*10-6);
  36. var speX = Math.floor(Math.random()*10-6);
  37. var myball=document.createElement("div");
  38. var mycolorR = Math.floor(Math.random()*255);
  39. var mycolorB = Math.floor(Math.random()*255);
  40. var mycolorG = Math.floor(Math.random()*255);
  41. var contW = comobj.clientWidth;
  42. var contH = comobj.clientHeight;
  43. // console.log(mycolorR+','+mycolorG)
  44. myball.style.backgroundColor = `rgb(${mycolorR} ${mycolorB} ${mycolorG})`;
  45. myball.style.width = `${myWidth}px`;
  46. myball.style.height = `${myWidth}px`;
  47. // console.log(`rgb(${mycolorR} ${mycolorB} ${mycolorG})`);
  48. myball.className='ball';
  49. comobj.appendChild(myball);
  50. myball.style.top = `${contW/2}px`;
  51. myball.style.left =`${contH/2}px`;
  52. // console.log( `${comobj.clientWidth/2}px`)
  53. setInterval(function () {
  54. var oftop = myball.offsetTop;
  55. var ofleft= myball.offsetLeft;
  56. var myWid = myball.offsetWidth;
  57. var myhei = myball.offsetHeight;
  58. var tp= contH-myhei;
  59. var lp = contW-myWid;
  60. var opSy = 0;
  61. var lpSx = 0;
  62. opSy = oftop + speY;
  63. lpSx = ofleft + speX;
  64. if (opSy<=0)
  65. {
  66. myball.style.top='0px';
  67. speY = ~speY;
  68. } else if (opSy>=tp) {
  69. myball.style.top = `${tp}px`;
  70. speY = ~speY;
  71. }else {
  72. myball.style.top = `${opSy}px`;
  73. }
  74. if (lpSx<=0)
  75. {
  76. myball.style.left='0px';
  77. speX = ~speX;
  78. }else if (lpSx>=lp) {
  79. myball.style.left = `${lp}px`;
  80. speX = ~speX;
  81. }else {
  82. myball.style.left = `${lpSx}px`;
  83. }
  84. },50)
  85. }
  86. </script>
  87. </body>
  88. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议