博主信息
博文 8
粉丝 0
评论 0
访问量 6682
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
点击弹出球
风轻云淡
原创
1220人浏览过
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>html基础</title>
  5. <meta charset="UTF-8">
  6. </head>
  7. <style>
  8. *{
  9. padding: 0;
  10. margin: 0;
  11. }
  12. .box{
  13. width: 500px;
  14. height: 500px;
  15. background-color: pink;
  16. position: relative;
  17. }
  18. </style>
  19. <body>
  20. <button id="btn" onclick="fn()">弹出球</button>
  21. <div class="box" id="box">
  22. </div>
  23. </div>
  24. <script>
  25. var obox= document.getElementById('box')
  26. var colors=['red','black','white','green','yellow']
  27. function fn() {
  28. var oball= document.createElement('div')
  29. oball.style.cssText='border-radius: 50%;box-shadow: 1px 2px #666;position: absolute;background-color:red'
  30. var wh=Math.floor(Math.random()*(50-20)+20)
  31. oball.style.width=wh+'px'
  32. oball.style.height=wh+'px'
  33. oball.style.backgroundColor=colors[Math.floor(Math.random()*colors.length)]
  34. obox.appendChild(oball)
  35. var top = obox.clientHeight/2 - oball.offsetHeight/2;//?
  36. var left = obox.clientWidth/2 - oball.offsetWidth/2;//?
  37. oball.style.left=left+'px'
  38. oball.style.top=top+'px'
  39. var x=Math.floor(Math.random()*(7-2)+2)
  40. var y=Math.floor(Math.random()*(7-2)+2)
  41. setInterval(function () {
  42. left+=x
  43. top+=y
  44. if (left< obox.offsetLeft || left > (obox.offsetLeft+obox.offsetWidth-oball.offsetWidth)) {
  45. x = -x
  46. }
  47. if (top < obox.offsetTop || top > (obox.offsetTop+obox.offsetHeight-oball.offsetHeight-3)) {
  48. y = -y
  49. }
  50. oball.style.left=left+"px";
  51. oball.style.top=top+"px";
  52. }, 30)
  53. }
  54. </script>
  55. </body>
  56. </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+教程免费学