+ -
当前位置:首页 → 问答吧 → 有简单例子,如何处理这种情况?

有简单例子,如何处理这种情况?

时间:2010-05-13

来源:互联网


<!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> <title>Demo</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <style> *{margin:0;padding:0;font-size:12px;text-decoration:none} </style> <script> function go(num){ var box=document.getElementById("box"); var show=document.getElementById("show"); var t,i=0; t=setInterval(function(){ if(i>=num){ //box.style.left=num+"px"; clearInterval(t); }; i+=3; box.style.left=i+"px"; show.innerHTML=parseInt(box.style.left); },20) } window.onload=function(){ go(200); } </script> </head> <body> <div id="box" style="width:50px;height:50px;background:red;position:absolute;left:0;top:0"></div> <div id="show"></div> </body> </html>
 提示:您可以先修改部分代码再运行
有没有什么聪明点的办法??还请劳驾帮忙点化点化.

我要它不管步长i是多少,到200就给我停下,别多走!!

作者: hotcrab2008   发布时间: 2010-05-13

点化谈不上,就这样将就着用吧。
<!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> <title>Demo</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <style> *{margin:0;padding:0;font-size:12px;text-decoration:none} </style> <script> function go(num){ var box=document.getElementById("box"); var show=document.getElementById("show"); var t,i=0; t=setInterval(function(){ if(i>=num){ clearInterval(t); box.style.left = num + "px"; return; }; i+=3; box.style.left=i+"px"; i<=num ? show.innerHTML=parseInt(box.style.left) : show.innerHTML=num; },20) } window.onload=function(){ go(200); } </script> </head> <body> <div id="box" style="width:50px;height:50px;background:red;position:absolute;left:0;top:0"></div> <div id="show"></div> </body> </html>
 提示:您可以先修改部分代码再运行

作者: chenjmdgjl   发布时间: 2010-05-13