【问题标题】:Fancy box not opening second time花式盒子第二次打不开
【发布时间】:2014-10-15 16:16:35
【问题描述】:

我正在尝试打开一个精美的盒子,除了第二次无法打开精美盒子的问题之外,一切都已准备就绪。

第一次打开很好,第二次尝试打开它没有打开它,这是我的代码

jQuery(document).ready(function() {

jQuery("#destination_Weather").fancybox({
    'titlePosition'     : 'inside',
    'autoScale'          :true,
    'autoDimensions'    : true,  
    'width'             : 600,
    'height'            : 'auto',
    'transitionIn'      : 'none',
    'transitionOut'     : 'none'
   });
});
<div style="display: none">
    <div id="destinationWeather">
        <?php  if(!empty($lat_long_cordinates))  {
             echo displayDestinationWeather('',$lat_long_cordinates);
          } ?>
     </div>
   // one more div used for another fancybox content
</div>

<a href="#destinationWeather" id="destination_Weather">link </a>

我不确定为什么会发生这种情况,但是当我第二次单击链接时,它正在重新加载整个页面。 不确定这是花式实现的问题还是 PHP 的一些错误实现。

我注意到还有一件事,当我第一次关闭弹出窗口时,destinationWeather div 丢失了所有数据,我只能在那里看到这些信息

<div class="fancybox-inline-tmp" style="display: none;"></div>

不知道为什么会这样?

重要提示:请注意,fancybox v1.3.4 不适用于 jQuery v1.9+。

【问题讨论】:

  • 能否在这里发个链接,可能是网址有问题
  • @MoeedFarooqui:我正在本地开发它,所以我不确定我是否能够粘贴链接
  • fancybox 什么版本?
  • @JFK 我使用的是 1.3.4 版
  • @UmeshAwasthi :你说 1.3.4 不适用于 jQuery 1.9+,我使用 1.11 和 jQuery 的迁移插件。有没有办法解决这个问题?我无法迁移 Fancybox 1.3.4 ....

标签: php jquery fancybox


【解决方案1】:

由于您使用的是fancybox v1.3.4,因此您必须这样做

1)。您的目标内联 div 应该包含在一个额外的 div 中,例如:

<div style="display: none">
    <div id="destinationWeather">
        <?php  if(!empty($lat_long_cordinates))  {
            echo displayDestinationWeather('',$lat_long_cordinates);
        } ?>
    </div>
</div>

注意目标div#destinationWeather)不应该有任何display:none属性,而是父包装div(在上面的代码中添加)

2)。 v1.3.4 中有一个关于inline 内容的已知错误(记录在here),因此您必须在代码中应用解决方法:

jQuery(document).ready(function () {
    jQuery("#destination_Weather").fancybox({
        'titlePosition': 'inside',
        'autoScale': true,
        'autoDimensions': true,
        'width': 600,
        'height': 'auto',
        'transitionIn': 'none',
        'transitionOut': 'none',
        // fix inline bug
        'onCleanup': function () {
            var myContent = this.href;
            $(myContent).unwrap();
        }
    });
});

JSFIDDLE

重要提示:还要注意,fancybox v1.3.4 不适用于 jQuery v1.9+。请参阅this post 以获取更多参考和解决方法。

【讨论】:

  • 感谢您的输入,我的内容 div 周围已经有 &lt;div style="display: none"&gt;,我使用了您的代码,但它仍然无法正常工作。我还注意到我点击 link 的那一刻,它删除了destinationWeather div 并替换为&lt;div class="fancybox-inline-tmp" style="display: none;"&gt;&lt;/div&gt;,所以我猜onCleanup 为时已晚
  • replacing with &lt;div class="fancybox-inline-tmp" style="display: none;"&gt;&lt;/div&gt; 是预期的行为,因为内容从文档流中的位置移动到fancybox并返回设置临时占位符的位置...除非错误.
  • 啊哈,感谢您的输入,但仍然存在问题,并且第二次点击整个页面正在重新加载。
  • 可能是因为 php 被缓存了所以我想知道这个if(!empty($lat_long_cordinates)) 条件在移动到fancybox时是如何验证的
  • 我使用了这个解决方案,但用if (myContent.charAt(0)=="#") $(".fancybox-inline-tmp").replaceWith($(myContent)[0].outerHTML) 替换了$(myContent).unwrap();。此代码手动将 fancybox tmp 占位符替换为原始内容(已移动到 fancybox 容器)
