Auto-Full Screen for a Youtube embed

前端 未结 4 875
清酒与你
清酒与你 2020-11-30 02:38

I have a Youtube video embeded on a webpage.

Is it possible to have the video go full screen when the user presses play, using the HTML5 iframe with Youtube\'s API?

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 03:07

    Update November 2013: this is possible - real fullscreen, not full window, with the following technique. As @chrisg says, the YouTube JS API does not have a 'fullscreen by default' option.

    • Create a custom play button
    • Use YouTube JS API to play video
    • Use HTML5 fullscreen API to make element fullscreen

    Here's the code.

    var $ = document.querySelector.bind(document);
    
    // Once the user clicks a custom fullscreen button
    $(playButtonClass).addEventListener('click', function(){
      // Play video and go fullscreen
      player.playVideo();
    
      var playerElement = $(playerWrapperClass);
      var requestFullScreen = playerElement.requestFullScreen || playerElement.mozRequestFullScreen || playerElement.webkitRequestFullScreen;
      if (requestFullScreen) {
        requestFullScreen.bind(playerElement)();
      }
    })
    

提交回复
热议问题