0

I'm having trouble using lightGallery() as a callback; I'm trying to use this in Shopify when a customer is selecting product options.

For example, I have a div #lightgallery that has 5 images, and after the page loads, $("#lightgallery").lightGallery(); is called.

When a customer chooses a product variant, the previous 5 images in #lightgallery are removed, a new 4 images of the variant are created, but $("#lightgallery").lightGallery(); doesn't initialize the new gallery for the variant.

besseddrest
  • 155
  • 1
  • 8

3 Answers3

1

Rather than creating and destroying several lightgalleries, I found out that multiple light galleries can be instantiated at once, like so:

$('.gallery').lightGallery();
besseddrest
  • 155
  • 1
  • 8
0

lightGallery needs to be destroyed before a new one is instantiated. So, I call the destroy() method on the onCloseAfter event:

const $lg = $("#lightgallery");    
$lg.lightGallery();

$lg.one('onCloseAfter.lg',function(event){
  $lg.data('lightGallery').destroy('true');
});

Note $lg.one and NOT $lg.on, check out mervick's last comment here: https://github.com/sachinchoolur/lightGallery/issues/238

besseddrest
  • 155
  • 1
  • 8
0

Write down the below mention script on ajax success.

Note: unitPlanslightGallery is the class or id on which we are applying the light gallery.

var lg = jQuery('.unitPlanslightGallery');
// destroy
lg.data('lightGallery').destroy(true);
suvojit_007
  • 1,690
  • 2
  • 17
  • 23