<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>运动框架</title>
<style type="text/css">
*{margin: 0;padding: 0}
div{width: 100px;height: 100px;background-color: red; margin: 10px;opacity: 0.3}
</style>
<script>
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
function startMove(obj,json,fn){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var bStop=true; //所有运动都已经完成
for(var attr in json){
//1.取当前样式值
var icur=0;
if(attr=="opacity"){
icur=parseInt(parseFloat(getStyle(obj,attr))*100);
}else{
icur=parseInt(getStyle(obj,attr));
}
//2.计算速度
var iSpeed=(json[attr]-icur)/8;
iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
//3.检测停止
if(icur!=json[attr]){
bStop=false;
}
if(attr=="opacity"){
obj.style.filter="alpha(opacity:"+(icur+iSpeed)+")";//兼容老IE
obj.style.opacity=(icur+iSpeed)/100;
}else{
obj.style[attr]=icur+iSpeed+'px';
}
}
if(bStop){
clearInterval(obj.timer);
fn && fn();
}
},30);
};
//以上可以封装为move.js,满足多属性运动
window.οnlοad=function(){
var oDiv=document.getElementById("div1");
oDiv.οnmοuseοver=function(){
startMove(oDiv,{opacity:100, height:200,width:200},function(){
alert("ok");
});
}
oDiv.οnmοuseοut=function(){
startMove(oDiv,{opacity:50,height:100,width:100});
}
}
</script>
</script>
</head>
<body>
<div id="div1"></div>
</body>
</html>