【发布时间】:2016-08-31 23:05:34
【问题描述】:
我有一个视频 (<video id="video" controls src="foo.webm"></video>),通过这个视频我有一个 onclick 和 ondblclick 集。在 Chrome 上这工作正常,但在 Firefox 上,他们的“点击切换播放状态”和“双击全屏”会干扰我设置的代码。有没有办法从 Javascript 中禁用这些“功能”?
我已经尝试过event.preventDefault() 和return false,但这些对ondblclick 根本不起作用,并且部分适用于onclick。部分工作,这意味着他们完全禁用所有控件,您甚至无法单击视频左下方的播放/暂停按钮来播放/暂停它。
HTML
<div class="videocontainer">
<div class="close">X</div>
<video id="video" controls src="foo.webm"></video>
</div>
JavaScript
var video = document.getElementById('videocontainer');
video.onclick = function(ev) {
ev.preventDefault(); //Disables all click events, including play/pause button
if (ev.target.getAttribute('class') === close) closeVideo(); //if you clicked the close button it will close the video
return false; //Disables all click events, including play/pause button
};
video.ondblclick = function(ev) {
ev.preventDefault(); //doesn't change anything
if (ev.target.nodeName.toLowerCase() === 'video') popoutVideo(); //if you double clicked the video itself it will make the video fixed at the bottom right of the screen
return false; //doesn't change anything
};
【问题讨论】:
-
您可以隐藏原生控件并提供自己的控件。
-
@Oriol 我尽量避免这样做
-
那么你可以使用点击的坐标来判断一个控件是否被点击了,但这并不可靠。
标签: javascript firefox