我需要知道fancybox 已经打开以允许或拒绝启动另一个功能。
像“onStart”或“onClosed”这样的内置函数 Fancybox 不起作用。
我说的是版本 1.3.0 RC2
我需要知道fancybox 已经打开以允许或拒绝启动另一个功能。
像“onStart”或“onClosed”这样的内置函数 Fancybox 不起作用。
我说的是版本 1.3.0 RC2
$.fancybox.isOpen
(bool) 表示,fancybox 是否打开。
您使用的版本显然与文档不匹配;我查看了源代码,发现选项的名称与在线文档不同。我刚刚用 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
尝试使用方法
beforeLoad, // Before loading
afterLoad, // After loading
beforeShow, // Before changing in current item
afterShow, // After opening
此代码工作正常
$(".fancybox").fancybox({beforeShow:function(){alert('blah');}});
如果您正在寻找一种方法来确定fancybox 是否打开(而不是在打开fancybox 时触发的事件),那么从fancybox v2 开始,您可以使用该$.fancybox.isOpen
属性。它没有记录(至少我没有找到任何参考资料),但它有效。
例子:
if(!$.fancybox.isOpen) {
$.fancybox.open({
href: '#newsletter-campaign-popup',
height: 385,
width: 510,
type: 'inline'
});
}
如果已经打开了第二个fancybox,则上面的示例会阻止打开第二个fancybox。
我不确定你所说的内置函数是什么意思。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
}
这似乎对我有用:
isLoaded : function(){
return $('#fancybox-content').html()!='';
}
如果您有多个用于 fancybox 的 div,以下代码可能是更好、更通用的解决方案:
if ($('.fancybox-opened').length == 0) {
//there is no fancybox opened at all
}
另请注意,即使 fancybox 已关闭,fancybox 的内容可能并不总是空的。
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>