有没有办法绑定事件回调,以便为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).
更多推荐
发布评论