ASCII码 ASCII码

飞舞的气泡

发布于:2021-12-09 10:01:44  栏目:技术文档

<html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style>

  1. #main {
  2. width:800px;
  3. height:600px;
  4. background: #ccc;
  5. position: relative;
  6. }
  7. ball{
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <input type="button" value="弹出球" id="btn">
  13. <div id="main">
  14. </div>
  15. <script>
  16. var main = document.getElementById('main');
  17. var btn = document.getElementById('btn');
  18. btn.onclick = function () {
  19. var color = "#"+parseInt(Math.random()*1000);
  20. var wh = Math.floor(Math.random()*50)+20;
  21. var ball = document.createElement('div')
  22. ball.style.cssText = 'position: absolute;border-radius: 50%;'
  23. ball.style.background = color;
  24. ball.style.width = wh+'px';
  25. ball.style.height = wh+'px';
  26. var top = main.clientHeight/2 - wh;
  27. var left = main.clientWidth/2 - wh;
  28. ball.style.top = top+'px';
  29. ball.style.left = left+'px';
  30. main.appendChild(ball)
  31. var x = Math.floor(Math.random()*20)+1
  32. var y = Math.floor(Math.random()*20)+1
  33. setInterval(function () {
  34. left+=x;
  35. top+=y;
  36. if (left<=main.offsetLeft || left > (main.offsetLeft+main.offsetWidth-ball.offsetWidth) ){
  37. x = -x;
  38. }
  39. if (top<=main.offsetTop || top > (main.offsetTop+main.offsetHeight-ball.offsetHeight-3)){
  40. y= -y;
  41. }
  42. ball.style.left = left
  43. ball.style.top = top
  44. },100)
  45. }
  46. </script>
  47. </body>
  48. </html>
相关推荐
阅读 +