我使用JavaScript构建了自定义视频播放器,并且当我单击“空间”以停止视频时,页面就会掉下来,我需要防止这种情况
我的JavaScript代码
window.onkeydown = vidCtrl;
function vidCtrl(e){
const vid = document.querySelector('video')
const key = e.code;
if (key === 'ArrowLeft') {
vid.currentTime -= 5;
if (vid.currentTime < 0) {
vid.pause();
vid.currentTime = 0;
}
} else if (key === 'ArrowRight') {
vid.currentTime += 5;
if (vid.currentTime > vid.duration) {
vid.pause();
vid.currentTime = 0;
}
} else if (key === 'Space') {
if (vid.paused || vid.ended) {
vid.play();
} else {
vid.pause();
}
}
e.preventDefault()
当用户按下空格键时添加以防止不必要的滚动
else if (key === 'Space') {
e.preventDefault();
if (vid.paused || vid.ended) {
vid.play();
} else {
vid.pause();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句