javascript javascript滚动div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7256820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript scroll div
提问by u894345
setInterval
in this code doesn't work properly
setInterval
在此代码中无法正常工作
<script language="javascript">
x = y = 0;
function mvtb(p, g){
i = 0;
tmp = setInterval(function (){
if(i < 100 && i >= 0){
document.getElementById("plan").scrollTop = 200*(y+((y-g)*(i/100)));
document.getElementById("plan").scrollLeft = 660*(x+((x-p)*(i/100)));
i++;
}
else if(i == 100){
clearInterval(tmp);
}
}, 10);
x = p; y = g;
}
</script>
CSS
CSS
<style>
#plan {overflow: hidden; height: 200px; width: 660px; background: #444;}
.plan {width: 1980px;}
.plan table {height: 200px; width: 660px;}
</style>
HTML
HTML
<body style="background: black; color: white;">
<center>
<div id="plan">
<table class="plan">
<tr id="g0">
<td class="p0"><table bgcolor="gray"><tr><td>0 0</td></tr></table></td>
<td class="p1"><table bgcolor="blue"><tr><td>1 0</td></tr></table></td>
<td class="p2"><table bgcolor="yellow"><tr><td>2 0</td></tr></table></td>
</tr>
<tr id="g1">
<td class="p0"><table bgcolor="green"><tr><td>0 1</td></tr></table></td>
<td class="p1"><table bgcolor="orange"><tr><td>1 1</td></tr></table></td>
<td class="p2"><table bgcolor="red"><tr><td>2 1</td></tr></table></td>
</tr>
<tr id="g2">
<td class="p0"><table bgcolor="brown"><tr><td>0 2</td></tr></table></td>
<td class="p1"><table bgcolor="white"><tr><td style="color: black">1 2</td></tr></table></td>
<td class="p2"><table bgcolor="crimson"><tr><td>2 2</td></tr></table></td>
</tr>
</table>
</div>
<br /><br /><br />Move Table<br />
<button onClick="mvtb(0, 0);">0 0</button> <button onClick="mvtb(1, 0);">1 0</button> <button onClick="mvtb(2, 0);">2 0</button><br />
<button onClick="mvtb(0, 1);">0 1</button> <button onClick="mvtb(1, 1);">1 1</button> <button onClick="mvtb(2, 1);">2 1</button><br />
<button onClick="mvtb(0, 2);">0 2</button> <button onClick="mvtb(1, 2);">1 2</button> <button onClick="mvtb(2, 2);">2 2</button><br />
</center>
</body>
回答by Dennis
Try this instead. Only update g and p after the animation is finished and swap the subtraction when calculating the new scrollTop/scrollLeft. You also need to make sure the loop runs 100 times, not 99. JSFiddle
试试这个。仅在动画完成后更新 g 和 p,并在计算新的 scrollTop/scrollLeft 时交换减法。您还需要确保循环运行 100 次,而不是 99 次 。JSFiddle
x = y = 0;
function mvtb(p, g) {
var i = 1;
var tmp = setInterval(function() {
if (i <= 100 ) {
document.getElementById("plan").scrollTop = 200 * (y + ((g - y) * (i / 100)));
document.getElementById("plan").scrollLeft = 660 * (x + ((p - x) * (i / 100)));
i++;
}
else {
clearInterval(tmp);
x = p;
y = g;
}
}, 10);
}
回答by Hogan
Use the var statement to get closure to work right.
Check for >=
.
使用 var 语句使闭包正常工作。检查>=
.
(i
is a really bad variable name and you probably use it elsewhere and reset it in the global context.)
(这i
是一个非常糟糕的变量名,您可能在其他地方使用它并在全局上下文中重置它。)
There is another problem with your x, y, p and g. But I'm not sure what those values are used for... I think you might need another two variables but I'm not sure.
您的 x、y、p 和 g 存在另一个问题。但我不确定这些值的用途……我想您可能还需要另外两个变量,但我不确定。
<script language="javascript">
var x = y = 0;
function mvtb(p, g){
var i = 0;
tmp = setInterval(function (){
if(i < 100 && i >= 0){
document.getElementById("plan").scrollTop = 200*(y+((y-g)*(i/100)));
document.getElementById("plan").scrollLeft = 660*(x+((x-p)*(i/100)));
i++;
}
else if(i >= 100){
clearInterval(tmp);
}
}, 10);
x = p;
y = g;
}
</script>