javascript实现弹球效果
来源:互联网 发布:软件开发过程及规范 编辑:程序博客网 时间:2024/05/04 02:00
javascript实现弹球效果
今天介绍的是一种通过javascript实现的一种炫酷的动画效果,具体实现特效我通过图片展示给大家
还有释放后这些小球会在一个指定范围内进行运动,最关键的部分就是这些小球的各种样式都是随机获取的,所以这样才出现了一个炫酷的效果,主要使用到的随机数生成的代码如下:
//获取一个范围内的随机数random返回一个大于0小于1的一个随机数
function selectFrom(Lowervalue,upperValue){
var choices=upperValue-Lowervalue+1;
return Math.floor(Math.random()*choices+Lowervalue);
}
之后这些小球的各种样式通过随机函数获得,剩下的一部分是就是有关定位方面的,每一个小球都是一个div,其都是绝对定位,通过offsetLeft来获取这些小球的相对于父容器的位置,防止其跑出边界,只要实现方式是,通过offsetLeft获得这个div的相对位置后,在判断当期移动到边界的时候,让这个div的速度等于速度的相反数,
//设置运行速度 Circle.prototype.run=function(){ var maxLeft=1435-this.r*2; var maxTop=700-this.r*2; var that=this; //使用间隔式计时器 setInterval(function(){ var left=that.div.offsetLeft + that.speedX; var top=that.div.offsetTop + that.speedY; if(left<=0) { left=0; that.speedX *=-1; } if(top<=0) { top=0; that.speedY *=-1; } if(left>=maxLeft) { left=maxLeft; that.speedX*=-1; } if(top>=maxTop) { top=maxTop; that.speedY*=-1; } that.div.style.left=left+"px"; that.div.style.top=top+"px"; },30) }
之后就看到这些弹球在一个范围内运动的效果了:
整个效果的完整代码如下:
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>躁动的小球</title> <style type="text/css"> *{ margin: 0; padding: 0; } div{ border-radius: 50%; position: absolute; } body{ width: 100%; height: 100%; background-image: url(img/1369966956468.jpg) ; background-repeat: no-repeat; } </style> <script type="text/javascript"> //获取一个范围内的随机数random返回一个大于0小于1的一个随机数 function selectFrom(Lowervalue,upperValue){ var choices=upperValue-Lowervalue+1; return Math.floor(Math.random()*choices+Lowervalue); } function Circle(){ this.div=document.createElement("div"); this.r=selectFrom(10,40); this.left=selectFrom(0,100); this.top=selectFrom(0,50); this.bg='rgba('+selectFrom(0,255)+','+selectFrom(0,255)+','+selectFrom(0,255)+','+Math.random()+')'; this.speedX=selectFrom(-10,10); this.speedY=selectFrom(-8,8); } //绘制小球 Circle.prototype.drawCirle=function(parent){ //将parent设置成该div的一个属性 this.parent=parent; var style=this.div.style; style.width=this.r*2+"px"; style.height=this.r*2+"px"; style.left=this.left+"px"; style.top=this.top+"px"; style.backgroundColor=this.bg; parent.appendChild(this.div); } //设置运行速度 Circle.prototype.run=function(){ var maxLeft=1435-this.r*2; var maxTop=700-this.r*2; var that=this; //使用间隔式计时器 setInterval(function(){ var left=that.div.offsetLeft + that.speedX; var top=that.div.offsetTop + that.speedY; if(left<=0) { left=0; that.speedX *=-1; } if(top<=0) { top=0; that.speedY *=-1; } if(left>=maxLeft) { left=maxLeft; that.speedX*=-1; } if(top>=maxTop) { top=maxTop; that.speedY*=-1; } that.div.style.left=left+"px"; that.div.style.top=top+"px"; },30) } </script> </head> <body> <div id=""></div> <script type="text/javascript"> for(var i=0;i<100;i++) { //这些函数里面涉及原型,通过 Circle()函数可以找到指向draw,run函数。 var c = new Circle(); c.drawCirle(document.body); c.run(); } </script> </body></html>
这里面还涉及到this的使用,在函数内部再使用另一个函数时,一定要判断当前函数的作用域,分清this指向的作用于域,在内部调用时,在外部可以使用变量来保存当前this指向的作用域,希望这些对你的学习能有所帮助。
阅读全文
0 0
- javascript实现弹球效果
- 原生JavaScript实现弹球游戏
- HTML5与Javascript 实现网页弹球游戏
- Cocos2d-x--伪物理世界,实现Box2D弹球效果
- Android动画之自定义Evaluator实现弹球效果
- 弹球小游戏(JavaScript)
- java实现弹球游戏
- Android 实现弹球游戏
- 安卓动画系列之一,妙用Interpolator 快速实现弹球效果
- 弹球游戏的编程实现
- Android动画之弹球落地效果
- JavaScript实现拖动效果
- javascript实现下雪效果
- javascript实现幻灯片效果
- JavaScript 实现画图板效果
- javascript 实现load效果
- javascript实现拖动效果
- javascript 实现sleep效果
- jzoj5241【GDOI2018模拟8.8】苹果和雪梨 贪心
- Maven引用本地包?
- 干货一千零一夜:Day 4 原来开宝马的人想的跟你不一样
- bzoj1023: [SHOI2008]cactus仙人掌图
- Redis客户端常见异常分析
- javascript实现弹球效果
- java代理模式详解
- 剖析Vue原理&实现双向绑定MVVM
- JZOJ 5232【NOIP2017模拟】带权排序(概率,线段树)
- java对象转换String类型的三种方法
- 【排序算法】冒泡排序 bubble sort(交换类排序)
- 计蒜客 学生成绩查找系统
- 第二章 Python数据导入
- VirtualBox的Nat模式设置及端口映射