moveRight(num) problems!
Hey there Stackoverflow! I'm pretty new at Javascript, but I'm getting the hang of it pretty quickly. I'm working on a website and I can't quite get it working.
First we have a button.
<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>
Next we have the code chunk that controls 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>
The movement is fine. The problem I have is with stopping it, which is what this snippet does
pp2.style.left = lft+"px";
if (lft > num) {
pp2.style.left = num;
When the num in (lft > num) and pp2.style.left = num is substituted with a real number, say 310, it works just fine. However when "num" is left in it does not stop. The reason I want to be able to substitute the stopping number is because I will have several buttons that will开发者_运维问答 make the bar stop at various places on the site. I've been working on it for several hours and I've done everything I could think of. Hopefully you guys can do better than me! :)
This is pretty close (barring editorial on coding style). On subsequent entries through your pseudo-recursion, there is no value for '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>
精彩评论