jquery制作 隨機(jī)彈跳的小球特效
更新時間:2015年02月01日 16:17:19 投稿:hebedich
這篇文章主要介紹了jquery制作 隨機(jī)彈跳的小球特效,需要的朋友可以參考下
以下是源碼:
復(fù)制代碼 代碼如下:
<!doctype html>
<html>
<head>
<title>HTML5 隨機(jī)彈跳的小球</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特效 隨機(jī)彈跳的小球</h1>
<div>請使用支持HTML5的瀏覽器開打本頁。 <button id="stop-keleyi-com">暫停</button>
<button id="run-keleyi-com">繼續(xù)</button>
<button id="addBall-keleyi-com">增加小球</button> <button onclick="javascript:window.location.reload();">刷新</button>
<br />每次刷新頁面的小球大小,顏色,運(yùn)動路線,都是新的,可以點(diǎn)擊上面各個按鈕看看效果。
</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) {//生產(chǎn)隨機(jī)數(shù)
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>
相關(guān)文章
JavaScript的jQuery庫中ready方法的學(xué)習(xí)教程
這篇文章主要介紹了JavaScript的jQuery庫中ready方法的學(xué)習(xí)教程,包括ready的相關(guān)簡短寫法,rally cool,需要的朋友可以參考下2015-08-08
終于實(shí)現(xiàn)了!精彩的jquery彈幕效果
終于實(shí)現(xiàn)精彩的jquery彈幕效果了,這篇文章就為大家詳細(xì)介紹了jquery彈幕效果的實(shí)現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
IE中的File域無法清空使用jQuery重設(shè)File域
IE中的File域無法清空,可能你看到的是已經(jīng)沒有值了,其實(shí)它還是存在的,下面有個不錯示例,大家可以參考下2014-04-04
jQuery學(xué)習(xí)筆記之Ajax用法實(shí)例詳解
這篇文章主要介紹了jQuery學(xué)習(xí)筆記之Ajax用法,結(jié)合實(shí)例形式較為詳細(xì)的分析總結(jié)了jQuery中ajax的相關(guān)使用技巧,包括ajax請求、載入、處理、傳遞等,需要的朋友可以參考下2015-12-12
jquery 取子節(jié)點(diǎn)及當(dāng)前節(jié)點(diǎn)屬性值
本節(jié)主要介紹了jquery如何取子節(jié)點(diǎn)及當(dāng)前節(jié)點(diǎn)屬性值,需要的朋友可以參考下2014-07-07
基于jQuery的固定表格頭部的代碼(IE6,7,8測試通過)
目前只能算個不完整的腳本,不過一般的僅僅需要表頭凍結(jié)就可以使用了2010-05-05
jquery parent和parents的區(qū)別分析
從字面上不難看出parent是指取得一個包含著所有匹配元素的唯一父元素的元素集合。parents則是取得一個包含著所有匹配元素的祖先元素的元素集合(不包含根元素)。可以通過一個可選的表達(dá)式進(jìn)行篩選2013-10-10

