我正在重写一个像这样的jQuery fancybox:
$(document).ready(function() {
$("#help").fancybox({
'width': '90%',
'height': '90%',
'autoScale': true,
'transitionIn': 'elastic',
'transitionOut': 'none',
'titleShow': false,
'type': 'iframe'
});
});
但是,在页面传输/回发时,它会多次注册,这会使其减慢到破坏点。有没有办法检查事件是否已经注册,如果没有,请注册吗?
伪代码:
//check if fancybox has not been registered
if($("help").fancybox == null))
{
//register fancy box
}
答案 0 :(得分:3)
当fancybox运行时,它会在jQuery的fancybox
对象中添加data
条目。
您可以测试它的存在:
if( ! $("#help").data().fancybox )
{
//register fancy box
}
答案 1 :(得分:2)
Fancybox使用$(element).data()来存储其元素相关配置。您只需检查fancybox
是否存在。
$("#fancy").data("fancybox");
有关更多信息,请参阅此fiddle。
答案 2 :(得分:1)
此函数会查找您的#help
元素,并仅在fancybox.js存在时加载它们。成功加载Fancybox.js后,它将调用success函数并初始化你的fancybox。
这只会影响具有元素#help
的网页。因此,您可以保存HTTP请求和带宽。
请参阅:http://api.jquery.com/jQuery.ajax/
您还应该查找允许您处理错误的error function
。
$(document).ready(function() {
// Look for #help
if ( $('#help').html() !== null ) {
// Load, init and config the fancybox
$.ajax({
url: '/js/path/to/fancybox.js',
type: 'GET',
dataType: 'script',
success: function() {
// Init Fancybox
$("#help").fancybox({
'width': '90%',
'height': '90%',
'autoScale': true,
'transitionIn': 'elastic',
'transitionOut': 'none',
'titleShow': false,
'type': 'iframe'
});
}
});
}
});
答案 3 :(得分:0)
有点晚了,但对于Fancybox 2,我使用:
parent.jQuery.fancybox.isOpen
我认为你也可以使用:
parent.jQuery.fancybox.isOpened
如果网站上打开了fancybox,则两个语句都将返回true。