document.getElementsByTagName("body")[0].style.backgroundColor="#000"
//构造函数
function Ball( size, left1, top1, color, step, directionLeft, directionTop, timeSpace,opc){
this.dom = null
this.size = size
this.left1 = left1
this.top1 = top1
this.color = color
this.step = step
this.timeSpace = timeSpace
this.directionLeft = directionLeft
this.directionTop = directionTop
this.opc = opc
//创建球
this.createDom = function(){
this.dom = document.createElement("div")
this.dom.style.cssText = `
position: absolute;
left: ${this.left1}px;
top: ${this.top1}px;
width: ${this.size}px;
height: ${this.size}px;
border-radius: 50%;
background-color:${this.color};
opacity: ${this.opc};
z-index: -999999999;
`;
document.body.appendChild(this.dom)//吧效果加到body;里面
} //球的动作
this.go = function () {
setInterval(() => {
this.left1 = this.left1 + this.directionLeft * this.step
this.top1 = this.top1 + this.directionTop * this.step // 边界判断
let clientHeigth = document.documentElement.clientHeight || document.body.clientHeight
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop
if (this.top1 + this.size > clientHeigth + scrollTop) {
this.top1 = clientHeigth + scrollTop - this.size
this.directionTop = -1
} else if (this.top1 < scrollTop) {
this.top1 = scrollTop
this.directionTop = 1
}
let clientWidth = document.documentElement.clientWidth || document.body.clientWidth
let scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft
if (this.left1 + this.size > clientWidth + scrollLeft) {
// 右边界
this.left1 = clientWidth + scrollLeft - this.size
this.directionLeft = -1
} else if (this.left1 < scrollLeft) {
//左边界
this.left1 = scrollLeft
this.directionLeft = 1
} this.dom.style.left = this.left1 + "px"
this.dom.style.top = this.top1 + "px"
}, this.timeSpace)
}
//调用
this.createDom()
this.go()
} window.onload = function () {
var w = document.documentElement.clientWidth||document.body.clientWidth
var h =document.documentElement.clientHeight||document.body.clientHeight
for(var i=0;i<200;i++){
// 随机大小(10-150)
let size = parseInt(Math.random()*141)+10
// 随机位置;
let left1 = parseInt(Math.random()*w)
let top1 = parseInt(Math.random()*h)
// 随机颜色
let color = getColor()
// 随机步长(1-10)
let step = parseInt(Math.random()*5)+1
// 随机方向
let directionLeft = parseInt(Math.random()*2)==0?-1:1 //0和1
let directionTop = parseInt(Math.random()*2)==0?-1:1 //0和1
// 随机时间间隔(5-50)
let timeSpace = parseInt(Math.random()*46)+5
//透明度
let opc = (parseInt(Math.random()*6)+1)/10 new Ball( size, left1, top1, color, step, directionLeft, directionTop, timeSpace,opc)
}
}
function getColor(){//随机颜色
var str = "#"
for(var i=0;i<6;i++){
str += parseInt(Math.random()*16).toString(16)
}
return str
}

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. create view and switch view

    pageView扩展backbone cAbstractApp定义view加载.切换.回退.跳转-webApp/cWebViewApp/hybirdApp为其子类 1.cWebApp扩展了父类的bin ...

  2. C++走向远洋——36(数组做数据成员,工资)

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

  3. django之初建项目

    一.项目预览 1.在创建项目之前,必须先进入虚拟环境,因为我们的包安装在我们的虚拟环境中,不在我们的中环境中 >>> ./venv/Scripts/activate 2.创建一个项目 ...

  4. tfgan折腾笔记(一):核心功能简要概述

    tfgan是什么? tfgan是tensorflow团队开发出的一个专门用于训练各种GAN的轻量级库,它是基于tensorflow开发的,所以兼容于tensorflow.在tensorflow1.x版 ...

  5. 前阿里数据库专家总结的MySQL里的各种锁(下篇)

    在上篇中,我们介绍了MySQL中的全局锁和表锁. 今天,我们专注于介绍一下行锁,这个在日常开发和面试中常常困扰我们的问题. 1.行锁基础 由于全局锁和表锁对增删改查的性能都会有较大影响,所以,我们自然 ...

  6. 7-10 jmu-python-异常-学生成绩处理基本版 (15 分)

    小明在帮老师处理数据,这些数据的第一行是n,代表有n行整数成绩需要统计.数据没有错误,则计算平均值(保留2位小数)并输出.数据有错误,直接停止处理,并且不进行计算. 注:该程序可以适当处理小错误,比如 ...

  7. JavaScript sort() 对json进行排序(数组)

    function up(x,y){//升序 return x[val.prop] - y[val.prop] } function down(x,y){//降序 return y[val.prop] ...

  8. Windows安装python包出现PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问的问题解决方案

    在python中安装sqlalchemy时,总是提示(当安装依赖有vs的python包时,可能会出现以下错误:) PermissionError: [WinError 32] 另一个程序正在使用此文件 ...

  9. python之嵌套 闭包 装饰器 global、nonlocal关键字

    嵌套: 在函数的内部定义函数闭包: 符合开放封闭原则:在不修改源代码与调用方式的情况下为函数添加新功能  # global 将局部变量变成全局变量 num = 100 def fn1(): globa ...

  10. ant tree 展开key的集合

    这次有个功能 ant的tree 展开 点击子节点 新增节点之后 数据能够照常展开 有几种方法 我能想到的 因为ant 有个expanded 只要设置为true就能展开了,但是这边有个陷阱,就是仅仅设置 ...