【问题标题】:How to destroy and re-initialise lightgallery.js如何销毁和重新初始化 lightgallery.js
【发布时间】:2020-04-10 13:01:33
【问题描述】:

我在我的页面上使用 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函数,谁能指点一下?

【问题讨论】:

    标签: javascript lightgallery


    【解决方案1】:

    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。

    【讨论】:

      猜你喜欢
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 2023-03-30
      • 1970-01-01
      • 2020-09-11
      • 2019-07-29
      相关资源
      最近更新 更多