为什么我用了 var that,照样不能链式动画?

来源:5-1 JS链式动画

慕无忌4172087

2020-03-25 17:00

<!DOCTYPE html>

<html>

<head>

<title>链式动画</title>

<style type="text/css">

div{

background: yellow;

border:2px solid red;

width: 120px;

height: 50px;

margin-bottom: 30px;

}

</style>

</head>

<body>

<div></div>

<div></div>

<div></div>

<script type="text/javascript">

window.onload = function(){

var div1 =document.getElementsByTagName('div');

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

div1[i].timer = null;

div1[i].onmouseover = function(){

var that = this;

start(this,480,'width',function(){

start(that,150,'height');

});

}

div1[i].onmouseout = function(){

var that = this;

start(this,120,'width',function(){

start(that,50,'height');

});

}

}

}

//fn = function(){start(...,...,...)}


function start(obj,itarget,attr,fn){

clearInterval(obj.timer);

obj.timer = setInterval(function(){

if (offattr!= itarget) {

var offattr = parseInt(getStyle(obj,attr));

speed = (itarget - offattr)/20;

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

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

}

else{

clearInterval(obj.timer);

if (fn) {

fn;

}

}

},30)

}


function getStyle(obj,attr){

if (obj.currentStyle) {

return obj.currentStyle[attr];

}

else{

return getComputedStyle(obj,false)[attr];

}

}

</script>

</body>

</html>

如上代码只能改变宽度,高度无法改变,请问是什么原因?

写回答 关注

1回答

  • 慕无忌4172087
    2020-03-25 17:02:05

    fn()改了   还是没用

JS动画效果

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

113925 学习 · 1443 问题

查看课程

相似问题