这里面的变量flag怎么会变回true?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>无标题文档</title>

<style type="text/css">

*{margin:0;padding:0}

div{width:200px;height:200px;background:#9F0;}

</style>

</head>


<body>

<div id="div1"></div>

<script type="text/javascript">

window.onload=function(){

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

div1.onmouseover=function(){

startMove(div1,{'width':300,'opacity':30});

}

div1.onmouseout=function(){

startMove(div1,{'width':200,'opacity':100});

}

}

function startMove(obj,Json,fn){

var flag=true;

clearInterval(obj.timer);

obj.timer=setInterval(function(){

for(var attr in Json){

now=0;

if(attr=='opacity'){

now=parseFloat(getStyle(obj,attr))*100;

}

else{

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

}

speed=(Json[attr]-now)/10;

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

if(Json[attr]!=now){

flag=false;

}

if(attr=='opacity'){

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

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

}

   else{

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

}

if(flag){

clearInterval(obj.timer);

if(fn){

fn();

}

}

}},10)

}


function getStyle(obj,attr){

if(obj.currentStyle){

return obj.currentStyle[attr];

}

else{

return getComputedStyle(obj,false)[attr];

}

}

</script>

</body>

</html>


慕丝1360766
浏览 1962回答 1
1回答

李夜

if(Json[attr]!=now){ flag=false; }因为你声明flag时赋值true,后面只有在上述判断条件成立时才会将其改变为false
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript