【发布时间】:2018-10-24 18:50:17
【问题描述】:
我试图限制用户在触摸 iframe 时滚动。所以,如果他们接触到身体,他们就可以滚动。
想知道为什么下面的代码在 Mobile Chrome 中运行良好,但在 Mobile Safari 中无法运行。有什么办法可以为 safari 解决这个问题吗?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
.overflowHidden {
position:relative;
overflow-y:hidden;
}
.overflowAuto {
-webkit-overflow-scrolling: touch;
overflow: auto;
}
</style>
</head>
<body>
<section>
<p>hello</p><p>hello</p><p>hello</p><p>hello</p><p>hello</p><p>hello</p><p>hello</p>
<iframe id="appSimulator" style="background: #000000;" width="189px" height="400px" frameborder="0" scrolling="no"></iframe>
<p>hello</p><p>hello</p><p>hello</p><p>hello</p><p>hello</p><p>hello</p><p>hello</p>
</section>
<script type="text/javascript">
document.body.addEventListener('touchstart', function(){
document.body.style.overflow="auto";
$('body').removeClass('overflowHidden');
$('body').addClass('overflowAuto');
}, false)
document.body.addEventListener('touchend', function(){
document.body.style.overflow="hidden";
$('body').removeClass('overflowAuto');
$('body').addClass('overflowHidden');
}, false)
</script>
</body>
</html>
编辑
移动 Chrome 的示例 - 这就是我想要的 Safari 移动版
谢谢。
编辑 2
感谢 muecas 的帮助。
这是来自 Safari Mobile 的当前结果
当前代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style>
body {
-webkit-overflow-scrolling: touch;
}
.iframeContainer, iframe {
width: 189px;
height: 405px;
}
.iframeContainer {
overflow: auto;
}
</style>
</head>
<body>
<section>
<p>hello</p><p>hello</p><p>hello</p><p>hello</p><p>hello</p><p>hello</p><p>hello</p>
<div class="iframeContainer">
<iframe id="appSimulator" src="https://appetize.io/embed/keyyyyyyy?device=iphone5s&scale=50&autoplay=false&orientation=portrait&deviceColor=black&language=zh-Hant" frameborder="0" scrolling="no"></iframe>
</div>
<p>hello</p><p>hello</p><p>hello</p><p>hello</p><p>hello</p><p>hello</p><p>hello</p>
</section>
</body>
</html>
如果我设置.iframeContainer { overflow: hidden; }
【问题讨论】:
-
该代码,如果设法让它工作,将阻止页面上的所有滚动,无论您是否触摸 iframe。 iframe 上的
touch不会在主html 中触发touch。所以问题是,问题不在于溢出html或挂钩到iframe 事件以防止滚动?还是两者兼而有之? -
@muecase 当我尝试使用 Chrome 浏览器在移动设备中测试此代码时,如果我触摸 iFrame 并向上或向下滚动,则正文视图不会滚动。但是当我在移动设备上使用 Safari 执行此操作时,没有任何事情发生,就像只是向上或向下滚动而没有事件。我遇到的问题是我在 iFrame 中有一个滚动视图。这是我要嵌入的东西。 appetize.io 当我尝试滚动他们的模拟器时,只有身体会滚动。模拟器 (iFrame) 中的视图不起作用。
-
容器 html 必须是可滚动的?原因是您在 Chrome 中的示例,您也无法滚动主 html。
-
在 chrome mobile 中,我可以滚动。请看我的编辑
-
感谢您提供的图形示例。我会尽力帮助你。
标签: javascript iframe overflow addeventlistener