PhotoSwipe:有没有一种方法可以将事件处理程序附加到"init"事件,或者当插件打开灯箱时? [英] PhotoSwipe: Is there a way to attach an event handler to the 'init' event or when the plugin opens the lightbox?
问题描述
我正在将 PhotoSwipe 灯箱与
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.
在 PhotoSwipe API 中,它们具有一个close
和destroy
事件来监听,但不是init
或open
的事件.有没有人找到这样做的好方法?
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?
类似的东西:
$('#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();
推荐答案
因此,我发现您可以在初始化之前执行所需的操作,然后在close
上附加事件处理程序.
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
.
Slick还存在一个问题,即从1.3.15版开始,无法使用slickSetOption
命令关闭自动播放功能.
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();
这篇关于PhotoSwipe:有没有一种方法可以将事件处理程序附加到"init"事件,或者当插件打开灯箱时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!