简体   繁体   English

ajax调用后如何重新初始化lightgallery

[英]How to reinitialize the lightgallery after ajax call

I am using lightgallery plugin to display images of my website on click.我正在使用 lightgallery 插件在点击时显示我网站的图像。 I initialized the light gallery as我将灯光画廊初始化为

$(document).ready(function(){
    $('#lightgallery').lightGallery({
        selector: '.item'
    });
});

At the document load its working fine.在文档加载它的工作正常。 The problem is when i am loading more images and appending them using jquery i want to reinitialize the lightgallery again to work for the iamges loaded by ajax call.问题是当我加载更多图像并使用 jquery 附加它们时,我想再次重新初始化 lightgallery 以便为 ajax 调用加载的图像工作。 But the lightgallery is not working for them.但是灯廊对他们不起作用。 It is working only for the images which are loaded at the time of page loading.它仅适用于页面加载时加载的图像。

This might help: 这可能会有所帮助:

function createLightGallery()
{
    $('#lightgallery').lightGallery({
        selector: '.item'
    });
}

// on document load
$(document).ready(function(){
    createLightGallery();
});

// on the AJAX request
$.ajax({ 
    url: "/get_images",
    .
    .
    success: function()
    {
         if("#lightgallery").data("lightGallery"))
             $("#lightgallery").data("lightGallery").destroy(true);
         createLightGallery();
    }
});

This is my answer that works in ajax这是我在 ajax 中有效的答案

success: function() {成功:函数(){

//destroy your lightgallery
 try{ $('#lightgallery').lightGallery(); $('#lightgallery').data('lightGallery').destroy(true); }catch(ex){};

//and then re-initiate gallery again
$('#lightgallery').lightGallery({
    selector: '.item'
});

} }

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

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