飞舞的气泡

  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>

飞舞的气泡

更多相关文章

  1. 飞舞的小球
  2. 【源码实例】纯CSS水波纹效果和动画气泡
  3. Android实现气泡漂浮动画,类似IOS Game Center中气泡动画
  4. 【原创】android 实现气泡式listview
  5. Android(安卓)弹出菜单(类iOS UIMenuController) 气泡菜单
  6. DiscreteSeekBar使用简介,一个带气泡的SeekBar
  7. Android(安卓)>> 27. Android(安卓)Studio 9-Patch 图片制作以及
  8. android实现桌面icon未读信息图标(类似与iPhone的badge)
  9. Android(安卓)自定义View双向选择带气泡SeekBar

随机推荐

  1. Android 实例子源代码文件下载地址380个
  2. Android基本控件常用属性及方法
  3. android facebook authorize 时禁止调用f
  4. Android之Android(安卓)Studio JNI
  5. android HTTPURLConnection解决不能访问H
  6. Android Firebase Dynamic Links 动态链
  7. 关于安卓 TextView 添加点击事件
  8. Android(安卓)studio的NDK配置
  9. 安卓 相对布局属性大全
  10. Android(安卓)Studio启动崩溃的问题:Start