欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

飞舞的气泡

程序员文章站 2022-03-13 13:55:58
...

照猫画虎做了几遍

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>飞舞的气泡</title>
  8. <style >
  9. #dhk{
  10. background: lightgrey;
  11. width: 600px;
  12. height: 600px;
  13. box-shadow: 0px 5px 5px 3px gray;
  14. }
  15. #btn{
  16. width: 70px;
  17. height: 30px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="dhk"></div>
  23. <input type="button" value="点一点" id="btn">
  24. <script >
  25. var dhk = document.getElementById("dhk");
  26. var colors=['red', 'yellow', 'blue', 'green', 'yellowgreen', '#FF00FF'];
  27. document.getElementById('btn').onclick=function(){
  28. var ball = document.createElement('div');
  29. ball.style.cssText="position:absolute;border-radius:50%;box-shadow: 0px 3px 5px #666";
  30. var kg = Math.floor(Math.random()*70)+30;
  31. ball.style.width=kg+'px';
  32. ball.style.height= kg+'px';
  33. ball.style.backgroundColor=colors[Math.floor(Math.random()*colors.length)];
  34. dhk.appendChild(ball);
  35. var top = dhk.clientHeight/2 - ball.offsetHeight/2;
  36. var left = dhk.clientWidth/2 - ball.offsetWidth/2;
  37. ball.style.left=left+"px";
  38. ball.style.top=top+"px";
  39. var x=Math.floor(Math.random()*10)+1;
  40. var y=Math.floor(Math.random()*10)+1;
  41. setInterval(function() {
  42. left+=x;
  43. top+=y;
  44. if(left < dhk.offsetLeft || left > (dhk.offsetLeft+dhk.offsetWidth-ball.offsetWidth)) {
  45. x = -x;
  46. }
  47. if(top < dhk.offsetTop || top > (dhk.offsetTop+dhk.offsetHeight-ball.offsetHeight-3)) {
  48. y = -y;
  49. }
  50. ball.style.left=left+"px";
  51. ball.style.top=top+"px";
  52. }, 40);
  53. }
  54. </script>
  55. </body>
  56. </html>

飞舞的气泡