jquery:如何延迟each()被触发

发布于 2024-12-01 16:46:34 字数 492 浏览 0 评论 0原文

如何延迟 each() 被触发?

这是延迟每个框在给定的特定时间淡出的代码。

$(document).ready(function(){

    var delay = 0;
    $('.block-item:lt(16)').each(function(){ 

        //^^ do for every instance less than the 16th (starting at 0)
        $(this).delay(delay).animate({
            opacity:0
        },500);
        delay += 500;

    });


});

但我想在触发 each() 之前延迟大约五秒。可行吗?

这是链接

How can I delay each() from being triggered?

This is the code that delays each box from fading out at certain time given.

$(document).ready(function(){

    var delay = 0;
    $('.block-item:lt(16)').each(function(){ 

        //^^ do for every instance less than the 16th (starting at 0)
        $(this).delay(delay).animate({
            opacity:0
        },500);
        delay += 500;

    });


});

But I want to delay about five second before the each() is triggered. Is it feasible?

Here is the link.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

清风不识月 2024-12-08 16:46:35

如果只是延迟初始动画的问题,为什么不从延迟 5000 开始呢?

http://jsfiddle.net/QAWTy/1/

$(document).ready(function(){

    var delay = 5000;
    $('.block-item:lt(16)').each(function(){ 

        //^^ do for every instance less than the 16th (starting at 0)
        $(this).delay(delay).animate({
            opacity:0
        },500);
        delay += 500;

    });


});

If it is just a matter of delaying the initial animation, why not just start with a 5000 delay?

http://jsfiddle.net/QAWTy/1/

$(document).ready(function(){

    var delay = 5000;
    $('.block-item:lt(16)').each(function(){ 

        //^^ do for every instance less than the 16th (starting at 0)
        $(this).delay(delay).animate({
            opacity:0
        },500);
        delay += 500;

    });


});
陈独秀 2024-12-08 16:46:35

是的,你可以,就像这样

$(document).ready(function(){

    var delay = 0;
    setTimeout(function() {
    $('.block-item:lt(16)').each(function(){ 

        //^^ do for every instance less than the 16th (starting at 0)
        $(this).delay(delay).animate({
            opacity:0
        },500);
        delay += 500;

    });

   }, 5000); 

});

Yes you can, like this

$(document).ready(function(){

    var delay = 0;
    setTimeout(function() {
    $('.block-item:lt(16)').each(function(){ 

        //^^ do for every instance less than the 16th (starting at 0)
        $(this).delay(delay).animate({
            opacity:0
        },500);
        delay += 500;

    });

   }, 5000); 

});
猛虎独行 2024-12-08 16:46:35

您的意思是在首次呼叫每个人之前等待 5 秒钟吗?如果是这样,请使用 setTimeout setTimeout 参考

现场演示

$(document).ready(function(){

    var delay = 0;
    // Wrap the function with setTimeout
    setTimeout(function(){
        $('.block-item:lt(16)').each(function(){ 

            //^^ do for every instance less than the 16th (starting at 0)
            $(this).delay(delay).animate({
                opacity:0
            },500);
            delay += 500;
        });
    }, 5000); // 5000 = 5 seconds


});

Do you mean wait 5 seconds just before the initial call to each? If so use setTimeout setTimeout Reference

Live Demo

$(document).ready(function(){

    var delay = 0;
    // Wrap the function with setTimeout
    setTimeout(function(){
        $('.block-item:lt(16)').each(function(){ 

            //^^ do for every instance less than the 16th (starting at 0)
            $(this).delay(delay).animate({
                opacity:0
            },500);
            delay += 500;
        });
    }, 5000); // 5000 = 5 seconds


});
提笔书几行 2024-12-08 16:46:35

您可以使用 setInterval 方法来实现此目的。

$(document).ready(function(){
    var count = 0;
    var $blockItems = $('.block-item:lt(16)');
    var timer;
    timer = setInterval(function(){
               if(count == 16){ 
                  clearInterval(timer);
                  return;
               }
               $blockItems.eq(count).animate({
                 opacity:0
               },500);
               count++;
            }, 500);
});

