如何销毁并重新初始化lightgallery.js

时间:2020-04-10 13:01:33

标签: javascript lightgallery

我在页面上使用了lightgallery,效果很好。我向图库添加了过滤器功能,因此现在需要销毁并重建我的lightgallery,以反映图库内容中的更改。到目前为止,我已经知道了:

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

我根本无法使用destroy函数,任何人都可以提供任何指针吗?

1 个答案:

答案 0 :(得分:0)

The documentation表明您可以使用以下方法摧毁:

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

,然后再次初始化。因此,您的情况应该是:

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

您可以看到用例here