博主信息
博文 16
粉丝 0
评论 0
访问量 13923
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
飞舞的气泡
坨坨
原创
1233人浏览过


<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>
批改老师: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+教程免费学