javascript多物體運(yùn)動(dòng)實(shí)現(xiàn)方法分析
本文實(shí)例講述了javascript多物體運(yùn)動(dòng)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
這里需要注意:每個(gè)運(yùn)動(dòng)物體的定時(shí)器作為物體的屬性獨(dú)立出來(lái)互不影響,屬性與運(yùn)動(dòng)對(duì)象綁定,不能公用。
運(yùn)行效果截圖如下:

例子:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>多物體運(yùn)動(dòng)</title>
<style>
div{ width:100px; height:100px; background:red; float:left; margin:10px; border:1px solid black; opacity:0.3; filter:alpha(opacity=30);}
</style>
<script>
window.onload = function()
{
var aDiv = document.getElementsByTagName('div');
aDiv[0].onmouseover = function()
{
startMove(this, 'width', 300);
};
aDiv[0].onmouseout = function()
{
startMove(this, 'width', 100);
};
aDiv[1].onmouseover = function()
{
startMove(this, 'height', 300);
};
aDiv[1].onmouseout = function()
{
startMove(this, 'height', 100);
};
aDiv[2].onmouseover = function()
{
startMove(this, 'opacity', 100);
};
aDiv[2].onmouseout = function()
{
startMove(this, 'opacity', 30);
};
aDiv[3].onmouseover = function()
{
startMove(this, 'borderWidth', 20);
};
aDiv[3].onmouseout = function()
{
startMove(this, 'borderWidth', 1);
};
};
function getStyle(obj, attr)
{
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj, false)[attr];
}
}
function startMove(obj, attr, iTarget)
{
clearInterval(obj.timer);
obj.timer = setInterval(function(){
var iCur = 0;
if(attr == 'opacity'){
iCur = parseInt(parseFloat(getStyle(obj, attr)) * 100);
}else{
iCur = parseInt(getStyle(obj, attr));
}
var iSpeed = (iTarget - iCur) / 8;
iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
if(iCur == iTarget){
clearInterval(obj.timer);
}else{
if(attr == 'opacity'){
obj.style.filter = 'alpha(opacity='+ (iCur+iSpeed) +')';
obj.style.opacity = (iCur+iSpeed)/100;
}else{
obj.style[attr] = iCur + iSpeed + 'px';
}
}
}, 30);
}
</script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
更多關(guān)于JavaScript運(yùn)動(dòng)效果相關(guān)內(nèi)容可查看本站專(zhuān)題:《JavaScript運(yùn)動(dòng)效果與技巧匯總》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- JavaScript實(shí)現(xiàn)多個(gè)物體同時(shí)運(yùn)動(dòng)
- 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)
- 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)文章
Javascript中函數(shù)分類(lèi)&this指向的實(shí)例詳解
其實(shí)想要徹底理解js中this的指向,不必硬背,這篇文章主要給大家介紹了關(guān)于Javascript中函數(shù)分類(lèi)&this指向的相關(guān)資料,需要的朋友可以參考下2021-05-05
JavaScript面向?qū)ο髮?shí)現(xiàn)貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了JavaScript面向?qū)ο髮?shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
uni-app返回上一個(gè)頁(yè)面并進(jìn)行頁(yè)面刷新的方法
這篇文章主要給大家介紹了關(guān)于uni-app返回上一個(gè)頁(yè)面并進(jìn)行頁(yè)面刷新的相關(guān)資料,返回上一頁(yè)面在uniapp有多種方法,文中給出了詳細(xì)的圖文示例,需要的朋友可以參考下2023-07-07
window.location的重寫(xiě)及判斷l(xiāng)ocation是否被重寫(xiě)
這篇文章主要介紹了window.location的重寫(xiě)及判斷l(xiāng)ocation是否被重寫(xiě),需要的朋友可以參考下2014-09-09
javascript + jquery實(shí)現(xiàn)定時(shí)修改文章標(biāo)題
用javascript+jquery寫(xiě)的一個(gè)定時(shí)器,定時(shí)修改文章標(biāo)題,需要的朋友可以參考下2014-03-03

