【发布时间】:2018-10-21 02:04:26
【问题描述】:
以下代码
在计算机中,我可以在滚动 iframe 时通过检测鼠标来禁用正文滚动
在移动设备中,我试图通过检测身体触摸来禁用身体滚动,但不起作用。如果我触摸simulator 之外的点,我可以看到 h1 更改为 auto,如果我触摸 simulator,则 h1 不会更改为 auto。
有什么解决办法吗?
<!DOCTYPE html>
<html>
<body>
<section>
<iframe id="simulator" style="background: #000000;" src="" width="378px" height="1500px" frameborder="0" scrolling="no"></iframe>
<h1 id="toucheventtt">dsds</p>
<script>
function disable_scroll() {
document.body.style.overflow="hidden";
document.getElementById("toucheventtt").innerHTML = "hidden";
}
function enable_scroll() {
document.body.style.overflow="auto";
document.getElementById("toucheventtt").innerHTML = "auto";
}
document.getElementById("simulator").onmouseenter = disable_scroll;
document.getElementById("simulator").onmouseleave = enable_scroll;
document.body.addEventListener('touchstart', function(){
enable_scroll();
}, false)
document.body.addEventListener('touchend', function(){
disable_scroll();
}, false)
</script>
</section>
</body>
</html>
另外添加以下代码仅适用于计算机。
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style>
html, body {
overflow-y: hidden;
}
body {
position: relative;
}
</style>
</head>
【问题讨论】:
标签: javascript html iframe document-body