1.  
  2. document.getElementsByTagName("body")[0].style.backgroundColor="#000"
  3. //构造函数
  4. function Ball( size, left1, top1, color, step, directionLeft, directionTop, timeSpace,opc){
  5. this.dom = null
  6. this.size = size
  7. this.left1 = left1
  8. this.top1 = top1
  9. this.color = color
  10. this.step = step
  11. this.timeSpace = timeSpace
  12. this.directionLeft = directionLeft
  13. this.directionTop = directionTop
  14. this.opc = opc
  15. //创建球
  16. this.createDom = function(){
  17. this.dom = document.createElement("div")
  18. this.dom.style.cssText = `
  19. position: absolute;
  20. left: ${this.left1}px;
  21. top: ${this.top1}px;
  22. width: ${this.size}px;
  23. height: ${this.size}px;
  24. border-radius: 50%;
  25. background-color:${this.color};
  26. opacity: ${this.opc};
  27. z-index: -999999999;
  28. `;
  29. document.body.appendChild(this.dom)//吧效果加到body;里面
  30. }
  31.  
  32. //球的动作
  33. this.go = function () {
  34. setInterval(() => {
  35. this.left1 = this.left1 + this.directionLeft * this.step
  36. this.top1 = this.top1 + this.directionTop * this.step
  37.  
  38. // 边界判断
  39. let clientHeigth = document.documentElement.clientHeight || document.body.clientHeight
  40. let scrollTop = document.documentElement.scrollTop || document.body.scrollTop
  41. if (this.top1 + this.size > clientHeigth + scrollTop) {
  42. this.top1 = clientHeigth + scrollTop - this.size
  43. this.directionTop = -1
  44. } else if (this.top1 < scrollTop) {
  45. this.top1 = scrollTop
  46. this.directionTop = 1
  47. }
  48. let clientWidth = document.documentElement.clientWidth || document.body.clientWidth
  49. let scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft
  50. if (this.left1 + this.size > clientWidth + scrollLeft) {
  51. // 右边界
  52. this.left1 = clientWidth + scrollLeft - this.size
  53. this.directionLeft = -1
  54. } else if (this.left1 < scrollLeft) {
  55. //左边界
  56. this.left1 = scrollLeft
  57. this.directionLeft = 1
  58. }
  59.  
  60. this.dom.style.left = this.left1 + "px"
  61. this.dom.style.top = this.top1 + "px"
  62. }, this.timeSpace)
  63. }
  64. //调用
  65. this.createDom()
  66. this.go()
  67. }
  68.  
  69. window.onload = function () {
  70. var w = document.documentElement.clientWidth||document.body.clientWidth
  71. var h =document.documentElement.clientHeight||document.body.clientHeight
  72. for(var i=0;i<200;i++){
  73. // 随机大小(10-150)
  74. let size = parseInt(Math.random()*141)+10
  75. // 随机位置;
  76. let left1 = parseInt(Math.random()*w)
  77. let top1 = parseInt(Math.random()*h)
  78. // 随机颜色
  79. let color = getColor()
  80. // 随机步长(1-10)
  81. let step = parseInt(Math.random()*5)+1
  82. // 随机方向
  83. let directionLeft = parseInt(Math.random()*2)==0?-1:1 //0和1
  84. let directionTop = parseInt(Math.random()*2)==0?-1:1 //0和1
  85. // 随机时间间隔(5-50)
  86. let timeSpace = parseInt(Math.random()*46)+5
  87. //透明度
  88. let opc = (parseInt(Math.random()*6)+1)/10
  89.  
  90. new Ball( size, left1, top1, color, step, directionLeft, directionTop, timeSpace,opc)
  91. }
  92. }
  93. function getColor(){//随机颜色
  94. var str = "#"
  95. for(var i=0;i<6;i++){
  96. str += parseInt(Math.random()*16).toString(16)
  97. }
  98. return str
  99. }

