加载时折叠菜单

时间:2015-03-16 12:26:46

标签: jquery

我试过制作一些JQuery。单击“动画高度”按钮时,框会折叠。但是如果我想摆脱按钮,那么它将在fx 1秒后自动折叠,我将如何做到这一点?我猜我必须在某处使用onLoad,或者我是否需要重写整个代码?

最诚挚的问候 MADS

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script>
$(document).ready(function(){
    $("#btn1").click(function(){
        $("#box").animate({height: "300px" });
        $("#box").animate({width: "300px" });
    });

    $("#btn2").click(function(){
        $("#box").animate({height: "100px"});
        $("#box").animate({width: "100px" });
    });
});
</script>
</head>
<body>

<button id="btn1">Animate height</button>

<button id="btn2">Reset height</button>

<div id="box" style="background:#98bf21;height:100px;width:100px;margin:6px;"></div>

</body>
</html>

解决方案:

<script>
$(document).ready(function(){
    setTimeout(function () {
        $("#box").animate({height: "300px"});
        $("#box").animate({width: "300px"});
    }, 100); // the 1000 is the delay in milliseconds
});
</script>

3 个答案:

答案 0 :(得分:1)

你可以设置超时来触发开场动画,例如:

$(document).ready(function(){
    setTimeout(function () {
        $("#box").animate({height: "300px",  width: "300px"});
    }, 1000); // the 1000 is the delay in milliseconds
});

答案 1 :(得分:0)

您可以尝试这样: -

$.animate(action1).delay(n).animate(action2)

其中n是延迟毫秒

$(document).ready(function(){
            $("#box").delay(1000).animate({ height: "300px", width: "300px" }).delay(1000).animate({ height: "100px", width: "100px" });
        });

答案 2 :(得分:0)

您可以使用setTimeout作为动画,但也可以使用transition css属性。 atmd 为您提供了setTimeout的示例,我将为您提供transition的示例。试试这个:

<!DOCTYPE html>
<html>
<head>
<style> 
div {
    width: 100px;
    height: 100px;
    background: red;
    -webkit-transition: width 2s; /* For Safari 3.1 to 6.0 */
    transition: width 2s;
}

div:hover {
    width: 300px;
}
</style>
</head>
<body>

<p><b>Note:</b> This example does not work in Internet Explorer 9 and earlier versions.</p>

<div></div>

<p>Hover over the div element above, to see the transition effect.</p>

</body>
</html>

取自here