Prevent Esc Key from Exiting FullScreen App Mode on Website

 ̄綄美尐妖づ 提交于 2019-12-01 04:13:26

问题


I'm developing a custom "lightbox" photo gallery. I'm adding key event "listeners" (left>prev, right>next, esc>close), and I'm having the issue of Safari (or any fullscreen mode browser) exiting fullscreen mode on escape. My site works fine, just the viewer then has to resume fullscreen mode for Safari (and others).

Now, I know there's a way to bypass this. Facebook for instance, while looking at a photo in the "theatre" mode, can be closed with the esc key without causing my browser to exit fullscreen.

Any thoughts?


回答1:


Practically you cannot disable ESC key for browsers as that is a compromise for user to at least have some control over their browser. However, you do have a way to bind an eventlistener to your element field (say your lightbox div) to catch the Event when Esc is pressed and suppress it, like below: (so when people press ESC over the gallery div it will not exit browser fullscreen)

document.querySelector("div.lightbox").addEventListener("keydown",function(e){
    var charCode = e.charCode || e.keyCode || e.which;
    if (charCode == 27){
         alert("Escape is not suppressed for lightbox!");
        return false;
    }
});

Also many browsers toggle fullscreen mode with F11 key now.



来源:https://stackoverflow.com/questions/20916109/prevent-esc-key-from-exiting-fullscreen-app-mode-on-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!