本文實例講述了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="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程式設計有所幫助。
Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。

JavaScript框架的強大之處在於簡化開發、提升用戶體驗和應用性能。選擇框架時應考慮:1.項目規模和復雜度,2.團隊經驗,3.生態系統和社區支持。

引言我知道你可能會覺得奇怪,JavaScript、C 和瀏覽器之間到底有什麼關係?它們之間看似毫無關聯,但實際上,它們在現代網絡開發中扮演著非常重要的角色。今天我們就來深入探討一下這三者之間的緊密聯繫。通過這篇文章,你將了解到JavaScript如何在瀏覽器中運行,C 在瀏覽器引擎中的作用,以及它們如何共同推動網頁的渲染和交互。 JavaScript與瀏覽器的關係我們都知道,JavaScript是前端開發的核心語言,它直接在瀏覽器中運行,讓網頁變得生動有趣。你是否曾經想過,為什麼JavaScr


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6
視覺化網頁開發工具