moveRight(num)问题!

时间:2011-05-23 01:47:52

标签: javascript

嘿,有Stackoverflow!我是Javascript的新手,但我很快就掌握了它。我正在网站上工作,但我无法让它发挥作用。

首先我们有一个按钮。

<SPAN ID="button2">
<A HREF="#" 
onClick="changeZIndex(1,'contactus');changeZIndex(0,'aboutus');changeZIndex(0,'pagethree');moveRight(310)">
<IMG NAME="two" SRC="contactus.gif"></A>
</SPAN>

接下来我们有控制moveRight的代码块

<script type="text/javascript">

var userWidth = window.screen.width;
function moveRight(num) {
var pp2 = document.getElementById("bar");
var lft = parseInt(pp2.style.left);
var tim = setTimeout("moveRight()",10); 
lft = lft+5; 
pp2.style.left = lft+"px";
if (lft > num) {  
pp2.style.left = num;  
clearTimeout(tim);
}
}
</script>

运动很好。我遇到的问题是停止它,这就是这个代码片段的作用

 pp2.style.left = lft+"px";
 if (lft > num) {  
 pp2.style.left = num;

当num in(lft&gt; num)和pp2.style.left = num用实数代替310时,它可以正常工作。但是当留下“num”时它不会停止。我希望能够替换停止号码的原因是因为我将有几个按钮,使栏杆停在网站的不同位置。我已经工作了几个小时,我已经做了我能想到的一切。希望你们能做得比我好! :)

1 个答案:

答案 0 :(得分:2)

这非常接近(除了编码风格的社论)。在通过伪递归的后续条目中,'num'没有值。

<script type="text/javascript">
var userWidth = window.screen.width;
function moveRight(num) 
{
   var pp2 = document.getElementById("bar");
   var lft = parseInt(pp2.style.left);

   // you have give the next iteration of 'moveRight' the value for num.
   var tim = setTimeout("moveRight("+ num +")",10);

   lft = lft+5; 
   pp2.style.left = lft+"px";
   if (lft > num) 
   {  
      // here, you have to make sure that you're adding the unit qualifier (px)
      //  to your style.
      pp2.style.left = num + 'px';  

      clearTimeout(tim);
   }
}
</script>