Javascript 触摸事件在 Mobile Safari 中不起作用
Javascript touch event not working in Mobile Safari
我试图限制用户在触摸 iframe 时滚动。因此,如果他们触摸 body,他们就可以滚动。
想知道为什么下面的代码在 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; }
似乎触摸事件工作正常但溢出不是。
我不确定您的 html 是什么样子,但是您可以试试这些:
隐藏:
body {
/* position can be fixed */
position:relative;
overflow-y:hidden;
}
自动:
body {
-webkit-overflow-scrolling: touch;
overflow: auto;
}
Safari iOS 的主要问题是 iframe
标签被视为某种响应元素,并且对其大小和包含的元素(已加载 HTML) 尺寸。我使用带有真实内容的 iframe
进行了测试,因此它可以完全滚动。使用与示例中相同的代码,Safari iOS 显示 iframe
包含的 html 内容事件的完整高度,并定义了 width
和 height
。
要解决此问题,您需要将 iframe
包含在 block
容器中,然后设置块容器大小(width
和 height
)和 overflow
到 auto
,并将 vendor 属性添加到主体以允许 iframe
正确滚动。另外,不要忘记将 iframe 设置为可滚动。
您可以放弃 js
实施。
我在每个桌面浏览器、Safari iOS 和移动 Chrome 上测试了这个。
当讨论 iframe 中的响应式内容时,您可能还想查看 this link。
希望对您有所帮助。
主要html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style>
body {
-webkit-overflow-scrolling: touch;
}
.iframeContainer, iframe {
width: 200px;
height: 200px;
}
.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" frameborder="0" src="scrolling.html" scrolling="yes"></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>
已加载 iframe
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style>
body {
background-color: black;
color: white;
}
</style>
</head>
<body>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
</body>
</html>
我试图限制用户在触摸 iframe 时滚动。因此,如果他们触摸 body,他们就可以滚动。
想知道为什么下面的代码在 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; }
似乎触摸事件工作正常但溢出不是。
我不确定您的 html 是什么样子,但是您可以试试这些:
隐藏:
body { /* position can be fixed */ position:relative; overflow-y:hidden; }
自动:
body { -webkit-overflow-scrolling: touch; overflow: auto; }
Safari iOS 的主要问题是 iframe
标签被视为某种响应元素,并且对其大小和包含的元素(已加载 HTML) 尺寸。我使用带有真实内容的 iframe
进行了测试,因此它可以完全滚动。使用与示例中相同的代码,Safari iOS 显示 iframe
包含的 html 内容事件的完整高度,并定义了 width
和 height
。
要解决此问题,您需要将 iframe
包含在 block
容器中,然后设置块容器大小(width
和 height
)和 overflow
到 auto
,并将 vendor 属性添加到主体以允许 iframe
正确滚动。另外,不要忘记将 iframe 设置为可滚动。
您可以放弃 js
实施。
我在每个桌面浏览器、Safari iOS 和移动 Chrome 上测试了这个。
当讨论 iframe 中的响应式内容时,您可能还想查看 this link。
希望对您有所帮助。
主要html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style>
body {
-webkit-overflow-scrolling: touch;
}
.iframeContainer, iframe {
width: 200px;
height: 200px;
}
.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" frameborder="0" src="scrolling.html" scrolling="yes"></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>
已加载 iframe
:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<style>
body {
background-color: black;
color: white;
}
</style>
</head>
<body>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
<p>hello</p>
</body>
</html>