【解决方案2】:

如果这对这个问题的未来观点有任何用处,我只是花了很长时间试图让它在 Wordpress 中工作,其中一个插件正在加载 v1.3.4 Fancybox。

在对变通办法没有太多好运之后(至少在某种意义上仍然没有给我插件的工作配置),我发现 v1.3.6 Fancybox 不包含上述错误并且是最快和最对我来说方便的解决方案。

【讨论】:

  • 备案,没有fancybox v1.3.6。它很可能是 WordPress 插件的版本,我敢打赌它仍然使用原来的 fancybox v1.3.4(除非你能证明我错了)
  • 天哪,我不知道我指的是哪个网站来检查 1.3.6 是什么。我可以看到 Fancybox 在他们的网站上没有那个版本。不幸的是,我现在无法澄清实际的 Fancybox 版本是什么。
【解决方案3】:

有一种方法可以解决与 fancybox-1.3.4 相关的问题。使用 fancybox 'onClosed' 事件,将原始 div 内容 HTML 重新分配回相同的 fancybox div 元素。这样当fancybox关闭时,我们会将div(html)的原始内容重新分配给它自己。

$(document).ready(function () {

        fancyMain = $("#fancyMain").html(); //here fancyMain is the div element which is set with display:none

        $("#fancyLinkId").fancybox({
            'hideOnContentClick': false,
            'autoScale': false,
            'autoSize': true,

            'onClosed': function () {
                $('#fancyMain').html(fancyMain);

        }
});

FancyMain 是主 div,样式为 display:none 这是实际花式框内容与 id fancydata 一起的内部 div //花式框中的内容放在这里 //结束内部div //结束主目录

//然后锚链接关联到内部div一个fancyLinkId href #fancydata

这行得通

【讨论】:

    【解决方案4】:

    动态内联内容加载

          function loadAnswer(id) 
            {     
                    jQuery.fancybox({       
                     'content' : jQuery("#outerFAQ"+id).html(),         
                      /*For Auto height and width*/
                     'onComplete' : function(){
                      jQuery('#fancybox-wrap').css({height:'auto',width:'auto'});
                            /*jQuery('.fancybox-wrap').css('left','50%');*/
                              jQuery.fancybox.resize();
                             jQuery.fancybox.center();  
                                    }
                }); 
            }
    
        /*Dynamically Set the id and passing it to function*/  
         <a class="fancybox"  href="#" onClick="loadAnswer(<?php echo $data->getId()/*Dynamic id*/ ?>);"> Demo </a>
    
            <div id="outerFAQ<?php echo $data->getId(); ?>"  style="display:none;"> 
            <div id="ans<?php echo $data->getId();?>">
                       demo        
                 </div>
     <div>  
    

    单个内联内容加载

       jQuery( "#demo" ).click(function() {
          jQuery.fancybox({
                    'content' : jQuery("#demoContent").html(),
                     onComplete: function () {
    
                                    /*Static height width*/  
                                   jQuery("#fancybox-content").css({
                                            height:'500px',
                                            overflow:'scroll',
                                        });
                                        jQuery("#fancybox-wrap").css("top","10");
                                        jQuery(".fancybox-wrap").css("position", "absolute");
                                    }
            });
    
        <a  id="demo"  href="#">Click me</a>
    
        <div style="display: none">
                    <div id="demoContent">
                        demo
                    </div>
        </div> 
    

    【讨论】:

      猜你喜欢
      • 2011-07-02
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多