简体   繁体   English

如何延迟动画

[英]How to delay animation

I have a jQuery animation which renders curtains opening and closing on the click function of .rope 我有一个jQuery动画,它在.rope的click函数上呈现打开和关闭窗帘

When the page loads the curtains automatically open after adding 页面加载后,添加后窗帘自动打开

$(".rope").trigger('click');

to the end of the string. 到字符串的末尾。

The full code works great but when the page loads, I want it to delay for a few seconds before the curtains open, but not on the click. 完整的代码很好用,但是当页面加载时,我希望它在窗帘打开之前延迟几秒钟,但不要单击。 I've tried adding .delay(2000) before .animate and before .ready, I'm toying around with a setTimeout but I',m not sure where to put it or if it's even what I need. 我尝试在.animate和.ready之前添加.delay(2000),我想使用setTimeout,但是我不确定将其放置在哪里,或者甚至不是我需要的。 My javascript skills is pretty basic. 我的JavaScript技能非常基础。 Here is what I'm using. 这是我正在使用的。 I just want to add the delay 我只想添加延迟

jQuery(document).ready(function($) {

            $curtainopen = false;

            $(".rope").click(function(){
                $(this).blur();
                if ($curtainopen == false){ 
                    $(this).stop().animate({top: '0px' }, {queue:false, duration:500, easing:'easeOutBounce'}); 
                    $(".leftcurtain").stop().animate({width:'60px'}, 2000 );
                    $(".rightcurtain").stop().animate({width:'60px'},2000 );
                    $curtainopen = true;
                }else{
                    $(this).stop().animate({top: '-40px' }, {queue:false, duration:500, easing:'easeOutBounce'}); 
                    $(".leftcurtain").stop().animate({width:'50%'}, 2000 );
                    $(".rightcurtain").stop().animate({width:'51%'}, 2000 );
                    $curtainopen = false;
                }
                return false;
            });
            $(".rope").trigger('click');
        });

A timeout would be ideal for this, I think. 我认为,超时将是理想的选择。

var autoOpen = setTimeout(function() {
    $(".rope").trigger("click");
},2000);

Be sure to add this just inside your $(".rope").click(...) function: 确保将其添加到$(".rope").click(...)函数中:

clearTimeout(autoOpen);

This will ensure that if the user clicks manually, then it cancels the "open automatically after two seconds" thing which might otherwise interfere ;) 这将确保如果用户手动单击,则将取消“两秒钟后自动打开”的操作,否则可能会干扰;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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