<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>任意属性值</title>
<style type="text/css">
*{
margin: 0 auto;
padding: 0;
}
#box {
top:50px;
left:50px;
position: absolute;
list-style: none;
width:200px;
height: 200px;
background-color: yellow;
opacity:0.3;
border: 4px solid black;
*/
}
</style>
<script type="text/javascript">
window.onload=function(){
var box_1=document.getElementById('box');
box_1.onmouseover=function(){
startMove(this,'height',400,function(){
startMove(this,'opacity',100);
});};//将这里的this 换成指定的box_1就可以实现链式效果。不知道为什么this不行。
box_1.onmouseout=function(){
startMove(this,'height',200);};
var timer=null;
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
function startMove(obj,attr,tar,fn){
clearInterval(timer);
timer=setInterval(function(){
var icurr=0;
if(attr=='opacity'){
icurr=Math.round(parseFloat(getStyle(obj,attr))*100);
}else{
icurr=parseInt(getStyle(obj,attr));}
var incre=(tar-icurr)/10;
incre=incre>0?Math.ceil(incre):Math.floor(incre);
if(icurr==tar){
clearInterval(timer);
if(fn){
fn();}
}else{
if(attr=='opacity'){
obj.style[attr]=(icurr+incre)/100;}
else{
obj.style[attr]=icurr+incre+'px';}
}
},30)
}
}
</script>
</head>
<body>
<div id='box'>
</div>
</body>
</html>