简体   繁体   English

内容完成渲染后动态设置fancybox 2.1.4 iframe的高度

[英]Setting height of fancybox 2.1.4 iframe dynamically after contents finish rendering

We have some jQuery graphs that load into a fancybox iframe. 我们有一些jQuery图加载到fancybox iframe中。 Recently, some of these graphs are too tall for the fancybox frame, and the design requirement is that there is no scroll bar unless the height is over 700px AND there is no more than 10px of whitespace above and below the graph. 最近,这些图形中的一些对于fancybox框架而言过高,并且设计要求是除非高度超过700px并且图形上方和下方的空白不超过10px,否则没有滚动条。

I tried this: 我尝试了这个:

afterLoad : function(){
    //(...)
    $('.fancybox-inner').height($('.fancybox-iframe').contents().height() + 20);
    console.log($('.fancybox-inner').height());
}

The console.log() correctly displays the desigred height. console.log()正确显示指定的高度。

But the fancybox is still displayed with the default height. 但是fancybox仍显示为默认高度。

Putting a breakpoint on the console.log() line, the page displays the fancybox frame on the top of the window, graph rendered and iframe with the correct height. console.log()行上放置一个断点,页面将在窗口顶部显示fancybox框架,呈现的图形和具有正确高度的iframe。 When I release the debugger stop, fancybox moves the frame to the center of the viewport, and it is back to the default height (the same behavior when not in debug mode). 当我释放调试器停止点时,fancybox将框架移到视口的中心,然后又回到默认高度(不在调试模式下,行为相同)。

How can I make fancybox use the desired height? 如何使fancybox使用所需的高度? It depends on the graph, so I cannot set it in the options. 它取决于图形,因此我无法在选项中进行设置。

Dealing with iframes make things a bit difficult but you may try something like 处理iframe会使事情有些困难,但是您可以尝试以下方法

$(document).ready(function () {
    $("a.fancybox").fancybox({
        type: "iframe",
        fitToView: false, // we need this
        autoSize: false, // and this
        maxWidth: "95%", // and this too
        beforeShow: function () {
            // find the inner height of the iframe contents
            var _newHeight = $(".fancybox-iframe").contents().find("html").innerHeight();
            this.height = _newHeight
        }
    }); // fancybox
}); // ready

Notice we disable fitToView and autoSize to be able to set the preferred height , however we also need to set a maxWidth to avoid fancybox going off screen dimensions. 注意,我们禁用fitToViewautoSize来设置首选height ,但是我们还需要设置maxWidth以避免fancybox超出屏幕尺寸。

Also notice we used the beforeShow callback to set the this.height setting. 还要注意,我们使用了beforeShow回调来设置this.height设置。

的jsfiddle

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

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