js实现运动的框架实例

html代码:鼠标移入时多物体实现高宽同时变化接着改变透明度

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="t.css">
<script type="text/javascript" src="jstest.js"></script>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
</ul>
</body>
</html>

js代码:

window.onload=function(){
var test1=document.getElementsByTagName('li');
for(var i=0;i<test1.length;i++)
{
test1[i].timer=null;
test1[i].alpha=30;
//test1[i].speed=0;
test1[i].onmouseover=function(){
var g=this;
getmove(this,{height:100,width:400},function(){
getmove(g,{opacity:100})
});
};
test1[i].onmouseout=function(){
var g=this;
getmove(this,{opacity:30},function(){
getmove(g,{height:70,width:300})
});
};
}
};
//a1是目标值,obj为对象,attrr是需要的属性,fn回调函数

function getmove(obj,json,fn){
clearInterval(obj.timer);
obj.timer=setInterval(function(){
var flag=1;
//jason循环
for(var attrr in json){
//获取当前样式值
if(attrr=='opacity'){
icur=Math.round(parseFloat(getStyle(obj,attrr))*100);
}else{
icur=parseInt(getStyle(obj,attrr));
}
//计算速度
var speed=(json[attrr]-icur)/10;
speed=speed>0? Math.ceil(speed):Math.floor(speed);
//判断是否全部达到目标值
if(icur!=json[attrr]){
flag=0;
}
//计算结果
if(attrr=='opacity')
{
obj.style.filter='alpha(opacity:'+(icur+speed)+')';
obj.style.opacity=(icur+speed)/100;
}
else{
obj.style[attrr]=icur+speed+'px';
}
}
//判断是否的关闭定时器
if(flag){
clearInterval(obj.timer);
if(fn){
fn();
}

}
},30);}
//获取当前样式值的函数表达式
function getStyle(obj,attrr){
if(obj.currentStyle){
return obj.currentStyle[attrr];
}else{
return getComputedStyle(obj,false)[attrr];
}
}

posted @ 2016-08-20 12:43  jjooee  阅读(364)  评论(0编辑  收藏  举报