简体   繁体   English

jQuery的淡入/淡出,自定义幻灯片故障,淡入淡出的记忆? 淡出队列?

[英]jquery fadeIn/Out, custom slideshow glitches, fade memory? fade queue?

I am building a background img slideshow and running into glitches I can't comprehend. 我正在构建背景img幻灯片并遇到我无法理解的故障。

I have several objects that contains a list of images. 我有几个包含图像列表的对象。 I have two functions that will take these images, create one div per each, and add the imgs as background of these divs, all within a container. 我有两个函数将拍摄这些图像,每个函数创建一个div,并将img作为这些div的背景添加到一个容器中。

Then, as described in this website , I fadeout the first div,and fadeIn the second, then move the first child to last child position, and loop, creating a slideshow effect. 然后,如本网站中所述 ,我淡出第一个div,然后淡入第二个div,然后将第一个孩子移到最后一个孩子位置并循环播放,以创建幻灯片效果。

When I want this over i .empty() the container. 当我要在我.empty()容器上。 Then the process can start again with the same or another object. 然后,该过程可以从相同或另一个对象重新开始。

The first time I do this, it works, but second, third... times, it starts to glitch. 我第一次执行此操作,但是第二次,第三次...开始出现故障。 Not only two, but all divs start to fade in and out, for I don't know what reason 不仅两个,而且所有div都开始淡入淡出,因为我不知道是什么原因

This happens even if I am using the same object in the first, second, third... attempts. 即使我在第一次,第二次,第三次尝试中使用相同的对象,也会发生这种情况。

It would seem as if although the divs are erased from DOM, apparently there is some memory of them? 似乎虽然从DOM中删除了div,但显然还有一些记忆吗? Could it be related to the fact that created divs share the name with previously created divs? 可能与创建的div与先前创建的div共享名称有关吗? maybe fadein out keep some kind of internal queue I am unaware of? 也许淡出让我不知道的某种内部队列?

Here is an JsFiddle: 这是一个JsFiddle:

https://jsfiddle.net/93h51k9m/11/ https://jsfiddle.net/93h51k9m/11/

and the code: 和代码:

$(document).ready(function(){ 

var imgObject = {
imgs: ['http://lorempixel.com/400/200/sports/1/','http://lorempixel.com/400/200/sports/2/','http://lorempixel.com/400/200/sports/3/']
};
var imgObject2 = {
imgs: ['http://lorempixel.com/400/200/sports/4/','http://lorempixel.com/400/200/sports/5/','http://lorempixel.com/400/200/sports/6/']
};
var noImgObject = {
};
function prepare(index) {
  if ($("#cover").css("display") != "none") {
            console.log("cover is visible: hide it first");
        console.log("fadeOut cover in 3000ms");
        $("#cover").fadeOut(3000, function() {
      console.log("then empty cover")
      $("#cover").empty();
      console.log("now for the images")
      roll(index);
    });
  } else {
    console.log("cover is already hidden: now for the images");
    roll(index);
  };
};

function roll(index) {
  if (typeof index.imgs != "undefined") {
        console.log("called object has images")
        console.log("get them and their numbers")
    var imgs = index.imgs;
    var imgsLength = imgs.length;
    console.log("create as many divs as imgs, and place each img as bg in each div")
    for (i = 0; i < imgsLength; i++) {
      $("#cover").append("<div class='imgdiv" + i + "'></div>");
      $(".imgdiv" + i).css("background-image", "url('"+imgs[i]+"')");
    };
        console.log("now hide all but first div, fadeIn cover and start the carousel");
    //as seen at http://snook.ca/archives/javascript/simplest-jquery-slideshow
    $('#cover').fadeIn(3000);
    $('#cover div:gt(0)').hide();
    setInterval(function() {
        console.log("fade and swap")
      $('#cover :first-child').fadeOut(3000)
        .next('div').fadeIn(3000)
        .end().appendTo('#cover')
    }, 6000);
  } else {
    console.log("index has no images, nothing to do");
  };
};

$("#imgobj").click(function(){
    console.log("imgObject called");
    prepare(imgObject);
});
$("#imgobj2").click(function(){
    console.log("imgObject2 called");
    prepare(imgObject2);
});
$("#noimgobj").click(function(){
    console.log("noImgObject called");
    prepare(noImgObject);
});

});

Thank you 谢谢

Every time click event is invoked, another interval is being started and that is the reason, actions are appended in the queue 每次调用click事件时,都会启动另一个间隔,这就是将操作追加到queue的原因

Use global variable which will hold the setInterval instance and clear it every time you start new Interval. 使用global变量,该变量将保存setInterval实例,并在每次启动新的Interval时将其clear

 var interval; $(document).ready(function() { var imgObject = { imgs: ['http://lorempixel.com/400/200/sports/1/', 'http://lorempixel.com/400/200/sports/2/', 'http://lorempixel.com/400/200/sports/3/'] }; var imgObject2 = { imgs: ['http://lorempixel.com/400/200/sports/4/', 'http://lorempixel.com/400/200/sports/5/', 'http://lorempixel.com/400/200/sports/6/'] }; var noImgObject = {}; function prepare(index) { clearInterval(interval); if ($("#cover").css("display") != "none") { console.log("cover is visible: hide it first"); console.log("fadeOut cover in 3000ms"); $("#cover").fadeOut(3000, function() { console.log("then empty cover") $("#cover").empty(); console.log("now for the images") roll(index); }); } else { console.log("cover is already hidden: now for the images"); roll(index); }; }; function roll(index) { if (typeof index.imgs != "undefined") { console.log("called object has images") console.log("get them and their numbers") var imgs = index.imgs; var imgsLength = imgs.length; console.log("create as many divs as imgs, and place each img as bg in each div") for (var i = 0; i < imgsLength; i++) { $("#cover").append("<div class='imgdiv" + i + "'></div>"); $(".imgdiv" + i).css("background-image", "url('" + imgs[i] + "')"); }; console.log("now hide all but first div, fadeIn cover and start the carousel"); //as seen at http://snook.ca/archives/javascript/simplest-jquery-slideshow $('#cover').fadeIn(3000); $('#cover div:gt(0)').hide(); interval = setInterval(function() { console.log("fade and swap") $('#cover :first-child').fadeOut(3000) .next('div').fadeIn(3000) .end().appendTo('#cover') }, 6000); } else { console.log("index has no images, nothing to do"); }; }; $("#imgobj").click(function() { console.log("imgObject called"); prepare(imgObject); }); $("#imgobj2").click(function() { console.log("imgObject2 called"); prepare(imgObject2); }); $("#noimgobj").click(function() { console.log("noImgObject called"); prepare(noImgObject); }); }); 
 html { color: black; height: 100%; padding: 0; margin: 0; overflow: hidden; } body { height: 100%; padding: 0; margin: 0; background: #f7fafa; } * { box-sizing: border-box; } button { cursor: pointer; } #buttons { z-index: 1000; } #cover { display: none; position: fixed; top: 5vh; left: 0; width: 100vw; height: 95vh; opacity: 0.5; z-index: 0; } #cover div { position: absolute; top: 0; left: 0; width: 100vw; height: 100vh; background-repeat: no-repeat; background-size: cover; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <div id="buttons"> <button id="imgobj">imgObject</button> <button id="imgobj2">imgObject2</button> <button id="noimgobj">noImgObject</button> </div> <div id="cover"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM