目前正在开发一个视频播放器,如果视频是全屏的,则添加特定的样式类。如果用户使用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;
}
});
暮色呼如
拉风的咖菲猫
相关分类