moveRight(num) problems!

moveRight(num) problems!

本文关键字:problems num moveRight      更新时间:2023-09-26

嘿,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;

当(lft>num)和pp2.style.left=num中的num被一个实数(比如310)取代时,它就可以正常工作。但是,当"num"保留时,它不会停止。我之所以想用停止号来代替,是因为我有几个按钮,可以让酒吧在网站上的不同地方停止。我已经做了好几个小时了,我已经把我能想到的都做了。希望你们能比我做得更好!:)

这非常接近(除非对编码风格进行编辑)。在通过伪递归的后续条目中,"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>