Jquery 逐个淡入淡出元素,如果超过限制则删除最后一个元素

发布于 2024-12-27 12:21:38 字数 1301 浏览 2 评论 0原文

问题 1: 我想淡化每个 href 或 img 标签,因为它们加载时有延迟,所以看起来好像它们是单独加载的。我不知道该使用哪个, delay()fadeToggle()fadeIn() `

目前我使用代码得到了所有这些下面,将一次性显示一次,每 20 秒一次。任何重复的图像都不会显示,因为它们已经加载到数组中。

 $(document).ready(function(){
        var existingElements= new Array(); // array of existing images

        setInterval(function(){
            $.get("/helloworld", function(data){
                data = $.parseJSON(data);

                for(i = 0; i < data.length; i++){
                    if($.inArray(data[i]["id"], existingElements) == -1){



                       var imagelinks = '<a class="removethis" href="link">'
                       +'<img src="'+data[i]["img"]+'"/>   </a>';

                       $(imagelinks).prependTo("#somediv").delay(300);


                       existingElements.push(data[i]["id"]);


                        }

 /// add limit here, see below
                    }
                });
            }, 20000);
        });

问题 2: div somediv 最多只能包含 20 张图像。所以如果json传入的图片超过20张(超过)。我想删除最后设置的图像,因为上面的代码总是会在 20 秒后引入新图像,我应该设置如下限制:

 if(i > 20){
$("#somediv .removethis:last").fadeOut(); // should i use remove()

}

Question 1:
I want to fade each href or img tag as they are loaded with delay, so it looks as if they are loaded separately. I dont know which to use, delay() , fadeToggle() or fadeIn() `

At moment i get all of them using the code below, which will display in one go, every 20 second. any duplicate images are not shown as they would already be loaded in the array.

 $(document).ready(function(){
        var existingElements= new Array(); // array of existing images

        setInterval(function(){
            $.get("/helloworld", function(data){
                data = $.parseJSON(data);

                for(i = 0; i < data.length; i++){
                    if($.inArray(data[i]["id"], existingElements) == -1){



                       var imagelinks = '<a class="removethis" href="link">'
                       +'<img src="'+data[i]["img"]+'"/>   </a>';

                       $(imagelinks).prependTo("#somediv").delay(300);


                       existingElements.push(data[i]["id"]);


                        }

 /// add limit here, see below
                    }
                });
            }, 20000);
        });

Question 2:
The div somediv can only have at max of 20 images. so if more than 20(exceed) images are brought in by json. i want to remove that last set images, since the above code will always bring in new images after 20 seconds, should i set a limit as follow:

 if(i > 20){
$("#somediv .removethis:last").fadeOut(); // should i use remove()

}

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

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

发布评论

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

评论(1

孤蝉 2025-01-03 12:21:38

像这样的事情应该做:

var existingElements = {};
var somediv = $("#somediv");

(function loopy() {
    $.get("/helloworld", function(data) {
        data = $.parseJSON(data);
        var imagelinks = [];
        for (i = 0; i < data.length; i++) {
            if (!existingElements[data[i]["id"]]) {
                imagelinks.push('<a class="removethis" href="link">' + '<img src="' + data[i]["img"] + '"/></a>');
                existingElements[data[i]["id"]] = data;
            }
        }
        // Add then all to the dom at once, hide them
        //then find that last one and start fading in.
        $(imagelinks.join('')).prependTo(somediv).hide().filter(":last").fadeIn("slow", fadeNext);
        setTimeout(loopy, 20000);
    });
})();

function fadeNext() {
    // Fade the next image in
    $(this).prev().fadeIn(fadeNext);
    //If there are more than 20 images visible fade the others out
    var more = somediv.find("img:visible:not(:animated):gt(20)");
    if (more.length) {
        more.fadeOut("slow", function() {
            $(this).remove();
        });
    }
}

Something like this should do:

var existingElements = {};
var somediv = $("#somediv");

(function loopy() {
    $.get("/helloworld", function(data) {
        data = $.parseJSON(data);
        var imagelinks = [];
        for (i = 0; i < data.length; i++) {
            if (!existingElements[data[i]["id"]]) {
                imagelinks.push('<a class="removethis" href="link">' + '<img src="' + data[i]["img"] + '"/></a>');
                existingElements[data[i]["id"]] = data;
            }
        }
        // Add then all to the dom at once, hide them
        //then find that last one and start fading in.
        $(imagelinks.join('')).prependTo(somediv).hide().filter(":last").fadeIn("slow", fadeNext);
        setTimeout(loopy, 20000);
    });
})();

function fadeNext() {
    // Fade the next image in
    $(this).prev().fadeIn(fadeNext);
    //If there are more than 20 images visible fade the others out
    var more = somediv.find("img:visible:not(:animated):gt(20)");
    if (more.length) {
        more.fadeOut("slow", function() {
            $(this).remove();
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文