博主信息
博文 119
粉丝 3
评论 1
访问量 121185
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
飞舞的小球演示
赵大叔
原创
724人浏览过

html代码

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>Title</title>
  5. <style>
  6. #content {
  7. width:800px;
  8. height:600px;
  9. background: rgb(252, 220, 220);
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <input type="button" value="弹出球" id="btn">
  15. <div id="content">
  16. </div>
  17. <script>
  18. var content = document.getElementById('content');
  19. var colors = ['red', 'yellow', 'blue', 'green', 'yellowgreen', '#FF00FF'];
  20. document.getElementById('btn').onclick = function(){
  21. var ball = document.createElement('div');
  22. ball.style.cssText = "position:absolute;border-radius:50%;box-shadow: 0px 3px 5px #666;";
  23. var wh = Math.floor(Math.random() * 60) + 20;
  24. ball.style.width = wh + 'px';
  25. ball.style.height = wh + 'px';
  26. ball.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
  27. content.appendChild(ball);
  28. var top = content.clientHeight/2 - ball.offsetHeight/2;
  29. var left = content.clientWidth/2 - ball.offsetWidth/2;
  30. ball.style.left = left + "px";
  31. ball.style.top = top + "px";
  32. var x = Math.floor(Math.random() * 10) + 1;
  33. var y = Math.floor(Math.random() * 10) + 1;
  34. setInterval(function(){
  35. left += x;
  36. top += y;
  37. if(left < content.offsetLeft || left > (content.offsetLeft + content.offsetWidth - ball.offsetWidth)) {
  38. x = -x;
  39. }
  40. if(top < content.offsetTop || top > (content.offsetTop + content.offsetHeight - ball.offsetHeight - 3)) {
  41. y = -y;
  42. }
  43. ball.style.left = left + "px";
  44. ball.style.top = top + "px";
  45. }, 50)
  46. }
  47. </script>
  48. </body>
  49. </html>

效果展示

批改老师:PHPzPHPz

批改状态:合格

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

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

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