gpt4 book ai didi

javascript - moveRight(num) 问题!

转载 作者:行者123 更新时间:2023-12-02 20:17:10 27 4
gpt4 key购买 nike

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

Action 没问题。我遇到的问题是停止它,这就是这个代码片段的作用

 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>

关于javascript - moveRight(num) 问题!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6092069/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com