最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to reinitialize the lightgallery after ajax call - Stack Overflow

programmeradmin0浏览0评论

I am using lightgallery plugin to display images of my website on click. 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. But the lightgallery is not working for them. It is working only for the images which are loaded at the time of page loading.

I am using lightgallery plugin to display images of my website on click. 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. But the lightgallery is not working for them. It is working only for the images which are loaded at the time of page loading.

Share Improve this question asked Oct 14, 2016 at 8:16 Deepak KishoreDeepak Kishore 1381 silver badge9 bronze badges 3
  • Please add the code where you load more images – Rory McCrossan Commented Oct 14, 2016 at 8:20
  • 1 can you make a fiddle? – Adrian C. Commented Oct 14, 2016 at 8:21
  • 1 checkout this! Its might help you. codepen.io/sachinchoolur/details/QjLNMM – Balasubramanian Commented Oct 14, 2016 at 8:26
Add a ment  | 

2 Answers 2

Reset to default 6

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

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'
});

}

发布评论

评论列表(0)

  1. 暂无评论