Loop through an Array of links and dynamically load the contents of each after a set interval

后端 未结 4 1361
孤城傲影
孤城傲影 2021-01-16 06:47

Using jQuery I would like to loop through an array of links and load the contents of each link into a DIV after a set interval. For example, if I have my parent page \"paren

4条回答
  •  梦谈多话
    2021-01-16 07:13

    DOM manipulation is expensive. Try not to do it if you can avoid it.

    Are the links you're loading static or dynamic? Meaning once loaded do you need to load them again? If the answer is no then do this:

    
    

    with CSS:

    #carousel div { display: none; }
    

    and:

    var pages = ["text1.html", "text2.html", "text3.html"];
    
    $(function() {
      for (int i=0; i
    ").appendTo("#carousel").load(pages[i]); } setInterval(rotate_pages, 5000); }); function rotate_pages() { var carousel = $("#carousel"); var pages = $(carousel).children(); var visible = pages.filter(":visible"); if (visible.length == 0) { pages.get(0).fadeIn(); } else { var index = pages.index(visible); index++; if (index >= pages.length) [ index = 0; } visible.fadeOut(function() { pages.get(0).fadeIn(); }); } }

    This way you only load the pages once and just hide/show divs as needed to rotate through them.

提交回复
热议问题