You can use setInterval method to achieve this.

$(document).ready(function(){
    var count = 0;
    var $blockItems = $('.block-item:lt(16)');
    var timer;
    timer = setInterval(function(){
               if(count == 16){ 
                  clearInterval(timer);
                  return;
               }
               $blockItems.eq(count).animate({
                 opacity:0
               },500);
               count++;
            }, 500);
});
十年不长 2024-12-08 16:46:35
$('.block-item:lt(16)').delay(delay).each(function(){  

        //^^ do for every instance less than the 16th (starting at 0) 
        $(this).animate({ 
            opacity:0 
        },500); 
        delay += 500; 

    }); 
)
$('.block-item:lt(16)').delay(delay).each(function(){  

        //^^ do for every instance less than the 16th (starting at 0) 
        $(this).animate({ 
            opacity:0 
        },500); 
        delay += 500; 

    }); 
)
滿滿的愛 2024-12-08 16:46:35

利用一些新功能http://jsfiddle.net/7czu4/

function HideItems(items, delay) {
    $(items[0]).fadeOut()
        .delay(delay)
        .promise()
        .done(function() {
            items.splice(0, 1);
            if (items.length > 0)
            {
                HideItems(items, delay);    
            }            
    });       
}

var items = $(".item");

HideItems(items, 5000);

Making use of some new features http://jsfiddle.net/7czu4/

function HideItems(items, delay) {
    $(items[0]).fadeOut()
        .delay(delay)
        .promise()
        .done(function() {
            items.splice(0, 1);
            if (items.length > 0)
            {
                HideItems(items, delay);    
            }            
    });       
}

var items = $(".item");

HideItems(items, 5000);
悲喜皆因你 2024-12-08 16:46:35

这是我专门为此目的制作的一个片段。
您可以调用 iniFadeChildren($('.parent'), 'li', 500)
而父母身上的所有的礼,都会一个接一个地消逝

function iniFadeChildren(pParent, pChildrenType, pDelay, pSpeed){
    pParent.find(pChildrenType).css({display:'none'});
    if(!pChildrenType){pChildrenType='*'} if(!pDelay){pDelay=200} if(!pSpeed){pSpeed=300}
    fadeChildren(pParent, pChildrenType, pDelay, 0, pParent.children(pChildrenType).length, pSpeed);
}

function fadeChildren(pParent, pChildrenType, pDelay, pNbr, pTotal, pSpeed){
    pParent.find(pChildrenType).eq(pNbr).fadeIn(pSpeed);
    pNbr++;
    if(pNbr!=pTotal){
        var command='fadeChildren('+pParent+', '+pChildrenType+', '+pDelay+', '+pNbr+', '+pTotal+')';
        t=setTimeout(function(){fadeChildren(pParent, pChildrenType, pDelay, pNbr, pTotal, pSpeed)}, pDelay);
    }
}

Here is a snippet I did specially for this purpose.
You can call iniFadeChildren($('.parent'), 'li', 500)
And all the li in parent are gonna fade one after another

function iniFadeChildren(pParent, pChildrenType, pDelay, pSpeed){
    pParent.find(pChildrenType).css({display:'none'});
    if(!pChildrenType){pChildrenType='*'} if(!pDelay){pDelay=200} if(!pSpeed){pSpeed=300}
    fadeChildren(pParent, pChildrenType, pDelay, 0, pParent.children(pChildrenType).length, pSpeed);
}

function fadeChildren(pParent, pChildrenType, pDelay, pNbr, pTotal, pSpeed){
    pParent.find(pChildrenType).eq(pNbr).fadeIn(pSpeed);
    pNbr++;
    if(pNbr!=pTotal){
        var command='fadeChildren('+pParent+', '+pChildrenType+', '+pDelay+', '+pNbr+', '+pTotal+')';
        t=setTimeout(function(){fadeChildren(pParent, pChildrenType, pDelay, pNbr, pTotal, pSpeed)}, pDelay);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文