简体   繁体   中英

How to destroy and re-initialise lightgallery.js

I am using lightgallery on my page, which is working fine. I have added a filter function to the gallery, so now need to destroy and rebuild my lightgallery to reflect the change in the gallery content. I have this so far:

source.js

import 'lightgallery.js';


var gallery = document.getElementById('gallerywrapper');

  if(gallery) {
    lightGallery(gallery, {
      download: false,
      counter: false,
      selector: '.item.active'
    });
  }

  var galleryFilterButtons = document.querySelectorAll('.gallery-filter-button');

  for(var i = 0; i<galleryFilterButtons.length; i++) {
    galleryFilterButtons[i].addEventListener("click", function(){
      // destroy and rebuild here...
    });
  }

I can't get the destroy function to work at all, can anyone give any pointers?

The documentation shows that you can destroy with this:

 window.lgData[gallery.getAttribute('lg-uid')].destroy(true);

and then just init again. So in your case it would be:

for (var i = 0; i < galleryFilterButtons.length; i++) {
    galleryFilterButtons[i].addEventListener("click", function(){
        window.lgData[gallery.getAttribute('lg-uid')].destroy(true);
        lightGallery(gallery); 
    });
}

You can see a use case here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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