iOS上的IFrame高度问题(手机游戏)

sua*_*kim 23 javascript iphone iframe mobile-safari ios

示例页面源:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>Page</title>
</head>
<body>
<div class="main" style="height: 100%;">
    <div class="header" style="height: 100px; background-color: green;"></div>

    <iframe src="http://www.wikipedia.com"
            style="height: 200px; width: 100%; border: none;"></iframe>

    <div class="footer" style="height: 100px; background-color: green;"></div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

问题是,height200px距离的IFrame内联样式是在移动Safari忽略:

在此输入图像描述

另外,我想通过vanilla JavaScript动态更改IFrame的高度,而这些JavaScript根本无法使用以下代码:

document.getElementsByTagName('iframe')[0].style.height = "100px"
Run Code Online (Sandbox Code Playgroud)

height根据开发工具正确更改样式的值,但它被忽略,因为IFrame的实际渲染高度不会改变.

这似乎只是移动Safari中的一个问题,并且在最新版本的桌面Safari,Firefox,Chrome,Androids WebView等上正常运行.

测试页:http://devpublic.blob.core.windows.net/scriptstest/index.html

Ps.:我在browserstack上使用各种设备对此进行了测试,并且还在那里截取了屏幕截图,因为我手头没有实际的iDevice.

小智 14

它看起来像这样:如何让IFrame在iOS Safari中响应?

iFrame仅在iOS上存在问题,因此您必须调整iframe.

你可以把一个包裹iframe的div,在iframe上设置css,以及对我有用的是添加:put属性scrolling ='no'.

祝你好运.


Fre*_* Yu 11

我遇到了同样的问题.在尝试了我能找到的所有解决方案之后,我终于找到了如何解决它.

这个问题是由iOS Safari引起的,它会自动消耗iframe的高度来适应里面的页面内容.

如果您将scrolling ='no'属性设置为iframe <iframe scrolling='no' src='content.html'>,则可以解决此问题,但iframe无法显示页面的完整内容,超出框架的内容将被剪切.

所以我们需要放一个div包装iframe,并处理它中的scroll事件.

<style>
.demo-iframe-holder {
  width: 500px;
  height: 500px;
  -webkit-overflow-scrolling: touch;
  overflow-y: scroll;
}

.demo-iframe-holder iframe {
  height: 100%;
  width: 100%;
}
</style>

<html>
<body>
    <div class="demo-iframe-holder">
        <iframe src="content.html" />
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

引用:

https://davidwalsh.name/scroll-iframes-ios

如何让IFrame在iOS Safari中响应?

希望能帮助到你.