用js实现多个效果一起变化

效果如下,有时可能会多个效果一起变化。

 

 

 

 

 

 

要点一:

box.onmouseover = function(){
startrun(box,{width:200,height:300},function(){
startrun(box,{opacity:100})
})
}

通过json串可以传很多值,这样就可以同时改变很多属性了。

要点二:

遍历json串中的属性值对,找出所有的属性和对应的值,计算相应的数。

并设置一个标识位,当没达到目标值时,标识位设为false,当标识位为true时,可以关闭定时器,执行下一次的调用

 

最后,上代码

<!DOCTYPE html>
<html>
<head>
<meta charset="gb2312" />
<title>无标题文档</title>
<style>
<!--
body
{margin:0; padding:0; font:12px/1.5 arial;}
#box
{width:100px; height:100px; position:absolute; background:#06c; filter:alpha(opacity=30); opacity:0.3}
-->
</style>
<script>
<!--
window.onload
= function(){
var box = document.getElementById("box");
box.onmouseover
= function(){
startrun(box,{width:
200,height:300},function(){
startrun(box,{opacity:
100})
})
}
box.onmouseout
= function(){
startrun(box,{width:
100,height:100},function(){
startrun(box,{opacity:
30})
})
}
}

function getstyle(obj,name){
if(obj.currentStyle){
return obj.currentStyle[name];
}
else{
return getComputedStyle(obj,false)[name];
}
}

function startrun(obj,json,fn){
clearInterval(obj.timer);
obj.timer
= setInterval(function(){
var isall = true;
for(var attr in json){
var cur=0;
if(attr == "opacity"){
cur
= Math.round(parseFloat(getstyle(obj,attr))*100);
}
else{
cur
= parseInt(getstyle(obj,attr));
}

var speed = (json[attr] - cur)/8
speed = speed>0?Math.ceil(speed):Math.floor(speed);
if(cur != json[attr]){
isall
= false;
}
if(attr == "opacity"){
obj.style.filter
= "alpha(opacity="+(cur+speed)+")";
obj.style.opacity
= (cur+speed)/100;
}else{
obj.style[attr]
= cur+speed+"px";
}
}

if(isall){
clearInterval(obj.timer);
if(fn){
fn();
}
}

},
30);
}
//-->
</script>
</head>

<body>
<div id="box">
</div>
</body>
</html>



posted @ 2012-03-14 16:43  jingangel  阅读(514)  评论(0编辑  收藏  举报