1

我正在制作页面构建器类型的东西。对于普通视图,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 时不要调整大小,然后在消息进入时相应地调整大小。

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

4

1 回答 1

0

我认为我没有 100% 理解你的问题。但我试图尽我所能回答你的问题。

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

你可以这样做:<iframe href="https://google.com" width="100%" height="100%">Your Browser Does Not Support Iframe</iframe>.

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

您还可以使用 php 自动更改它的直径。

于 2017-12-22T10:46:29.027 回答