为什么透明明值不为整数1而是0.83?我找不到原因

来源:7-1 JS动画案例

情积雪

2016-05-22 23:51

<script type="text/javascript">

window.onload = function(){

var content = document.getElementsByClassName('content')[0];

var A = document.getElementsByTagName('a');

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

A[i].onmouseenter = function(){

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

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

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

move(_this,{top:20,opacity:100},function(){

//_this.style.opacity = 1;

});

});

}

}

}

function move(obj,json,fu){

clearInterval(obj.time);

obj.time = setInterval(function(){

for(var attr in json){

var flag = true;

var icur = 0;

if(attr == 'opacity'){

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

}else{

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

}

var speed = 0;

speed = (json[attr] - icur)/8;

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

if(icur != json[attr]){

flag = false;

}

if(attr == 'opacity'){

obj.style[attr] =(icur+speed)/100;

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

}else{

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

}

if(flag){

clearInterval(obj.time);

if(fu){

fu();

}

}

}

},10);

}

function getStyle(obj,attr){

   if(obj.currentStyle){

    return obj.currentStyle[attr];

   }else{

    return getComputedStyle(obj,false)[attr];

   }

}

</script>


写回答 关注

3回答

  • 慕粉1703136593
    2017-03-12 22:55:58

    终于找到原因了,可是想不太懂为什么?

  • 哇妙012
    2016-09-23 16:54:29

    我也是这个问题,什么原因啊

    情积雪

    有答案了

    2016-09-24 10:11:15

    共 1 条回复 >

  • 情积雪
    2016-05-23 16:49:09

    if(flag){

    clearInterval(obj.time);

    if(fu){

    fu();

    }

    要放到for(var attr in json){}之外。自己能找到原因真是太好了

    情积雪 回复背着灯笼

    迷迷糊糊的,哈哈

    2016-05-24 22:13:39

    共 3 条回复 >

JS动画效果

通过本课程JS动画的学习,从简单动画开始,逐步深入各种动画框架封装

113923 学习 · 1443 问题

查看课程

相似问题