简体   繁体   中英

why javascript onclick div slide not working?

can you tell me why this JavaScript not working .

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>
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);
    }
}

Alternatively, use jQuery to do this.

function expand()
{
    $("#expandDiv").slideDown(2000, function() {
        // Afterwards
    });
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM