将回调应用于Magnific Popup的所有实例?(Apply callback to all instances of Magnific Popup?)

编程入门 行业动态 更新时间:2024-10-25 22:24:21
将回调应用于Magnific Popup的所有实例?(Apply callback to all instances of Magnific Popup?)

有没有办法绑定事件回调,以便为Magnific Popup的所有实例调用它? 在一个网站上我正在构建几个元素有固定的位置,并在主浏览器滚动条消失时“跳转”。 我想在打开任何弹出窗口之前设置一个回调来为这些元素添加填充,并在弹出窗口关闭时删除填充。 每次初始化Magnific Popup时我都不想重新使用回调 - 我宁愿设置一组每次Magnific打开并关闭弹出窗口时都会运行的回调。

我的第一个想法是使用事件委托并在文档树中绑定相当高的值。 我试图绑定到mfpOpen事件并监听BODY标记,但它似乎不是事件气泡。 还有其他解决方案吗?

Is there a way to bind an event callback so that it's called for all instances of Magnific Popup? On a site I'm building several elements have fixed position and "jump" when the main browser scrollbar disappears. I want to set a callback to add padding for those elements before any popup is opened, and remove the padding when the popup is closed. I don't want to re-use the callbacks every time I initialize Magnific Popup – I'd rather set one set of callbacks that is run every time Magnific opens and closes a popup.

My first thought was to use event delegation and bind pretty high in the document tree. I was trying to bind to the mfpOpen event and listen on the BODY tag, but it doesn't seem like the event bubbles. Is there another solution?

最满意答案

仅在目标元素或文档上调度事件(如果目标元素不存在)。

但您可以使用原型全局覆盖open函数,例如:

$.magnificPopup.instance.open = function(data) { console.log('before open anything'); $.magnificPopup.proto.open.call(this,data); console.log('after open anything'); };

你可以用close函数做同样的事情(它没有任何参数)。

Events are dispatched only on target element or on document (if target element is not present).

But you may override open function globally using prototype, e.g.:

$.magnificPopup.instance.open = function(data) { console.log('before open anything'); $.magnificPopup.proto.open.call(this,data); console.log('after open anything'); };

You may do the same thing with close function (it doesn't have any arguments).

更多推荐

本文发布于:2023-07-26 02:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269574.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用于   回调   实例   Magnific   Popup

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!