如何检测全屏事件发生时,按f11键



如何检测全屏事件发生时,按f11键。如果发生,这意味着屏幕处于全屏模式,然后显示一个警告。没有使用键码比较。请给我一个不同的方法。

// mozilla proposal
element.requestFullScreen();
document.cancelFullScreen(); 
// Webkit (works in Safari and Chrome Canary)
element.webkitRequestFullScreen(); 
document.webkitCancelFullScreen(); 
// Firefox (works in nightly)
element.mozRequestFullScreen();
document.mozCancelFullScreen(); 
// W3C Proposal
element.requestFullscreen();
document.exitFullscreen();

element.addEventListener('fullscreeneventchange', function(e) {
    if (document.fullScreen) {
       /* make it look good for fullscreen */
    } else {
       /* return to the normal state in page */
    }
}, true);
window.onresize = function (event) {
    var maxHeight = window.screen.height,
        maxWidth = window.screen.width,
        curHeight = window.innerHeight,
        curWidth = window.innerWidth;
    if (maxWidth == curWidth && maxHeight == curHeight) {
        // do something
    }
}

或者,你也可以尝试window.screen.availWidth &window.screen. availheight(注意,这些可能实际上不是屏幕分辨率,但允许菜单栏的宽度/浏览器窗口尺寸等)

如果您想在F11按键上检查全屏,然后尝试以下操作:

演示小提琴/全屏

function checkWH(){
    if((window.outerWidth-screen.width) ==0 && (window.outerHeight-screen.height) ==0 )
    {
        alert('fullscreen');
    }
}
$(window).keypress(function(event){
    var code = event.keyCode || event.which;
    if(code == 122){
        setTimeout(function(){checkWH();},1000);
    }
});

最新更新