JavaScript實(shí)現(xiàn)多個(gè)物體同時(shí)運(yùn)動(dòng)
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)多個(gè)物體同時(shí)運(yùn)動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
1、多個(gè)物體同時(shí)運(yùn)動(dòng)
---例子:多個(gè)Div,鼠標(biāo)移入變寬
單定時(shí)器,存在問(wèn)題
每個(gè)Div一個(gè)定時(shí)器
2、多物體運(yùn)動(dòng)框架
定時(shí)器作為物體的屬性
參數(shù)的傳遞:物體、目標(biāo)值
---例子:多個(gè)Div淡入淡出
所有東西都不能公用
屬性與運(yùn)動(dòng)對(duì)象綁定:速度、其他屬性值(如透明度等)
3、多個(gè)Div,鼠標(biāo)移入變寬的例子
3.1 代碼
<head> <meta charset="UTF-8"> <title>多物體運(yùn)動(dòng)框架</title> <style type="text/css"> div { width: 100px; height: 50px; background: red; margin-top: 50px; } </style> <script type="text/javascript"> window.onload = function() { //獲取元素 var aDiv = document.getElementsByTagName("div"); for (var i = 0; i < aDiv.length; i++) { aDiv[i].onmousemove = function() { startMove(this, 300); } aDiv[i].onmouseout = function() { startMove(this, 100); } } } //運(yùn)動(dòng)函數(shù) function startMove(obj, iTarget) { //清楚定時(shí)器函數(shù) //由于是是適應(yīng)于多個(gè)對(duì)象的變化,所以每個(gè)DIV有一個(gè)定時(shí)器 clearInterval(obj.timer); //定時(shí)器函數(shù) //每一個(gè)對(duì)象,有一個(gè)定時(shí)器 obj.timer = setInterval(function() { //記錄速度 var iSpeed = (iTarget - obj.offsetWidth) / 5; iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed); //運(yùn)動(dòng)和停止的判斷條件 if (obj.offsetWidth == iTarget) { //清楚定時(shí)器函數(shù) clearInterval(obj.timer); } else { obj.style.width = obj.offsetWidth + iSpeed + 'px'; } }, 30); } </script> </head> <body> <div></div> <div></div> <div></div> </body>
3.2 多個(gè)DIV淡入淡出的例子
<head> <meta charset="UTF-8"> <title>多物體運(yùn)動(dòng)框架</title> <style type="text/css"> div { width: 100px; height: 100px; background: red; margin-top: 50px; filter: alpha(opacity: 30); opacity: 0.3; } </style> <script type="text/javascript"> window.onload = function() { //獲取元素 var aDiv = document.getElementsByTagName("div"); for (var i = 0; i < aDiv.length; i++) { //將alpha作為,物體的一個(gè)屬性 aDiv[i].alpha = 30; aDiv[i].onmousemove = function() { startMove(this, 100); } aDiv[i].onmouseout = function() { startMove(this, 30); } } } //運(yùn)動(dòng)函數(shù) function startMove(obj, iTarget) { //清楚定時(shí)器函數(shù) clearInterval(obj.timer); //定時(shí)器函數(shù) obj.timer = setInterval(function() { //記錄速度 var iSpeed = (iTarget - obj.alpha) / 8; iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed); //運(yùn)動(dòng)和停止的判斷條件 if (obj.alpha == iTarget) { //清楚定時(shí)器函數(shù) clearInterval(obj.timer); } else { obj.alpha += iSpeed; obj.style.filter = 'alpha(opacity:' + obj.alpha + ')'; obj.style.opacity = obj.alpha / 100; } }, 30); } </script> </head> <body> <div></div> <div></div> <div></div> <div></div> </body>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS實(shí)現(xiàn)多物體運(yùn)動(dòng)的方法詳解
- JS運(yùn)動(dòng)改變單物體透明度的方法分析
- JS實(shí)現(xiàn)物體帶緩沖的間歇運(yùn)動(dòng)效果示例
- JS多物體實(shí)現(xiàn)緩沖運(yùn)動(dòng)效果示例
- js多個(gè)物體運(yùn)動(dòng)功能實(shí)例分析
- javascript多物體運(yùn)動(dòng)實(shí)現(xiàn)方法分析
- Javascript 多物體運(yùn)動(dòng)的實(shí)現(xiàn)
- JS實(shí)現(xiàn)多物體緩沖運(yùn)動(dòng)實(shí)例代碼
- JS多物體 任意值 鏈?zhǔn)?緩沖運(yùn)動(dòng)
- JS實(shí)現(xiàn)多物體運(yùn)動(dòng)
相關(guān)文章
JS對(duì)大量數(shù)據(jù)進(jìn)行多重過(guò)濾的方法
今天在工作中遇到一個(gè)問(wèn)題,當(dāng)前端通過(guò)Ajax從后端取得了大量的數(shù)據(jù),需要根據(jù)一些條件過(guò)濾,但是發(fā)現(xiàn)寫的過(guò)濾方法有問(wèn)題,后來(lái)仔細(xì)的查找問(wèn)題,通過(guò)網(wǎng)上的資料終于解決了這個(gè)問(wèn)題,現(xiàn)在將解決的過(guò)程以及解決方法分享給大家,有需要的朋友們可以參考借鑒。2016-11-11console.log()的作用與實(shí)現(xiàn)方式
這篇文章主要介紹了console.log()的作用與實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01JavaScript設(shè)置首頁(yè)和收藏頁(yè)面的小例子
這篇文章介紹了JavaScript設(shè)置首頁(yè)和收藏頁(yè)面的小例子,有需要的朋友可以參考一下2013-11-11JavaScript 中的 `==` 和 `===` 操作符詳解
在 JavaScript 中,== 和 === 是兩個(gè)常用的比較操作符,分別用于 寬松相等(類型轉(zhuǎn)換相等) 和 嚴(yán)格相等(類型和值必須相等) 的比較,理解它們的區(qū)別以及具體的比較規(guī)則對(duì)于編寫準(zhǔn)確和高效的代碼至關(guān)重要,需要的朋友可以參考下2024-09-09微信小程序手機(jī)號(hào)碼驗(yàn)證功能的實(shí)例代碼
這篇文章主要介紹了微信小程序手機(jī)號(hào)碼驗(yàn)證功能的實(shí)例代碼及微信小程序正則判斷手機(jī)號(hào)的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08