如何确定fancybox是否已打开?

10

我需要知道fancybox是否已经开放了允许或拒绝另一个函数启动的功能。

Fancybox内置的函数(如'onStart'或'onClosed')不起作用。

我说的是版本1.3.0 RC2。

8个回答

13

$.fancybox.isOpen (布尔值) 表示fancybox是否打开。


更好和更漂亮的解决方案,应该是答案。 - Dmitry Borovkov
因为某种原因,当它打开时,它返回未定义。 - Chewie The Chorkie

10

你使用的版本显然与文档不匹配;我查看了源代码并发现选项名称与在线文档不同。我刚刚在1.3RC2中进行了以下测试:

$(document).ready(function() {

    function myStartFunction() { alert('fancy box opened'); }

    $("a#inline").fancybox({
        'onStart': myStartFunction
    });  
});
你需要的选项是'onStart' - 在我的myStartFunction函数中,每次我打开一个盒子时都会触发警报。我不确定他们何时更改了选项,但你可以在使用的任何版本底部查看源代码,并查看应该称为什么选项。
编辑
我刚刚在v1.2.5上进行了双重检查-我使用的版本-回调确实有不同的名称。callbackOnStart适用于1.25,但如我所说,不适用于1.3。

哈哈,如果你能在 http://fancybox.net/dev/130/ 的源代码中看到他们从第二个参数调用'onStart'等函数,那么这种方式不起作用。非常感谢Erik ^^ - Ax.

8
尝试使用方法。
beforeLoad, // Before loading
afterLoad, // After loading
beforeShow, // Before changing in current item
afterShow, // After opening

这段代码运行良好。
$(".fancybox").fancybox({beforeShow:function(){alert('blah');}});

OP说:“我在谈论的是1.3.0 RC2版本”。 - JFK

4
如果您想要确定fancybox是否已打开(而不是在打开fancybox时触发的事件),则截至fancybox v2,可以使用$.fancybox.isOpen属性进行操作。它没有被记录在文档中(至少我没有找到任何参考资料),但它确实有效。
示例:
if(!$.fancybox.isOpen) {
    $.fancybox.open({
        href: '#newsletter-campaign-popup',
        height: 385,
        width: 510,
        type: 'inline'
    });
}

上面的例子防止打开第二个fancybox,如果已经打开了一个。


3

我不确定您所说的“内置函数”是什么意思。Fancybox有回调函数,可以调用用户自定义的函数(这些函数需要您自己编写)。例如:

function myClose()
{
    alert('close');
}

function myStart()
{
    alert('start');
}

$("a#single_image").fancybox({
     'callbackOnClose': myClose,
     'callbackOnStart': myStart
// etc, etc..
}); 

或者您可以检查fancy_content div,看看里面是否有任何内容。(如果有,则fancybox已打开)。

if ( $('#fancy_content:empty').length > 0 )
{
  // Is empty
}

2

这对我来说似乎有效:

        isLoaded : function(){
            return $('#fancybox-content').html()!='';
        }

1
如果您有多个用于fancybox的div,下面的代码可能是更好和更通用的解决方案:
if ($('.fancybox-opened').length == 0) {
  //there is no fancybox opened at all
}

请注意,即使fancybox关闭,其内容可能并不总是为空。

1

2018年12月的答案。

if ($('.fancybox-content').length == 0) {
    //there is no fancybox opened at all
}

细节:

    function checkf()
    {
        if ($('.fancybox-content').length == 0) {
            alert('there is no fancybox opened at all.');
        }
        else
        {
            alert('there is a fancybox opened.');
        }
    }
    $("#ffbox").click(function()
    {
        setTimeout(checkf,1000);
    });
    checkf();
    <head>
        <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.2/dist/jquery.fancybox.min.css" />
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="//cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.2/dist/jquery.fancybox.min.js"></script>
    </head>
    <body>
        <a data-fancybox data-options='{"iframe" : {"css" : {"width" : "80%", "height" : "80%"}}}' href="https://www.google.com/maps/search/?api=1&query=China" class="btn btn-primary" id="ffbox">Display Google Map</a>
    </body>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接