使delay()与插件一起工作

Making delay() work with a plugin

本文关键字:插件 一起 工作 delay      更新时间:2023-09-26

我有一个小插件,用于根据几个可变因素设置元素的高度。在页面加载时,我想要一个延迟,以防止在几秒钟内设置高度。我知道延迟只对效果有效,有没有办法把我的插件注册为效果,或者我需要用另一种方式吗?

所需发射代码:

$(".app_advert").delay(10000).set_height(2000);

插件代码:

       //SetHeight plugin
    (function($){
        $.fn.extend({
            set_height: function(time, callback){
                    scroll = $(window).scrollTop();
                    win_height = $(window).height();
                    document_height = $(document).height();

                    footer_height = $('#footer').height();
                    footer_offset = (document_height-(win_height+scroll));
                    footer_remainder = footer_height-footer_offset;
                return this.each(function(){
                    var x = $(this);
                    if(footer_height-footer_offset<=0){
                        height = 0;
                    } else {
                        height = footer_height-footer_offset;
                    }
                    $(x).stop(true, false).animate({bottom: height}, time, function(){
                        if (callback && typeof callback == 'function') { // make sure the callback is a function
                                callback.call(this); // brings the scope to the callback
                            }
                    });
                });
            }
        });
})(jQuery);

您需要手动调用jQuerys .queue()。可能看起来像;

$(".app_advert").delay(10000).queue(function() {
    $(this).set_height(2000);
    $(this).dequeue();
});

您甚至可以在后面的构造上链接更多排队的方法。请注意,默认情况下,所有fx methods都会排队等待您要应用于当前包装集的任何其他方法,调用另一个.queue()

阅读:http://api.jquery.com/queue

在插件中使用setTimeout如何。或者更好的是使用setTimeout编写自己的延迟插件,使用一个伪方法,这样你就可以链接你的事件。