我有一些自定义的html5视频控件,我想在用户观看视频时隐藏它们。我等了两秒钟,如果他们没有移动鼠标,我会隐藏控件,然后当他们再次移动鼠标时,我会显示控件。
这是一种实用,有效的方法吗?
答案 0 :(得分:1)
将
一起使用mousemove
事件与setTimeout
如果setTimeout
正在移动
mouse
var elem = document.getElementById('controls');
var timeout;
var duration = 3000;
document.addEventListener('mousemove', function() {
elem.textContent = 'Mouse is moving!'
clearTimeout(timeout);
timeout = setTimeout(function() {
elem.textContent = 'Mouse Has stopped!'
}, duration)
});

<div id="controls">Mouse Has stopped!</div>
&#13;
使用controls
属性
var video = document.getElementById('videoElem');
var timeout;
var duration = 500;
document.addEventListener('mousemove', function() {
video.setAttribute("controls", "controls");
clearTimeout(timeout);
timeout = setTimeout(function() {
video.removeAttribute("controls");
}, duration)
});
&#13;
html {
padding: 20px 0;
background-color: #efefef;
}
body {
width: 400px;
padding: 40px;
margin: 0 auto;
background: #fff;
box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.5);
}
video {
width: 400px;
display: block;
}
&#13;
<video preload controls id="videoElem">
<source id="mp4" src="http://grochtdreis.de/fuer-jsfiddle/video/sintel_trailer-480.mp4" type="video/mp4">
</video>
&#13;
答案 1 :(得分:0)
我最终使用了timeupdate事件:
<Setter .../>