博主信息
博文 70
粉丝 1
评论 0
访问量 68726
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
飞舞的气泡
葡萄枝子
原创
865人浏览过

飞舞的气泡

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. p {
  8. text-align: center;
  9. }
  10. .container {
  11. position: relative;
  12. width: 500px;
  13. height: 500px;
  14. border: 1px solid #ccc;
  15. background-color: #fbfbfb;
  16. margin: auto;
  17. }
  18. .container > .ball {
  19. position: absolute;
  20. border-radius: 50%;
  21. box-shadow: 0 0 10px #888888 inset;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <p>
  27. <button id="pup-ball">弹球</button>
  28. </p>
  29. <div class="container"></div>
  30. <script>
  31. document.getElementById('pup-ball').onclick = function () {
  32. var box = document.getElementsByClassName('container')[0],
  33. ball = document.createElement('div'),
  34. size = 20 + Math.floor(Math.random() * 5) * 10,
  35. color = ['yellow', 'green', 'blue', 'purple', 'red'][Math.floor(Math.random() * 5)],
  36. stepx = (5 + Math.floor(Math.random() * 10)) * ([-1, 1].sort((a, b) => Math.random() - 0.5).shift()),
  37. stepy = (5 + Math.floor(Math.random() * 10)) * ([-1, 1].sort((a, b) => Math.random() - 0.5).shift()),
  38. top = box.clientWidth / 2 - size / 2,
  39. left = box.clientHeight / 2 - size / 2;
  40. ball.setAttribute('class', 'ball');
  41. box.appendChild(ball);
  42. var timer = setInterval(function () {
  43. ball.style.cssText = `width:${size}px;height:${size}px;background:${color};top:${top}px;left:${left}px`;
  44. top += stepy
  45. if (top < 0 || top > box.clientHeight - size) stepy = -stepy;
  46. left += stepx;
  47. if (left < 0 || left > box.clientWidth - size) stepx = -stepx;
  48. }, 30)
  49. }
  50. </script>
  51. </body>
  52. </html>

飞舞的气泡

批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学