如果视频是全屏的,目前正在开发一个添加了特定样式类的视频播放器。如果用户使用esc而不是全屏按钮退出,则样式保持不变。
/* View in fullscreen */
function openFullscreen(elem) {
if (elem.requestFullscreen) {
elem.requestFullscreen();
} else if (elem.webkitRequestFullscreen) { /* Safari */
elem.webkitRequestFullscreen();
} else if (elem.msRequestFullscreen) { /* IE11 */
elem.msRequestFullscreen();
}
video.classList.add('video-fullscreen');
}
/* Close fullscreen */
function closeFullscreen() {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) { /* Safari */
document.webkitExitFullscreen();
} else if (document.msExitFullscreen) { /* IE11 */
document.msExitFullscreen();
}
video.classList.remove('video-fullscreen');
}
let fullscreen = false;
//Toggle fullscreen
function toggleFullScreen(){
!fullscreen ? openFullscreen(player) : closeFullscreen();
fullscreen = !fullscreen;
}
我为esc按钮尝试了一个事件侦听器,因此我可以使用它来更改样式,第一次 esc 按下关闭,第二次执行我的代码,非常烦人:
//detect Escape key press
window.addEventListener("keydown", (e) => {
if(e.key === "Escape" && fullscreen){
e.preventDefault();
closeFullscreen();
fullscreen = !fullscreen;
}
});