你能告诉我为什么这个JavaScript无效。
function expand()
{
var slidingDiv = document.getElementById("expandDiv");
var startPosition = 350;
var stopPosition = -150;
if ((parseInt(slidingDiv.style.top) > stopPosition )&&(parseInt(slidingDiv.style.top) < startPosition ))
{
slidingDiv.style.top = parseInt(slidingDiv.style.top) + 5 + "px";
setTimeout(expand, 5);
}
}
.......
<a onclick="expand();">Expand</a>
<div id="expandDiv" style="width:300;height:100;background-color:#fff;position:absolute;border:1px solid #ccc;">hello<br />great testing</div>
答案 0 :(得分:3)
function expand()
{
var slidingDiv = document.getElementById("expandDiv");
var startPosition = 350;
var stopPosition = -150;
var value = parseInt(slidingDiv.style.top.replace("px", ""));
if (value > stopPosition && value < startPosition)
{
slidingDiv.style.top = (value + 5).toString() + "px";
setTimeout(expand, 5);
}
}
或者,使用jQuery执行此操作。
function expand()
{
$("#expandDiv").slideDown(2000, function() {
// Afterwards
});
}