以一段时间间隔逐个隐藏图像集合

时间:2013-10-18 13:07:07

标签: jquery

我在div块中收集了图像

我想以一段时间逐个隐藏它们。

我试过这个剧本。

$(document).ready(function () {
    delayInSeconds = 1;
    i = 0;
    setInterval(function () {
        while (i < 10) {
            $(".images").eq(i).hide();
            i++;
        }
    }, delayInSeconds * 50);
});

3 个答案:

答案 0 :(得分:0)

这是因为第一次迭代开始时i的值为10。

解决问题的一种方法是:

...
function hideOne(i) {
    $(".images").eq(i).hide();
    if($(".images").eq(i+1).length > 0) { // or change to if(i+1 < 10) if you're sure
        setTimeout(function() { 
            hideOne(i+1);
        }, delayInSeconds * 1000); // given the variable name, the multiplier should be 1000, i believe
    }
}

hideOne(0); // start it
...

答案 1 :(得分:0)

$(document).ready(function () {
 $(".images").each(function(){
  setTimeOut($(this).hide(),300);
 });
});     

这应该这样做。 希望这会对你有所帮助

答案 2 :(得分:0)

这样做。它使用:visible选择器来丢弃隐藏的图像,:eq(0)选择器从匹配集中获取单个元素。

Demo

var delayInSeconds = 1;

function hide(){
    setTimeout(function(){
        var images = $('.images:visible:eq(0)');
        if(images.length == 1){
            images.hide();
            hide();
        }

    }, 1000 * delayInSeconds);
}

$(document).ready(function(){
    hide();
});