PhotoSwipe: Is there a way to attach an event handler to the 'init' event or when the plugin opens the lightbox?

ⅰ亾dé卋堺 提交于 2019-12-25 04:42:13

问题


I'm using PhotoSwipe lightbox in conjuntion with Slick carousel for a project, and I want to have the carousel autoplay while the photoswipe lightbox is not open, but when the lightbox is opened, I want the autoplay to stop.

In the PhotoSwipe API they have a close and destroy event to listen for, but not an event for init or open. Has anyone found a good way to do this?

Something like:

$('#gallery').slick({
  autoplay: true,
  dots: true,
});

// ... Setup photoswipe...

pswp.listen('open', function() {
  $('#gallery').slickSetOption('autoplay', false, false);
});

pswp.listen('close', function() {
  $('#gallery').slickSetOption('autoplay', true, false);
});

pwsp.init();

回答1:


So to do this, I found that you can just do what you need to do before init, then attach an event handler on close.

Also there's an issue with Slick where the autoplay doesn't turn off using the slickSetOption command as of version 1.3.15.

https://github.com/kenwheeler/slick/issues/731

Here is my workaround for it based on one of the comments on the issue:

// ... Setup PhotoSwipe ...
slider.slickPause();
slider.find('.slick-list').off('mouseleave.slick');

gallery.listen('close', function() { 
  slider.slickPlay();
  slider.find('.slick-list').on('mouseleave.slick', function() {
    slider.slickPlay();
  });
});

gallery.init();   


来源:https://stackoverflow.com/questions/27841324/photoswipe-is-there-a-way-to-attach-an-event-handler-to-the-init-event-or-whe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!