【问题标题】:How to force an iframe size programmatically?如何以编程方式强制 iframe 大小?
【发布时间】:2017-12-22 07:51:08
【问题描述】:

我正在制作页面构建器类型的东西。对于普通视图,iframe 需要相应地调整大小,但对于移动/平板电脑视图,它需要设置大小。

使用 iframe-resizer https://github.com/davidjbradshaw/iframe-resizer @david-bradshaw

父窗口:

<script type="text/javascript" src="javascript/iframeResizer.min.js"></script>
....
$("iframe")[0].iFrameResize({
    checkOrigin: false,
    enablePublicMethods     : true,
    heightCalculationMethod: "bodyScroll",
});
....

视点大小改变器会这样做:

// set the iframes width and height "before" iframeresizer does stuff to make it look seemless    
_this.editor.css({
    "width":mapper[size].width,
    "height":mapper[size].height
})

我看到 .size() 工作的唯一方法是 iframe 本身

// fire off a sendMessage cause only way i saw .size() working is from the iframe itself
_this.editor[0].iFrameResizer.sendMessage({
    "width":mapper[size].width,
    "height":mapper[size].height,
    "forceSize":true
});

iframe 窗口

<script>
    var iFrameResizer = {
        messageCallback: function(message){
            console.log("messageCallback")
            console.log(message)
            if (message.forceSize==true){
                console.log(window.parentIFrame)
                window.parentIFrame.autoResize(false);
                window.parentIFrame.size(message.height); // Set height to 100px
            } else {
                // apply auto resizing again
            }

        }
    }
</script>
<script type="text/javascript" src="javascript/iframeResizer.contentWindow.min.js"></script>

我希望有一种更简单的方法,比如在父级 disableIframeResizer() 或其他东西上,或者能够从父级设置 .size() ,因为这是选择你的视点部分的地方。我似乎无法弄清楚: (

我的问题是:我需要以某种方式告诉 iframe 在 forceSize = true 时不要调整其大小,然后在收到消息时相应地调整大小。

感谢您提供了一个相当不错的图书馆!

【问题讨论】:

    标签: iframe-resizer


    【解决方案1】:

    我认为我没有 100% 理解您的问题。但我尽量按照我的理解回答你的问题。

    您可以非常简单地在 html 中设置 iframe 的大小。你也可以在 CSS 的帮助下做到这一点。

    您可以这样做:&lt;iframe href="https://google.com" width="100%" height="100%"&gt;Your Browser Does Not Support Iframe&lt;/iframe&gt;

    您也可以像这样设置确切的直径:&lt;iframe href="https://google.com" width="300px" height="100px"&gt;Your Browser Does Not Support Iframe&lt;/iframe&gt;

    你也可以使用php来自动改变它的直径。

    【讨论】:

    • 我正在使用上面链接的自动调整大小的库。初始化时,它会添加事件 n 东西以自动调整 iframe 的大小以表现得像 div 等。寻找在某些情况下禁用该库的方法
    • 您希望在什么情况下移除 iframe?
    • 我正在制作一个页面构建器。当您在 pc 模式下查看它时,它需要调整大小。当您单击移动设备时,它需要禁用自动调整大小并设置高度/宽度
    猜你喜欢
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多