简体   繁体   English

jQuery-Lightgallery.js-检测到slidehsow即将结束

[英]jQuery - Lightgallery.js - Detect approaching end of slidehsow

Does anyone know what kind of condition I can use in the code below to establish whether the slideshow is nearing its end? 有谁知道我可以在下面的代码中使用哪种条件来确定幻灯片是否即将结束?

In other words, if there are fewer than 4 images left in the slideshow, send a new ajax call and append the response to the current response. 换句话说,如果幻灯片中剩余的图像少于4张,则发送一个新的ajax调用并将响应附加到当前响应。

I manage to get the index of each image, but the condition I'm using below, ie if (index > 2) obviously fails because it triggers the ajax call on any subsequent item, and the number of items in each slideshow will vary, so a fixed number won't do. 我设法获得每个图像的索引,但是我在下面使用的条件,即if (index > 2)显然失败,因为它触发了任何后续项的ajax调用,并且每个幻灯片的项数都会变化,所以固定号码不会。 Below is what I have so far. 以下是到目前为止的内容。

Also in a fiddle here . 还在这里摆弄。

$(document).ready(function() {
    $("#links").lightGallery({
        selector: '.img-wrap',
        html:true,
        speed: 300,
    });     
    var $lg = $('#links');     
    $lg.lightGallery(); 
    $lg.on('onBeforeSlide.lg',function(event, index, fromTouch, fromThumb){        
        console.log(index, fromTouch, fromThumb);        
        if (index > 2) { // This doesn't work
            // Trigger new ajax call
            // Append items in response to current slidewhow
        }
    });
});

What if you subtract your buffer number (two in this case) from the length of the elements with .img-wrap 如果使用.img-wrap从元素的长度中减去缓冲区号(在这种情况下为2)怎么办?

$(document).ready(function() {
    $("#links").lightGallery({
        selector: '.img-wrap',
        html:true,
        speed: 300,
    });     

    var $lg = $('#links');
    var bufferThreshold = 2;//number of images remaining before we fetch more

    $lg.lightGallery();

    $lg.on('onBeforeSlide.lg',function(event, index, fromTouch, fromThumb){
        console.log(index, fromTouch, fromThumb);
        if (index > $('.img-wrap').length - bufferThreshold) {
            //almost done, get more!
        }
    });

});

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

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