YouTube Player APIを使用してYouTube動画をフルスクリーンで表示するオリジナルボタンを実装する方法のデモページ

※デベロッパーツール等でソースコードをご確認ください(Please check using Developer tools)

マイナビAGENT

View in full screen

スポンサーリンク

マイナビAGENT

CSS

.wrap {
  width: 640px;
  margin: 50px auto 0;
}

.movie {
  position: relative;
  width: 100%;
  padding-bottom: 56.25%;
}

.movie iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.btn {
  width: 300px;
  margin: 30px auto 0;
  padding: 20px 0;
  text-align: center;
  cursor: pointer;
  color: #fff;
  font-weight: bold;
  background: #c00;
}

@media screen and (max-width:640px) {
  .wrap {
    width: 100%;
  }
}
  

JavaScript

var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player, iframe;
var $ = document.querySelector.bind(document);

function onYouTubeIframeAPIReady() {
  player = new YT.Player('player', {
    videoId: 'QQ63-VVhWss',
    events: {
      'onReady': onPlayerReady
    }
  });
}

function onPlayerReady(event) {
  var player = event.target;
  iframe = $('#player');
  setupListener();
}

function setupListener (){
$('.btn').addEventListener('click', playFullscreen);
}

function playFullscreen (){
  player.playVideo();
  var requestFullScreen = iframe.requestFullScreen || iframe.mozRequestFullScreen || iframe.webkitRequestFullScreen;
  if (requestFullScreen) {
    requestFullScreen.bind(iframe)();
  }
}