【发布时间】:2017-03-11 12:19:04
【问题描述】:
我有一个带有iframe 的HTML 页面,我需要在加载时滚动到某个位置。我的示例在除 iOS Safari 之外的大多数浏览器中都能正常工作——这是我真正需要的。它倾向于在第一个页面加载时在 iOS Safari 中工作,但通常之后的任何刷新都会将iframe 内容滚动到顶部。
我已经了解了重定向导致#anchorname 被丢弃的问题,但在这种情况下,情况并非如此。
真正的iframe 内容和主页位于不同的域中。我可以影响iframe 的内容,但我无法控制它。它也用于其他目的,所以我做任何可能干扰它的自定义的能力有限。
此处的实时链接: https://s3.amazonaws.com/ios-iframe-test/iframe_test.html
这是父内容:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<meta content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=0" name="viewport">
<style type="text/css">
#root {
position: relative;
}
#wrapper {
height: 300px;
width: 300px;
overflow-x: hidden;
overflow-y: scroll;
position: relative;
-webkit-overflow-scrolling: touch;
}
iframe {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<h1>Why won't this iframe scroll to where I want?</h1>
<div id="root">
<div id="wrapper">
<iframe src="https://s3.amazonaws.com/ios-iframe-test/iframe_content.html#scrolltome"></iframe>
</div>
</div>
</body>
</html>
这是iframe的内容:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<h1>Have some ipsum...</h1>
<p>
Lorem... (live example has much more here just to make the scrolling apparent)
</p>
<h1 style="color: red;">
<a name="scrolltome" id="scrolltome">This is where I want to scroll></a>
</h1>
<p>
Lorem...
</p>
</html>
【问题讨论】:
-
Safari 在 iframe 上滚动有很多问题。可以用 JS 解决方案吗?
标签: html ios iframe mobile-safari