iphone - 阻止 Mobile Safari 中 <video> 元素中的 touchmove 事件

标签 iphone webkit mobile-safari html5-video

我正在 Mobile Safari 中编写一个带有视频元素的主屏幕 Web 应用程序。出于可用性原因,我希望在屏幕上滑动不会使页面滚动。我可以使用以下代码成功阻止滑动滚动。但是,如果用户在视频元素上滑动,页面就会滚动。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width; height=device-height; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;"/> 
<meta name="apple-mobile-web-app-capable" content="yes" /> 
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>no scroll</title>
<script type="text/javascript">
window.onload = function(){
    document.addEventListener('touchmove',function(event){
        event.preventDefault();
    },false);
};
</script>
</head>
<body>
<p>anything but the video will be stationary when swiped.</p>
<video preload="auto" webkit-playsinline id="video" controls height="100" width="100">
    <source src="video.mp4" type="video/mp4" />
</video>
</body>
</html>

我尝试添加代码以防止视频元素吞噬这些事件(插入到 window.onload 函数中):

var videoEl = document.getElementById('video');
videoEl.addEventListener('touchmove',function(event){
    event.preventDefault();
},false);
videoEl.addEventListener('touchstart',function(event){
   event.preventDefault();
},false);

有解决办法吗?我是否以错误的方式解决问题?

最佳答案

我建议在顶部放置一个垫片,即具有较高 z-index 的空白 div 来捕获触摸事件。我现在没有机会测试,但应该很容易尝试。

关于iphone - 阻止 Mobile Safari 中 <video> 元素中的 touchmove 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5970497/

相关文章:

ios - 从 bundle 中调用 sqlite 数据库

java - 从 Java 代码将事件发送到 JavaFx webview 节点/元素

android - 如何将列表项内的跨度居中

javascript - 模拟 Webkit 的元素检查器

html - 在 HTML 数字输入中显示尾随的小数零

iphone - 分析,泄漏工具导致仪器崩溃

iphone - 是否有一个通用的媒体查询来设置所有 iphone 4、4s、5 的样式

ios - iPad Safari - 让键盘消失

html - 如何去除 iPhone/iOS 上电话号码的蓝色样式?

ios - 如何为曲线制作动画