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

I am using lightgallery plugin to display images of my website on click. I initialized the light galler

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

}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742354524a4428171.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信