下面的代碼用於在另一個隱藏的溢出div(未來網站的概念)中滑動div。 「下一個」和「前一個」鏈接下方的文本框用於指示x值。 使用IE8一切正常。Javascript setTimeout-recursion不適用於Chrome和Firefox
有了Chrome和Firefox,它似乎只能執行一次(一步)「下一個」和「上一個」功能,這意味着setTimeout不會運行。
<html>
<meta http-equiv="Content-Script-Type" content="text/javascript">
<head>
<script type="text/javascript">
var ie5 = (document.all && document.getElementById);
var ns6 = (!document.all && document.getElementById);
function next(startx, fadeStep, end){
if(startx > end){
startx -= Math.round((1/Math.pow(fadeStep,0.6)));
if (startx < end) startx = end;
document.getElementById('lastname').value = startx + "px";
document.getElementById('test1').style.left = startx + "px";
fadeStep += 0.00035;
args = "next(" + startx + "," + fadeStep + "," + end + ")";
setTimeout(args, 20);
}
}
function prev(startx, fadeStep, end){
if(startx < end){
startx += Math.round((1/Math.pow(fadeStep,0.6)));
if (startx > end) startx = end;
document.getElementById('lastname').value = startx + "px";
document.getElementById('test1').style.left = startx + "px";
setTimeout('prev(' + startx + ',' + (fadeStep+0.00035) + ',' + end + ')', 20);
}
}
</script>
</head>
<body>
<div style="position:relative;width:570;height:78;overflow:hidden">
<div id="test1" style="position:absolute;left:0px;top:0px;width:1180;height:78">
<img src="http://img38.imageshack.us/img38/7225/imageva.png" border=1>
<img src="http://img577.imageshack.us/img577/5554/image2te.png" border=1>
</div>
</div>
<div style="position:absolute;left:900px;top:0px;width:800">
<a href="#" onclick="next(0, 0.001, -570)">Next</a><br />
<a href="#" onclick="prev(-570, 0.001, 0)">Previous</a></ br>
<form><input type="text" id="lastname" value="gfgfg"/></form>
</div>
</body>
</html>
您能否協助?
我認爲你的代碼會大大簡化,如果你使用類似jQuery的東西。 – cambraca 2012-01-27 20:30:44
你嘗試傳遞實際的函數而不是字符串setTimeout? 'setTimeout(function(){next(startX,fadeStep,end);},20)' – hugomg 2012-01-27 20:35:43
我對這兩種瀏覽器都沒有問題...... – JayC 2012-01-27 21:32:14