如何全局处理fancybox的onClose事件? [英] How do I globally handle onClose event of fancybox?

查看:78
本文介绍了如何全局处理fancybox的onClose事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用Fancybox。现在要处理close事件,我们写下如下:

I am using Fancybox in my application. Now to handle the close event we write something like:

$(".fancybox").fancybox({onClose:function(){alert('blah');}}

如本文档页面所示:

http://fancyapps.com/fancybox/#docs

然而,我想为所有fancybox编写一些常见且全局的东西,每次都可以为任何fancybox运行。我该怎么做?总之我不想写每个fancybox上onClose的代码,并且不希望它依赖于class,id(在这种情况下,例如<。code> .fancybox )。我该怎么做?我试过了写:

However I want to write something common and global to all fancybox that gets run everytime for any of the fancybox. How do I do that? In short I don't want to write the code of onClose on each fancybox and don't want it to be dependent upon class,id (Eg. .fancybox in this case). How do I do that? I tried to write:

$.fancybox({onClose:function(){alert('blah');}}

但它不起作用,从文档看起来这是以程序方式打开的功能。

but it doesn't work and from the docs it looks like this is the function for opening progammatically.

推荐答案

我不确定这是否是最佳解决方案但我会去酒吧/ sub pattern所以,在你的fancybox中你应该做那样的事情。

I'm not sure if this is the best solution but I would go for a pub/sub pattern so, in your fancybox you should do something like that.

$(".fancybox").fancybox(
{   onClose:function(){
                $(window).trigger('fancyboxClosed');
            }
});

然后,代码中的某处:

$(window).on('fancyboxClosed', function(){
    // Your global function for fancybox closed
});

我不太了解你的目的,但要记住你总能传递一个命名函数而不是匿名的,所以你总是触发相同的功能。

I don't know very much about your purpose but have in mind you can always pass a named function instead of an anonymous one so you always trigger the same function.

如果您想根据每个花式箱制作不同的东西,上面的示例应该是可行的方法。

The above example should be the way to go if you want to make different things depending on each fancybox.

此外,您可以将事件附加到您想要的任何对象。只是使用了窗口以方便使用。

Also, you can attach the event to whatever object you want. Just used window for convenience.

编辑

您还可以编辑您的fancybox这样做的源(编辑为1.3.4)只需将 $(窗口).trigger('fancyboxClosed'); 添加到第953行左右。

You can also edit your fancybox source to do that (edit for 1.3.4) just add $(window).trigger('fancyboxClosed'); to around line 953.

这篇关于如何全局处理fancybox的onClose事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