昨天在CodeAcademy开始学习JavaScript,并决定我实际上可以编写代码。开玩笑。在网上浏览,发现有类似学习曲线的人,但没有任何答案解决了我的问题。即使在等待调用函数时,setTimeout也不会等待
在HTML中我有4个div。 3从左边距600px。 1的左边距400px。
还有4个按钮。当我按下一个按钮时,我试图让对应的div在一秒钟内向左滑动,直到剩下:400px。
我以前有divs跳(没有采取几乎看起来像滑动的步骤)到正确的位置,但已经打破了代码。好像setTimeout没有等待规定的时间。我做了一些在线调查,并认为在setTimeout完成时,保证金已经持续下降到最终的安息地点。
<html>
<head><title></title>
<script type="text/javascript" language="javascript">
//<![CDATA[
var stopPositionVisible = 400;
var stopPositionHiding = 200;
function slideIn(xslide){
slidingDivInNow = document.getElementById(xslide);
if (parseInt(slidingDivInNow.style.left) > stopPositionVisible) {
slidingDivInNow.style.left = parseInt(slidingDivInNow.style.left) - 2 + "px";
console.log(slidinDivInNow.style.left);
}
setTimeout(slideIn(xslide), 20);
}
//]]>
</script>
</head>
<body>
<a id="div0link" href="#" onclick="slideIn('d1');">Home</a>
<a id="div1link" href="#" onclick="slideIn('d2');">page1</a>
<a id="div2link" href="#" onclick="slideIn('d3'); ">page2</a>
<a id="div3link" href="#" onclick="slideIn('d4'); ">page3</a>
<div id="d1" style="position:absolute; left:400px; top:50px; background:black; color:white; width:200px">horizontally sliding div</div>
<div id="d2" style="position:absolute; left:600px; top:60px; background:blue; color:white; width:200px">horizontally sliding div</div>
<div id="d3" style="position:absolute; left:600px; top:70px; background:red; color:white; width:200px">horizontally sliding div</div>
<div id="d4" style="position:absolute; left:600px; top:80px; background:green; color:white; width:200px">horizontally sliding div</div>
</body>
</html>
谢谢你的任何见解。
20毫秒几乎没有时间。不确定你的意思是“保证金已经持续下降”;该保证金不会自行下滑,你告诉它何时以及如何。 – 2011-12-24 16:38:46
http://jsfiddle.net/mjaric/xRzXk/ – 2011-12-24 16:50:41
谢谢@mjaric - 这是我最喜欢的网站 – 2011-12-24 17:27:29