php的正則處理函數總結分析
735
2023-12-09
本文實例講述了jQuery實現的分子運動小球碰撞效果。分享給大家供大家參考,具體如下:
先看效果圖吧,因為小球是運動狀態的,不好截圖,這里就先大體畫了一下路線,表示大體意思吧,如果想看真實的效果,自己粘貼下去運行:
小球有點小喲,嘿嘿,沒有對細節進行詳細的處理,如果像讓它完美一點,自己處理下吧!這里是模擬的理想狀態下的,沒有摩擦力與組里的分子碰撞運動,高科技喲~~~~~~mu~a
代碼也沒有整理,如果有心的話,把它整理成面向對象形式的吧!
代碼如下:
<!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="http://www.gimoo.net/t/1903/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的約數 var lastAngle = dimAngle(Math.PI/8); // 發射角度(0-dimAngle(Math.PI)) var v = 120; //飛行速度【>0】 var lastPosition = {}; // 最后一次碰撞坐標 var lastTime = ""; // 最后一次碰撞時間 var lastDirection = "top"; // 開始發射方向(top,bottom,left,right) var horizen = 1; // 水品方向的積數 var vertical = 1; // 豎直方向的積數 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); // 如果沒有發生碰撞 if (currentDirection != lastDirection) { // 如果發生了碰撞 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>
更多關于jQuery特效相關內容感興趣的讀者可查看本站專題:《jQuery常見經典特效匯總》及《jQuery動畫與特效用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
#免責聲明#
本站[綠夏技術導航]提供的一切軟件、教程和內容信息僅限用于學習和研究目的;不得將上述內容用于商業或者非法用途,否則,一切后果請用戶自負。本站信息來自網絡收集整理,版權爭議與本站無關。您必須在下載后的24個小時之內,從您的電腦或手機中徹底刪除上述內容。如果您喜歡該程序或內容,請支持正版,購買注冊,得到更好的正版服務。我們非常重視版權問題,如有侵權請郵件[admin@lxwl520.com]與我們聯系進行刪除處理。敬請諒解!