Blogger Information
Blog 14
fans 0
comment 0
visits 8039
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
飞舞的泡泡球
「flasky」
Original
530 people have browsed it

飞舞的泡泡球
效果图如下

  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>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post