How to exit fullscreen onclick using Javascript?

后端 未结 5 1000
隐瞒了意图╮
隐瞒了意图╮ 2021-01-31 03:14

Not sure if the following code snip will work embedded on SO, as it didn\'t work when pasting it, however it does work stand-alone.

The problem, is I want this to be a t

5条回答
  •  终归单人心
    2021-01-31 03:53

    Solutions provided here are incredibly long. You can use these few lines to show or cancel fullscreen.

    Show full screen:

      /* You can use any HTML element through JS selector functions
       * eg. document.querySelector(".example");
      */
    const element = document; 
    const requestFullScreen = element.requestFullscreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;
    requestFullScreen.call(element);
    

    Cancel full screen:

    // As correctly mentioned in the accepted answer, exitFullscreen only works on document
    const cancellFullScreen = document.exitFullscreen || document.mozCancelFullScreen || document.webkitExitFullscreen || document.msExitFullscreen;
    cancellFullScreen.call(document);
    

    Chrome will display an error: Uncaught (in promise) TypeError: Document not active if exitFullscreen is called while not in fullscreen mode.

提交回复
热议问题