简体   繁体   English

Fancybox v2在第二次点击时失败

[英]Fancybox v2 failing on second click

I have a site that uses the ajax option of Fancybox v2 to display certain bits of information such as the help, contact information, etc. I have noticed a problem with it, however, where it works fine on the first click but generally misbehaves on subsequent clicks - the actual pop-up window may appear briefly and then disappear, or it may never appear at all, and the overlay may load two or three times so that it takes several clicks to clear it. 我有一个网站,使用Fancybox v2的ajax选项显示某些信息,如帮助,联系信息等。我注意到它有一个问题,但是,它在第​​一次点击时工作正常,但一般行为不端后续点击 - 实际弹出窗口可能会短暂出现然后消失,或者根本不会出现,并且叠加层可能会加载两到三次,因此需要多次点击才能清除它。

It doesn't matter whether you click the same link or another link that also uses Fancybox. 无论您是单击同一链接还是同时使用Fancybox的其他链接,都无关紧要。 The Firebug console reports no errors. Firebug控制台报告没有错误。 If you refresh the page you get another single reliable click, and then subsequent clicks exhibit the odd behaviour again. 如果刷新页面,则会获得另一次可靠的点击,然后后续点击再次显示奇怪的行为。

I have created a stripped-down version of one of our pages (removing all other scripts) to see if there was something causing a conflict, but this doesn't seem to be the case. 我创建了一个页面的精简版(删除所有其他脚本)以查看是否存在导致冲突的内容,但似乎并非如此。 You can have a look at it here: 你可以在这看看它:

http://frontandback.com.au/fancytest/ http://frontandback.com.au/fancytest/

The three links in the top-right of the site have the Fancybox applied to them. 站点右上角的三个链接已应用Fancybox。 Example: 例:

<ul>
    <li><a href="index.html" data-fancybox-href="index.html #main" target="_blank" class="fancybox fancybox.ajax">Contact us</a></li>
    <li><a href="index.html" data-fancybox-href="index.html #main" target="_blank" class="fancybox fancybox.ajax">Help</a></li>
    <li><a href="index.html" data-fancybox-href="index.html #main" target="_blank" class="fancybox fancybox.ajax">FAQs</a></li>
</ul>

If anyone has any suggestions as to what is causing this odd problem, it would be greatly appreciated. 如果有人对什么导致这个奇怪的问题有任何建议,将不胜感激。

Cheers. 干杯。

From this post I created a revised version where you can load partial content from a file via ajax. 这篇文章中,我创建了一个修订版本,您可以通过ajax从文件加载部分内容。

This new revision uses a (HTML5) data- attribute to pass the hash of the URL so instead of doing this 这个新版本使用(HTML5) data-属性来传递URL的hash ,而不是这样做

<li><a href="index.html" data-fancybox-href="index.html #contact" target="_blank" class="fancybox fancybox.ajax">Contact us</a></li>

we'll do this 我们会这样做的

<li><a href="index.html" data-segment="#contact" target="_blank" class="fancybox">Contact us</a></li>

... notice that we are not using any special fancybox class fancybox.ajax nor fancybox.iframe as suggested in the comments section. ...请注意 ,我们没有按照评论部分的建议使用任何特殊的fancybox fancybox.ajaxfancybox.iframe

Then the basic script 然后是基本脚本

$(".fancybox").on("click", function() {
    targetContent = $("<div />").load(this.href +" "+ $(this).data("segment")); 
    $.fancybox(targetContent, {
        fitToView: false,
        autoSize : false,
        width: 420, // or whatever
        height: 280
    }); // fancybox
    return false; // prevent default
}); // on

You may set dimensions to each <div> from the remote file via style="" attribute, then set autoSize : true and remove the width and height options to get a dynamic size. 您可以通过style=""属性从远程文件为每个<div>设置维度,然后设置autoSize : true并删除widthheight选项以获得动态大小。

Of course, the revised DEMO 当然, 修改后的DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM