Dengju
2016-11-16 19:33
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> *{ margin: 0px; padding: 0px; } ul,li{ list-style: none; } ul li{ width: 100px; height: 100px; background: #808002; margin-bottom: 10px; border: 4px solid #ccc; filter: alpha(opacity:50);//IE兼容 opacity: 0.5;//Chrome兼容 cursor: pointer; } </style> <script type="text/javascript"> window.onload=function(){ //载入滑动动效 var ddjli=document.getElementsByTagName("li"); for(var i=0;i<ddjli.length;i++){ ddjli[i].timer=null;//时间片私有化 ddjli[i].onmouseover=function(){ demo(this,"width",400,function(){ demo(this,"height",400); }); //demo(this,"height",400); //demo(this,"opacity",80); }; ddjli[i].onmouseout=function(){ demo(this,"width",100,function(){ demo(this,"height",100); }); //demo(this,"height",100); //demo(this,"opacity",50); }; } } //集成封装-缓冲拉长/透明度变幻/长宽动效 function demo(obj,attr,itarget,fn){ //先清除定时器,防止迭代 clearInterval(obj.timer); obj.timer=setInterval(function(){ //样式获取obj的attr属性 var icur=0; if(attr=="opacity"){ icur=Math.round(parseFloat(getStyle(obj,attr))*100); }else{ icur=parseInt(getStyle(obj,attr)); } //变加速,距离差越近速度越小,缓冲效果 var speed=(itarget-icur)/10; //速度取整 if(speed>0){ speed=Math.ceil(speed); }else{ speed=Math.floor(speed); } if(icur==itarget){ clearInterval(obj.timer); }else{ if(attr=="opacity"){ obj.style.filter="alpha(opacity:"+icur+speed+")"; obj.style.opacity=(icur+speed)/100; }else{ obj.style[attr]=icur+speed+"px"; } } if(fn){ fn(); } },30); } //样式BUG处理,解决offsetWidth漏洞问题 function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ return getComputedStyle(obj,false)[attr]; } } </script> </head> <body> <ul> <li>选项一</li> <li>选项二</li> <li>选项三</li> </ul> </body> </html>
第77行的fn位置放错了。当第一个任务完成后才会执行fu方法。把if(fn){} fn(); }放在69行
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{
margin: 0px;
padding: 0px;
}
ul,li{
list-style: none;
}
ul li{
width: 100px;
height: 100px;
background: #808002;
margin-bottom: 10px;
border: 4px solid #ccc;
filter: alpha(opacity:50);//IE兼容
opacity: 0.5;//Chrome兼容
cursor: pointer;
}
</style>
<script type="text/javascript">
window.onload=function(){
//载入滑动动效
var ddjli=document.getElementsByTagName("li");
for(var i=0;i<ddjli.length;i++){
ddjli[i].timer=null;//时间片私有化
ddjli[i].onmouseover=function(){
var g=this;
demo(this,{width:400,opacity:80},function(){
demo(g,{height:400});
});
//demo(this,"height",400);
//demo(this,"opacity",80);
};
ddjli[i].onmouseout=function(){
var g=this;
demo(this,{width:100,opacity:50},function(){
demo(g,{height:100});
});
//demo(this,"height",100);
//demo(this,"opacity",50);
};
}
}
//集成封装-缓冲拉长/透明度变幻/长宽动效
function demo(obj,json,fn){
//flag用于判断是否所有json都执行完毕
//先清除定时器,防止迭代
clearInterval(obj.timer);
obj.timer=setInterval(function(){
for(var attr in json){
//样式获取obj的attr属性
var icur=0;
var flag=true;
if(attr=="opacity"){
icur=Math.round(parseFloat(getStyle(obj,attr))*100);
}else{
icur=parseInt(getStyle(obj,attr));
}
//变加速,距离差越近速度越小,缓冲效果
var speed=(json[attr]-icur)/10;
//速度取整
if(speed>0){
speed=Math.ceil(speed);
}else{
speed=Math.floor(speed);
}
//检测停止
if(icur!=json[attr]){
flag=false;
if(attr=="opacity"){
obj.style.filter="alpha(opacity:"+icur+speed+")";
obj.style.opacity=(icur+speed)/100;
}else{
obj.style[attr]=icur+speed+"px";
}
}
if(flag){
clearInterval(obj.timer);
//链式回调
if(fn){
fn();
}
}
}
},30);
}
//样式BUG处理,解决offsetWidth漏洞问题
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return getComputedStyle(obj,false)[attr];
}
}
</script>
</head>
<body>
<ul>
<li>选项一</li>
<li>选项二</li>
<li>选项三</li>
</ul>
</body>
</html>
道理很简单。你把var flag=true;放在定时器外面flag就一直等于flase;
吧var flag=true放在定时器你就行了
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> *{ margin: 0px; padding: 0px; } ul,li{ list-style: none; } ul li{ width: 100px; height: 100px; background: #808002; margin-bottom: 10px; border: 4px solid #ccc; filter: alpha(opacity:50);//IE兼容 opacity: 0.5;//Chrome兼容 cursor: pointer; } </style> <script type="text/javascript"> window.onload=function(){ //载入滑动动效 var ddjli=document.getElementsByTagName("li"); for(var i=0;i<ddjli.length;i++){ ddjli[i].timer=null;//时间片私有化 ddjli[i].onmouseover=function(){ var g=this; demo(this,{width:400,opacity:80},function(){ demo(g,{height:400}); }); //demo(this,"height",400); //demo(this,"opacity",80); }; ddjli[i].onmouseout=function(){ var g=this; demo(this,{width:100,opacity:50},function(){ demo(g,{height:100}); }); //demo(this,"height",100); //demo(this,"opacity",50); }; } } //集成封装-缓冲拉长/透明度变幻/长宽动效 function demo(obj,json,fn){ //flag用于判断是否所有json都执行完毕 var flag=true; //先清除定时器,防止迭代 clearInterval(obj.timer); obj.timer=setInterval(function(){ for(var attr in json){ //样式获取obj的attr属性 var icur=0; if(attr=="opacity"){ icur=Math.round(parseFloat(getStyle(obj,attr))*100); }else{ icur=parseInt(getStyle(obj,attr)); } //变加速,距离差越近速度越小,缓冲效果 var speed=(json[attr]-icur)/10; //速度取整 if(speed>0){ speed=Math.ceil(speed); }else{ speed=Math.floor(speed); } //检测停止 if(icur!=json[attr]){ flag=false; if(attr=="opacity"){ obj.style.filter="alpha(opacity:"+icur+speed+")"; obj.style.opacity=(icur+speed)/100; }else{ obj.style[attr]=icur+speed+"px"; } } if(flag){ clearInterval(obj.timer); //链式回调 if(fn){ fn(); } } } },30); } //样式BUG处理,解决offsetWidth漏洞问题 function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ return getComputedStyle(obj,false)[attr]; } } </script> </head> <body> <ul> <li>选项一</li> <li>选项二</li> <li>选项三</li> </ul> </body> </html>
代码中有2个this,第二个this的指向已经跟第一个不一样了。用var g =this 代替。
var ddjli=document.getElementsByTagName("li");
for(var i=0;i<ddjli.length;i++){
ddjli[i].timer=null;//时间片私有化
ddjli[i].onmouseover=function(){
var g = this;
demo(this,“width”,400,function(){
demo(g,"height",400);
});
//demo(this,"height",400);
//demo(this,"opacity",80);
};
这样就行了
JS动画效果
113925 学习 · 1443 问题
相似问题