該實(shí)例可實(shí)現(xiàn)鼠標(biāo)移上,先寬度變化,再高度變化,最后透明度變化,鼠標(biāo)移出,再依次變回去的效果。
要點(diǎn)一:
startrun(obj,attr,target,fn)
box.onmouseover = function(){
startrun(box,width,200,function(){
startrun(box,height,200,function(){
startrun(box,opacity,100)
});
});
}
如上面,函數(shù)也可以做為參數(shù)使用,就可以達(dá)到先執(zhí)行某個動作,再執(zhí)行某個動作的效果了。
要點(diǎn)二:
if(cur == target){
clearinterval(obj.timer);
if(fn){
fn();
}
}
當(dāng)運(yùn)動到達(dá)目標(biāo)點(diǎn),關(guān)閉定時器,然后就可以執(zhí)行新的函數(shù)了。
最后,上代碼:
<!doctype html>
<html>
<head>
<meta charset=gb2312 />
<title>無標(biāo)題文檔</title>
<style>
<!--
body{margin:0; padding:0; font:12px/1.5 arial;}
#box{width:100px; height:100px; position:absolute;
background:#06c; left:0;filter:alpha(opacity=30); opacity:0.3;}
-->
</style>
<script>
<!--
function getstyle(obj,name){
if(obj.currentstyle){
return obj.currentstyle[name];
}else{
return getcomputedstyle(obj,false)[name];
}
}
window.onload = function(){
var box = document.getelementbyid(box);
box.onmouseover = function(){
startrun(box,width,200,function(){
startrun(box,height,200,function(){
startrun(box,opacity,100)
});
});
}
box.onmouseout = function(){
startrun(box,height,100,function(){
startrun(box,width,100,function(){
startrun(box,opacity,30);
});
});
}
}
function startrun(obj,attr,target,fn){
clearinterval(obj.timer);
obj.timer = setinterval(function(){
var cur = 0;
if(attr == opacity){
cur = math.round(parsefloat(getstyle(obj,attr))*100);
}else{
cur = parseint(getstyle(obj,attr));
}
var speed = (target-cur)/8;
speed = speed>0?math.ceil(speed):math.floor(speed);
if(cur == target){
clearinterval(obj.timer);
if(fn){
fn();
}
}else{
if(attr == opacity){
obj.style.filter = alpha(opacity=+(cur+speed)+);
obj.style.opacity = (cur+speed)/100;
}else{
obj.style[attr] = cur + speed + px;
}
}
},30)
}
//-->
</script>
</head>
<body>
<div id=box>
</div>
</body>
</html>