Angular 7 - How does work the HTML5 Fullscreen API ? I've a lot of errors

后端 未结 1 1008
失恋的感觉
失恋的感觉 2021-02-11 02:16

I use Angular 7 and I would like have a button for put my app in fullscreen. I use the HTML5 Fullscreen API and I\'ve make 2 functions :

1条回答
  •  无人共我
    2021-02-11 02:55

    You can tell typescript about the methods that you're going to use by using the as keyword to cast the interface of document and document.documentElement.

    Like this:

    const docElmWithBrowsersFullScreenFunctions = document.documentElement as HTMLElement & {
      mozRequestFullScreen(): Promise;
      webkitRequestFullscreen(): Promise;
      msRequestFullscreen(): Promise;
    };
    
    const docWithBrowsersExitFunctions = document as Document & {
      mozCancelFullScreen(): Promise;
      webkitExitFullscreen(): Promise;
      msExitFullscreen(): Promise;
    };
    

    Please note that this just prevents compile error and you still should check if the methods exist like you did.

    So your methods will be like this:

    openfullscreen() {
      // Trigger fullscreen
      const docElmWithBrowsersFullScreenFunctions = document.documentElement as HTMLElement & {
        mozRequestFullScreen(): Promise;
        webkitRequestFullscreen(): Promise;
        msRequestFullscreen(): Promise;
      };
    
      if (docElmWithBrowsersFullScreenFunctions.requestFullscreen) {
        docElmWithBrowsersFullScreenFunctions.requestFullscreen();
      } else if (docElmWithBrowsersFullScreenFunctions.mozRequestFullScreen) { /* Firefox */
        docElmWithBrowsersFullScreenFunctions.mozRequestFullScreen();
      } else if (docElmWithBrowsersFullScreenFunctions.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
        docElmWithBrowsersFullScreenFunctions.webkitRequestFullscreen();
      } else if (docElmWithBrowsersFullScreenFunctions.msRequestFullscreen) { /* IE/Edge */
        docElmWithBrowsersFullScreenFunctions.msRequestFullscreen();
      }
      this.isfullscreen = true;
    }
    
    closefullscreen(){
      const docWithBrowsersExitFunctions = document as Document & {
        mozCancelFullScreen(): Promise;
        webkitExitFullscreen(): Promise;
        msExitFullscreen(): Promise;
      };
      if (docWithBrowsersExitFunctions.exitFullscreen) {
        docWithBrowsersExitFunctions.exitFullscreen();
      } else if (docWithBrowsersExitFunctions.mozCancelFullScreen) { /* Firefox */
        docWithBrowsersExitFunctions.mozCancelFullScreen();
      } else if (docWithBrowsersExitFunctions.webkitExitFullscreen) { /* Chrome, Safari and Opera */
        docWithBrowsersExitFunctions.webkitExitFullscreen();
      } else if (docWithBrowsersExitFunctions.msExitFullscreen) { /* IE/Edge */
        docWithBrowsersExitFunctions.msExitFullscreen();
      }
      this.isfullscreen = false;
    }
    

    0 讨论(0)
提交回复
热议问题