js实现动态球球背景的更多相关文章

  1. js模拟抛出球运动

    js练手之模拟水平抛球运动 -匀加速运动 -匀减速运动 模拟运动有些基本的思路,当前所在点的坐标,元素的长宽是多少,向右/向下运动x/y增加,向上/向左运动x/y减少,运动的路程是多少,用什么方程进行 ...

  2. TYVJ4623 球球大作战·生存

    时间: 500ms / 空间: 65536KiB / Java类名: Main 背景 小天很喜欢玩球球大作战这个游戏,大家也应该都玩过.游戏规则是:移动自己的球,移动到别人的球(一定要比自己的球小)的 ...

  3. Atitit 动态按钮图片背景颜色与文字组合解决方案

    Atitit 动态按钮图片背景颜色与文字组合解决方案 转换背景颜色,setFont("cywe_img", fontScale, 50, 5) 设置文字大小与坐标 文字分拆,使用字 ...

  4. js实现动态操作table

     本章案例为通过js,动态操作table,实现在单页面进行增删改查的操作. 简要案例如下: <%@ page language="java" contentType=&quo ...

  5. 初探JavaScript(二)——JS如何动态操控HTML

    除去五一三天,我已经和<JavaScript Dom编程艺术>磨合了六天,第一印象很好.慢慢的,我发现这是一块排骨,除了肉还有骨头.遇到不解的地方就会多看几遍,实在不懂的先跳过,毕竟,初次 ...

  6. js的动态加载、缓存、更新以及复用(四)

    本来想一气呵成,把加载的过程都写了,但是卡着呢,所以只好在分成两份了. 1.页面里使用<script>来加载 boot.js . 2.然后在boot.js里面动态加载 bootLoad.j ...

  7. JS怎么动态命名变量名

    [摘要]本文是对JS怎么动态命名变量名的讲解,对学习JavaScript编程技术有所帮助,与大家分享. 1.用eval,例子: 1 2 3 4 5 6 7 <script> var Thr ...

  8. js中动态载入css js样式

    js中动态载入css样式,方法如下: //<link rel="stylesheet" type="text/css" href="http:/ ...

  9. js插件动态加载js、css解决方案

    最近因为工作需要做了一个js自动导入的插件,一开始很天真的以为动态创建个script添加到head中就ok了,试了之后才发现了问题,就是如果同时引入了多个js文件,而且后一个文件中用到了前一个文件中的 ...

  10. JS & JQuery 动态添加 select option

    因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接有时候会失效,所以 ...

随机推荐

  1. 恭喜你,Get到一份 正则表达式 食用指南

    先赞后看,养成习惯 前言 正则表达式 正则表达式: 定义一个搜索模式的字符串. 正则表达式可以用于搜索.编辑和操作文本. 正则对文本的分析或修改过程为:首先正则表达式应用的是文本字符串(text/st ...

  2. C++走向远洋——27(项目三,时间类)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:time.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  3. 我去,你写的 switch 语句也太老土了吧

    昨天早上通过远程的方式 review 了两名新来同事的代码,大部分代码都写得很漂亮,严谨的同时注释也很到位,这令我非常满意.但当我看到他们当中有一个人写的 switch 语句时,还是忍不住破口大骂:& ...

  4. PyQt5之音乐播放器

    按照自己思路简单写了个桌面播放器,只有自己喜欢的歌.使用QtDesigner设计的ui界面,功能简单,还有很多bug@~@,代码奉上: music_button.ui使用扩展工具pyuic5生成了mu ...

  5. 微信Android自动播放视频(可交互,设置层级,无控制条,非X5)ffmpeg,jsmpeg.js,.ts视频

    原料: ffmpeg : http://ffmpeg.zeranoe.com/builds/  win64 https://evermeet.cx/ffmpeg/   mac OS X 64 jsmp ...

  6. web 移动端 横向滚动的阻尼感很强,滑动不灵敏

    在添加 overflow-x: scroll的元素里增加如下style overflow-x: scroll; -webkit-overflow-scrolling: touch; //关键点

  7. [pdo_mysql.lo] Error 1 或者 [php_mysql.lo] Error 1

    make: *** [pdo_mysql.lo] Error 1 make: *** [php_mysql.lo] Error 1 这是因为这是因为在编译时需要 MySQL 的头的文件.而它按默认搜索 ...

  8. 认识Nginx

    无论你用浏览器还是APP访问多数网站,到达的第一站就是Nginx. 后来者居上的Nginx 千禧年前后,互联网业务迎来了高速发展,老牌的Web服务器都无法满足高性能.高可靠的市场需求. 一个开源的(遵 ...

  9. 简单易用的图像解码库介绍 —— stb_image

    原文链接:简单易用的图像解码库介绍 -- stb_image 说到图像解码库,最容易想起的就是 libpng 和 libjpeg 这两个老牌图像解码库了. libpng 和 libjpeg 分别各自对 ...

  10. LSP原则—关于正方形不是长方形

    长方形有二个属性长和宽.并有一个设置长的方法和设置宽的方法,还有一个求面积的方法. 像下面 private int length; private int width; public void set ...