博客列表 >飞舞的气泡

飞舞的气泡

坨坨
坨坨原创
2021年12月08日 16:50:15630浏览


<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>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议