jquery制作 随机弹跳的小球特效


Posted in Javascript onFebruary 01, 2015

以下是源码:

 <!doctype html>

 <html>

 <head>

 <title>HTML5 随机弹跳的小球</title>

 <style>

 body{

 font-family: 微软雅黑;    

 }

 body,h1{

 margin:0;

 }

 canvas{

 display:block;margin-left: auto;margin-right: auto;

 border:1px solid #DDD;    

 background: -webkit-linear-gradient(top, #222,#111);

 }    

 </style>

 </head>

 <body>

 <h1>HTML5特效 随机弹跳的小球</h1>

 <div>请使用支持HTML5的浏览器开打本页。 <button id="stop-keleyi-com">暂停</button>

 <button id="run-keleyi-com">继续</button>

 <button id="addBall-keleyi-com">增加小球</button> <button onclick="javascript:window.location.reload();">刷新</button>

 <br />每次刷新页面的小球大小,颜色,运动路线,都是新的,可以点击上面各个按钮看看效果。

 </div>

 <canvas id="canvas-keleyi-com" >

 </canvas>

 <script type="text/javascript" src="http://keleyi.com/keleyi/pmedia/jquery/jquery-1.11.0.min.js"></script>

 <script type="text/javascript">

 var nimo = {

 aniamted: null,

 content: null,

 data: {

 radiusRange: [5, 20],

 speedRange: [-5, 5],

 scrollHeight: null,

 scrollWdith: null

 },

 balls: [],

 ele: {

 canvas: null

 },

 fn: {

 creatRandom: function (startInt, endInt) {//生产随机数

 var iResult;

 iResult = startInt + (Math.floor(Math.random() * (endInt - startInt + 1)));

 return iResult

 },

 init: function () {

 nimo.data.scrollWdith = document.body.scrollWidth;

 nimo.data.scrollHeight = document.body.scrollHeight;

 nimo.ele.canvas = document.getElementById('canvas-ke'+'leyi-com');

 nimo.content = nimo.ele.canvas.getContext('2d');

 nimo.ele.canvas.width = nimo.data.scrollWdith - 50;

 nimo.ele.canvas.height = nimo.data.scrollHeight - 100;

 },

 addBall: function () {

 var aRandomColor = [];

 aRandomColor.push(nimo.fn.creatRandom(0, 255));

 aRandomColor.push(nimo.fn.creatRandom(0, 255));

 aRandomColor.push(nimo.fn.creatRandom(0, 255));

 var iRandomRadius = nimo.fn.creatRandom(nimo.data.radiusRange[0], nimo.data.radiusRange[1]);

 var oTempBall = {

 coordsX: nimo.fn.creatRandom(iRandomRadius, nimo.ele.canvas.width - iRandomRadius),

 coordsY: nimo.fn.creatRandom(iRandomRadius, nimo.ele.canvas.height - iRandomRadius),

 radius: iRandomRadius,

 bgColor: 'rgba(' + aRandomColor[0] + ',' + aRandomColor[1] + ',' + aRandomColor[2] + ',0.5)'

 };

 oTempBall.speedX = nimo.fn.creatRandom(nimo.data.speedRange[0], nimo.data.speedRange[1]);

 if (oTempBall.speedX === 0) {

 oTempBall.speedX = 1

 }

 if (oTempBall.speedY === 0) {

 oTempBall.speedY = 1

 }

 oTempBall.speedY = nimo.fn.creatRandom(nimo.data.speedRange[0], nimo.data.speedRange[1]);

 nimo.balls.push(oTempBall)

 },

 drawBall: function (bStatic) {

 var i, iSize;

 nimo.content.clearRect(0, 0, nimo.ele.canvas.width, nimo.ele.canvas.height)

 for (var i = 0, iSize = nimo.balls.length; i < iSize; i++) {

 var oTarger = nimo.balls[i];

 nimo.content.beginPath();

 nimo.content.arc(oTarger.coordsX, oTarger.coordsY, oTarger.radius, 0, 10);

 nimo.content.fillStyle = oTarger.bgColor;

 nimo.content.fill();

 if (!bStatic) {

 if (oTarger.coordsX + oTarger.radius >= nimo.ele.canvas.width) {

 oTarger.speedX = -(Math.abs(oTarger.speedX))

 }

 if (oTarger.coordsX - oTarger.radius <= 0) {

 oTarger.speedX = Math.abs(oTarger.speedX)

 }

 if (oTarger.coordsY - oTarger.radius <= 0) {

 oTarger.speedY = Math.abs(oTarger.speedY)

 }

 if (oTarger.coordsY + oTarger.radius >= nimo.ele.canvas.height) {

 oTarger.speedY = -(Math.abs(oTarger.speedY))

 }

 oTarger.coordsX = oTarger.coordsX + oTarger.speedX;

 oTarger.coordsY = oTarger.coordsY + oTarger.speedY;

 }

 }

 },

 run: function () {

 nimo.fn.drawBall();

 nimo.aniamted = setTimeout(function () {

 nimo.fn.drawBall();

 nimo.aniamted = setTimeout(arguments.callee, 10)

 }, 10)

 },

 stop: function () {

 clearTimeout(nimo.aniamted)

 }

 }

 }

 window.onload = function () {

 nimo.fn.init();

 var i;

 for (var i = 0; i < 10; i++) {

 nimo.fn.addBall();

 }

 nimo.fn.run();

 document.getElementById('stop-kel'+'eyi-com').onclick = function () {

 nimo.fn.stop()

 }

 document.getElementById('run-keley'+'i-com').onclick = function () {

 nimo.fn.stop()

 nimo.fn.run()

 }

 document.getElementById('addBall-k'+'eleyi-com').onclick = function () {

 var i;

 for (var i = 0; i < 10; i++) {

 nimo.fn.addBall();

 }

 nimo.fn.drawBall(true);

 }

 }

 </script>

 </body>

 </html>
Javascript 相关文章推荐
鼠标焦点离开文本框时验证的js代码
Jul 19 Javascript
基于jquery实现的文字淡入淡出效果
Nov 14 Javascript
使用javascript为网页增加夜间模式
Jan 26 Javascript
HTML页面弹出居中可拖拽的自定义窗口层
May 07 Javascript
深入理解javascript变量声明
Nov 20 Javascript
jquery中EasyUI使用技巧小结
Feb 10 Javascript
JS判断网页广告是否被浏览器拦截过滤的代码
Apr 05 Javascript
jQuery 1.9.1源码分析系列(十四)之常用jQuery工具
Dec 02 Javascript
JavaScript设计模式之单体模式全面解析
Sep 09 Javascript
jQuery实现可兼容IE6的滚动监听功能
Sep 20 jQuery
VUE在for循环里面根据内容值动态的加入class值的方法
Aug 12 Javascript
js模拟F11页面全屏显示
Sep 17 Javascript
jQuery实现单击和鼠标感应事件
Feb 01 #Javascript
jquery制作LED 时钟特效
Feb 01 #Javascript
thinkphp 表名 大小写 窍门
Feb 01 #Javascript
javascript实现带节日和农历的日历特效
Feb 01 #Javascript
2种jQuery 实现刮刮卡效果
Feb 01 #Javascript
jQuery实现炫酷的鼠标轨迹特效
Feb 01 #Javascript
jQuery+CSS3实现树叶飘落特效
Feb 01 #Javascript
You might like
PHP会话处理的10个函数
2015/08/11 PHP
PHP实现图片的等比缩放和Logo水印功能示例
2017/05/04 PHP
jquery validate在ie8下的bug解决方法
2013/11/13 Javascript
JavaScript中的值是按值传递还是按引用传递问题探讨
2015/01/30 Javascript
javaScript中push函数用法实例分析
2015/06/08 Javascript
JavaScript模拟鼠标右键菜单效果
2020/12/08 Javascript
Bootstrap开关(switch)控件学习笔记分享
2016/05/30 Javascript
JS框架之vue.js(深入三:组件1)
2016/09/29 Javascript
Bootstrap CSS组件之分页(pagination)和翻页(pager)
2016/12/17 Javascript
Angular的事件和表单详解
2016/12/26 Javascript
ES6学习教程之对象的扩展详解
2017/05/02 Javascript
2种简单的js倒计时方式
2017/10/20 Javascript
解决Vue axios post请求,后台获取不到数据的问题方法
2018/08/11 Javascript
详解Eslint 配置及规则说明
2018/09/10 Javascript
如何利用 JS 脚本实现网页全自动秒杀抢购功能
2020/10/12 Javascript
Python中的两个内置模块介绍
2015/04/05 Python
Python在线运行代码助手
2016/07/15 Python
python中字符串类型json操作的注意事项
2017/05/02 Python
安装好Pycharm后如何配置Python解释器简易教程
2019/06/28 Python
python接口调用已训练好的caffe模型测试分类方法
2019/08/26 Python
Python视频编辑库MoviePy的使用
2020/04/01 Python
UI自动化定位常用实现方法代码示例
2020/10/27 Python
HTML5之tabindex属性全面解析
2016/07/07 HTML / CSS
美国购买体育、音乐会和剧院门票网站:SelectATicket
2019/09/08 全球购物
北京银河万佳Java面试题
2012/03/21 面试题
班长岗位职责
2013/11/10 职场文书
初中生庆国庆演讲稿范文2014
2014/09/25 职场文书
个人遵守党的政治纪律情况对照检查材料思想汇报
2014/09/25 职场文书
绍兴鲁迅故居导游词
2015/02/09 职场文书
婚礼伴郎致辞
2015/07/28 职场文书
学校学期工作总结
2015/08/13 职场文书
Go语言带缓冲的通道实现
2021/04/26 Golang
为什么MySQL 删除表数据 磁盘空间还一直被占用
2021/10/16 MySQL
OpenCV项目实践之停车场车位实时检测
2022/04/11 Python
在SQL Server中使用 Try Catch 处理异常的示例详解
2022/07/15 SQL Server
Redis配置外网可访问(redis远程连接不上)的方法
2022/12/24 Redis