jQuery實(shí)現(xiàn)的分子運(yùn)動(dòng)小球碰撞效果
本文實(shí)例講述了jQuery實(shí)現(xiàn)的分子運(yùn)動(dòng)小球碰撞效果。分享給大家供大家參考,具體如下:
先看效果圖吧,因?yàn)樾∏蚴沁\(yùn)動(dòng)狀態(tài)的,不好截圖,這里就先大體畫(huà)了一下路線,表示大體意思吧,如果想看真實(shí)的效果,自己粘貼下去運(yùn)行:

小球有點(diǎn)小喲,嘿嘿,沒(méi)有對(duì)細(xì)節(jié)進(jìn)行詳細(xì)的處理,如果像讓它完美一點(diǎn),自己處理下吧!這里是模擬的理想狀態(tài)下的,沒(méi)有摩擦力與組里的分子碰撞運(yùn)動(dòng),高科技喲~~~~~~mu~a
代碼也沒(méi)有整理,如果有心的話,把它整理成面向?qū)ο笮问降陌桑?/p>
代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript" >
var dim_half_past_PI = dimAngle(Math.PI / 2); // Math.PI/2的約數(shù)
var lastAngle = dimAngle(Math.PI/8); // 發(fā)射角度(0-dimAngle(Math.PI))
var v = 120; //飛行速度【>0】
var lastPosition = {}; // 最后一次碰撞坐標(biāo)
var lastTime = ""; // 最后一次碰撞時(shí)間
var lastDirection = "top"; // 開(kāi)始發(fā)射方向(top,bottom,left,right)
var horizen = 1; // 水品方向的積數(shù)
var vertical = 1; // 豎直方向的積數(shù)
var box = {};
function dimAngle(angle) {
var tempAngle = angle + "";
return parseFloat(tempAngle.substring(0, 6));
}
function getWillDirection(position, box) {
var direction = lastDirection;
if (position.x < box.left) {
direction = "right";
}
if (position.x > box.right) {
direction = "left"
}
if (position.y < box.top) {
direction = "bottom";
}
if (position.y > box.bottom) {
direction = "top";
}
return direction;
}
function getScale(direction, angle) {
switch(direction){
case "top":
vertical = -1;
if (angle < dim_half_past_PI) {
horizen = 1;
}
if (angle > dim_half_past_PI) {
horizen = -1;
}
if (angle == dim_half_past_PI) {
horizen = 0;
}
break;
case "left":
horizen = -1;
if (angle > dim_half_past_PI) {
vertical = 1;
}
if (angle < dim_half_past_PI) {
vertical = -1;
}
if (angle == dim_half_past_PI) {
vertical = 0;
}
break;
case "bottom":
vertical = 1;
if(angle > dim_half_past_PI) {
horizen = 1;
}
if(angle < dim_half_past_PI) {
horizen = -1;
}
if(angle == dim_half_past_PI) {
horizen = 0;
}
break;
case "right":
horizen = 1;
if (angle > dim_half_past_PI) {
vertical = -1;
}
if (angle < dim_half_past_PI) {
vertical = 1;
}
if (angle == dim_half_past_PI) {
vertical = 0;
}
break;
}
}
function getOutAngle(inAngle) {
if (inAngle == dim_half_past_PI || inAngle == 0) {
return inAngle;
} else {
return dim_half_past_PI - inAngle;
}
}
function setPosition(obj, position) {
obj.css({ "left": position.x +"px", "top": position.y +"px"});
}
function run(obj) {
var vx = Math.cos(lastAngle) * v;
var vy = Math.sin(lastAngle) * v;
var runTime = (new Date().getTime() - lastTime) / 1000;
getScale(lastDirection, lastAngle);
var sx = vx * runTime * horizen;
var sy = vy * runTime * vertical;
var position = {
x: lastPosition.x + sx,
y: lastPosition.y + sy
};
setPosition(obj, position);
var currentDirection = getWillDirection(position, box);
console.log(currentDirection +":"+lastDirection+":"+vertical+":"+horizen+":"+lastAngle+":"+dim_half_past_PI);
// 如果沒(méi)有發(fā)生碰撞
if (currentDirection != lastDirection) {
// 如果發(fā)生了碰撞
lastDirection = currentDirection;
lastPosition = position;
lastTime = new Date().getTime();
lastAngle = getOutAngle(lastAngle);
}
setTimeout(function () {
run(obj);
}, 30);
}
$(document).ready(function () {
var boxer = $("#box");
var x = parseInt(boxer.offset().left);
var y = parseInt(boxer.offset().top);
box = {
left: x,
top: y,
right: x + boxer.width(),
bottom: y + boxer.height()
};
var runner = $("#runner");
lastTime = new Date().getTime();
lastPosition = {
x: x,
y: y + boxer.height()
};
run(runner);
});
</script>
<style type="text/css" >
body { margin:0; padding:0; }
#box { margin:0 auto; width:500px; height:500px; border:3px solid #DDDDDD; border-radius:4px; -wekit-border-radius:4px; -moz-border-radius:4px;}
#runner { width:10px; height:10px; font-size:10px; color:black; padding:0; position:absolute; left:100px; top:480px;}
</style>
</head>
<body>
<div id="box">
<div id="runner">●</div>
</div>
</body>
</html>
更多關(guān)于jQuery特效相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《jQuery常見(jiàn)經(jīng)典特效匯總》及《jQuery動(dòng)畫(huà)與特效用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jquery實(shí)現(xiàn)隱藏在左側(cè)的彈性彈出菜單效果
- jQuery彈性滑動(dòng)導(dǎo)航菜單實(shí)現(xiàn)思路及代碼
- Jquery實(shí)現(xiàn)彈性滑塊滑動(dòng)選擇數(shù)值插件
- jQuery實(shí)現(xiàn)背景彈性滾動(dòng)的導(dǎo)航效果
- 基于jQuery的煙花效果(運(yùn)動(dòng)相關(guān))點(diǎn)擊屏幕出煙花
- jQuery實(shí)現(xiàn)跟隨鼠標(biāo)運(yùn)動(dòng)圖層效果的方法
- jQuery拋物線運(yùn)動(dòng)實(shí)現(xiàn)方法(附完整demo源碼下載)
- jQuery實(shí)現(xiàn)橫向帶緩沖的水平運(yùn)動(dòng)效果(附demo源碼下載)
- jquery模擬實(shí)現(xiàn)鼠標(biāo)指針停止運(yùn)動(dòng)事件
- jQuery模擬物體自由落體運(yùn)動(dòng)(附演示與demo源碼下載)
- jquery animate動(dòng)畫(huà)持續(xù)運(yùn)動(dòng)的實(shí)例
- jQuery插件實(shí)現(xiàn)彈性運(yùn)動(dòng)完整示例
相關(guān)文章
幻燈片帶網(wǎng)頁(yè)設(shè)計(jì)中的20個(gè)奇妙應(yīng)用示例小結(jié)
幻燈片效果在網(wǎng)站中的使用非常流行,使用幻燈片效果既能在有限的網(wǎng)頁(yè)空間內(nèi)展示更多的內(nèi)容,又能增強(qiáng)視覺(jué)趣味,網(wǎng)上眾多的幻燈片插件資源也使得幻燈片的實(shí)現(xiàn)變得十分簡(jiǎn)單2012-05-05
jquery點(diǎn)擊回車(chē)鍵實(shí)現(xiàn)登錄效果并默認(rèn)焦點(diǎn)的方法
下面小編就為大家分享一篇jquery點(diǎn)擊回車(chē)鍵實(shí)現(xiàn)登錄效果并默認(rèn)焦點(diǎn)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
jQuery循環(huán)遍歷子節(jié)點(diǎn)并獲取值的方法
這篇文章主要介紹了jQuery循環(huán)遍歷子節(jié)點(diǎn)并獲取值的方法,涉及jQuery節(jié)點(diǎn)的遍歷與屬性操作相關(guān)技巧,需要的朋友可以參考下2016-04-04
jQuery插件實(shí)現(xiàn)控制網(wǎng)頁(yè)元素動(dòng)態(tài)居中顯示
這篇文章主要介紹了jQuery插件實(shí)現(xiàn)控制網(wǎng)頁(yè)元素動(dòng)態(tài)居中顯示,實(shí)例分析了jQuery插件的實(shí)現(xiàn)與元素動(dòng)態(tài)顯示的技巧,需要的朋友可以參考下2015-03-03
基于jquery的橫向滾動(dòng)條(滑動(dòng)條)
ASP.Net的GridView本身不帶滾動(dòng)條,可通過(guò)Panel實(shí)現(xiàn)。但是Windows自帶的橫向滾動(dòng)條只支持顯示在下方,為了使用方便,需要在上下方都顯示橫向滾動(dòng)條。2011-02-02
jquery.validate.js 多個(gè)相同name的處理方式
本文通過(guò)代碼給大家介紹了jquery.validate.js 多個(gè)相同name的處理方式,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-07-07
ASP.NET jQuery 實(shí)例2 (表單中使用回車(chē)在TextBox之間向下移動(dòng))
每次當(dāng)用戶(hù)在一個(gè)文本框輸入完數(shù)據(jù)后,更希望在敲入回車(chē)鍵后,焦點(diǎn)會(huì)自動(dòng)移動(dòng)到下一個(gè)文本框2012-01-01
jQuery+PHP+Ajax實(shí)現(xiàn)動(dòng)態(tài)數(shù)字統(tǒng)計(jì)展示功能
這篇文章主要介紹了jQuery+PHP+Ajax實(shí)現(xiàn)動(dòng)態(tài)數(shù)字統(tǒng)計(jì)展示功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12

