列子:多个div,变宽,变高
注:多物体运动框架中所有东西都不能公用
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
div{width:200px;height:200px;background: yellow;margin:10px;float:left;border:2px solid black;}
</style>
<script>
window.function()
{
var oDiv1=document.getElementById('div1');
var oDiv2=document.getElementById('div2');
oDiv1.onmouseover=function()
{
startMove(this,'height',400);
}
oDiv1.onmouseout=function()
{
startMove(this,'height',200);
}
oDiv2.onmouseover=function()
{
startMove(this,'width',400);
}
oDiv2.onmouseout=function()
{
startMove(this,'width',200);
}
}
function getStyle(obj,name)
{
if(obj.currentStyle)
{
return obj.currentStyle[name];
}
else
{
return getComputedStyle(obj,false)[name];
}
}
var timer=null;
function startMove(obj,attr,iTarget)
{
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var cur=parseInt(getStyle(obj,attr));
var speed=(iTarget-cur)/6;
speed=speed>0?Math.ceil(speed):Math.floor(speed)
if(cur==iTarget)
{
clearInterval(obj.timer);
}
else
{
obj.style[attr]=cur+speed+'px';
}
},30)
}
</script>
</head>
<body>
<div id="div1">变高</div>
<div id="div2">变宽</div>
</body>
</html>
效果图如下所示: