问答详情
源自:7-1 JS动画案例

求教:为什么透明度最终回不到1了啊啊啊啊啊???

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>js动画案例</title>

<style type="text/css">

*{

 margin: 0;

 padding: 0;

}

#div1{

padding: 10px;

width: 300px;

background-color: #f4f4f4;

margin: 10px auto;

border: 1px solid #ccc;

}

#div1 a{

display: inline-block;

width: 58px;

height: 25px;

border: 1px solid #ddd;

border-radius: 3px;

background-color: #fff;

text-align: center;

margin: 10px 17px;

position: relative;

padding-top: 40px;

color: #9c9c9c;

font-size: 12px;

text-decoration: none;

line-height: 25px;

overflow: hidden;

}

#div1 a i{

position: absolute;

top: 20px;

left: 0px;

display: inline-block;

width: 100%;

text-align: center;

filter:alpha(opacity=100);

opacity: 1;

}

#div1 a:hover{

color: #f00;

}


    </style>

    <script src="move.js"></script>

    <script type="text/javascript">

       window.onload=function(){

        var oDiv= document.getElementById('div1');

        var aList= oDiv.getElementsByTagName('a');

        for(i=0; i<aList.length; i++){

        aList[i].onmouseover= function(){

        var _this= this.getElementsByTagName('i')[0];

        startMove(_this,{top:-25,opacity:0},function(){

        _this.style.top= 30+'px';

        startMove(_this,{top:20,opacity:100});

        })

        }

        }

       }

    </script>


</head>

<body>

<div id='div1'>

<a href="#"><i><img src="baobei.png"></i><p>宝贝</p></a>

<a href="#"><i><img src="dianpu.png"></i><p>店铺</p></a>

<a href="#"><i><img src="sheying.png"></i><p>摄影</p></a>

<a href="#"><i><img src="wenzhang.png"></i><p>文章</p></a>

<a href="#"><i><img src="cafei.png"></i><p>咖啡</p></a>

<a href="#"><i><img src="tupian.png"></i><p>图片</p></a>


</div>

</body>

</html>

下面是运动框架:

// function startMove(obj,attr,iTarget,fn) {

function startMove(obj,json,fn) { 

  

  clearInterval(obj.timer);

  obj.timer= setInterval(function(){

  for(var attr in json){

  //1.取当前的值

  var incur=0;

  var flag= true;

  if(attr=='opacity'){

     incur=Math.round(parseFloat(getStyle(obj,attr))*100); 

  }else{

     incur=parseInt(getStyle(obj,attr));

  }

  //2.算速度

  var speed= (json[attr]-incur)/15;

  speed= speed>0?Math.ceil(speed):Math.floor(speed);

  //3.检测停止

  if(incur!=json[attr]){

    flag= false; 

    if(attr=='opacity'){

       obj.style.filter='alpha(opacity:'+(incur+speed)+')';

       obj.style.opacity=(incur+speed)/100;

    }else{

       obj.style[attr]=incur+speed+'px';

      }

  }

  if(flag){

    clearInterval(obj.timer);

    if(fn){

      fn();

    }

  }

  }

  },20)  

}



function getStyle(obj,attr){

  if(obj.currentStyle){

  return obj.currentStyle[attr];//IE

  }else{

  return getComputedStyle(obj,false)[attr];//firefox

  }

}


提问者:逃离星球 2016-04-10 11:44

个回答

  • 一颗猕猴桃
    2016-06-08 21:14:47

    clearInterval(obj.timer);

      obj.timer= setInterval(function(){

      for(var attr in json){     // for的花括号扩到我下面备注的地方

      //1.取当前的值

      var incur=0;

      var flag= true;

      if(attr=='opacity'){

         incur=Math.round(parseFloat(getStyle(obj,attr))*100); 

      }else{

         incur=parseInt(getStyle(obj,attr));

      }

      //2.算速度

      var speed= (json[attr]-incur)/15;

      speed= speed>0?Math.ceil(speed):Math.floor(speed);

      //3.检测停止

      if(incur!=json[attr]){

        flag= false; 

        if(attr=='opacity'){

           obj.style.filter='alpha(opacity:'+(incur+speed)+')';

           obj.style.opacity=(incur+speed)/100;

        }else{

           obj.style[attr]=incur+speed+'px';

          }

      }         

    }                               //花括号应该扩到这

      if(flag){

        clearInterval(obj.timer);

        if(fn){

          fn();

        }

      }

      }   //这个花括号去掉

      },20)  

    }

    这样应该就没问题了

  • Object_is_null
    2016-04-14 15:10:03

    你的运动框架代码有问题

    //改正后的move.js

    function startMove(obj,json,fn) { 

      clearInterval(obj.timer);

      var flag;

      obj.timer= setInterval(function(){

      flag= true;

      for(var attr in json){

      //1.取当前的值

      var incur=0;

      if(attr=='opacity'){

         incur=Math.round(parseFloat(getStyle(obj,attr))*100); 

      }else{

         incur=parseInt(getStyle(obj,attr));

      }

      //2.算速度

      var speed= (json[attr]-incur)/15;

      speed= speed>0?Math.ceil(speed):Math.floor(speed);

      //3.检测停止

      if(incur!=json[attr]){

        flag= false; 

      }

      if(attr=='opacity'){

           obj.style.filter='alpha(opacity:'+(incur+speed)+')';

           obj.style.opacity=(incur+speed)/100;

        }else{

           obj.style[attr]=incur+speed+'px';

          }

      }

      if(flag){

        clearInterval(obj.timer);

        if(fn){

          fn();

        }

      }

      },20)  

    }



    function getStyle(obj,attr){

      if(obj.currentStyle){

      return obj.currentStyle[attr];//IE

      }else{

      return getComputedStyle(obj,false)[attr];//firefox

      }

    }

    ps:用这个代码试试