Request Full Screen HTML5 Video onPlay

前端 未结 2 1260
忘掉有多难
忘掉有多难 2021-01-06 11:08

I\'m using the following code to trigger fullscreen when a user clicks on the play button on a element:

var video = $(\"#video\");         


        
2条回答
  •  [愿得一人]
    2021-01-06 11:51

    A less verbose way to toggle full screen combining answers from this and other questions.

    This should handle all browser flavours: chromium- and webkit-based, firefox, opera, and MS-based browsers.

    var p = document.querySelector('#videoplayer');
    
    if (!window.isFs) {
        window.isFs = true;
        var fn_enter = p.requestFullscreen || p.webkitRequestFullscreen || p.mozRequestFullScreen || p.oRequestFullscreen || p.msRequestFullscreen;
        fn_enter.call(p);
    } else {
        window.isFs = false;
        var fn_exit = p.exitFullScreen || p.webkitExitFullScreen || p.mozExitFullScreen || p.oExitFullScreen || p.msExitFullScreen;
        fn_exit.call(p);
    }
    

    p represents the DOM object of the video element, and window.isFs is just a random variable for storing the current fullscreen state.

    If your player is a jQuery object then you can get the underlying DOM-element with var p = player.get(0).

提交回复
热议问题