iOS iframe 页面加载后无法滑动

这儿有一个页面 page.html 需要在iframe中加载,加载完成后,高度超过iframe外部区域高度,所以需要滑动iframe内容才能完整显示。

代码如下:

<div class="outer-box">
    <iframe src="page.html" frameborder="0"></iframe>
</div>

问题:

Chrome、Android系统中操作正常,iOS系统中 iframe 无法滑动,超过外框 outer-box 的内容无法显示。

解决办法:

在 iframe 加载完成后,重新设置iframe的高度

<div class="outer-box">
    <iframe id="iframe" name="iframe" src="page.html" frameborder="0" scrolling="yes" onLoad="resetFrameHeight()"></iframe>
</div>


function resetFrameHeight() {
    var ios = navigator.userAgent.toLowerCase().match(/ipad|ipod|iphone/);
    if (ios) {
        var iframe = document.getElementById("iframe");
        var subContent = document.frames ? document.frames["iframe"].document : iframe.contentDocument;
        if (iframe != null && subContent != null) {
            iframe.height = subContent.body.scrollHeight;
        }
    }
}

如需转载,请注明出处: https://www.chadou.me/p/207

最新发布