Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://www.youtube.com/iframe_api" defer></script>

<div class="mesBox">
  <div id="mes"></div>
</div>

<main>
  <div class="mb-y">
    <div class="embed-youtube">
      <div id="sample01" data-movieid="HKThn7Phxkk" class="ytPlayerReady">
        <img src="https://img.youtube.com/vi/HKThn7Phxkk/mqdefault.jpg" alt="" width="640" height="390">
      </div>
    </div>

    <div class="controller" data-playerid="sample01" style="display:none">
      <p>
        <span class="y_start">スタート</span> |
        <span class="y_pause">一時停止</span>
      </p>
    </div>
  </div>

  <div class="mb-y">
    <div class="embed-youtube">
      <div id="sample02" data-movieid="jmhKf2nh2ZA" class="ytPlayerReady">
        <img src="https://img.youtube.com/vi/jmhKf2nh2ZA/mqdefault.jpg" alt="" width="640" height="390">
      </div>
    </div>

    <div class="controller" data-playerid="sample02" style="display:none">
      <p>
        <span class="y_start">スタート</span> |
        <span class="y_pause">一時停止</span>
      </p>
    </div>
  </div>

  <div class="mb-y">
    <div class="embed-youtube">
      <div id="sample03" data-movieid="KoHltPzjs3U" class="ytPlayerReady">
        <img src="https://img.youtube.com/vi/KoHltPzjs3U/mqdefault.jpg" alt="" width="640" height="390">
      </div>
    </div>
    <div class="controller" data-playerid="sample03" style="display:none">
      <p>
        <span class="y_start">スタート</span> |
        <span class="y_pause">一時停止</span>
      </p>
    </div>
  </div>

  <div class="mb-y">
    <div class="embed-youtube">
      <div id="sample04" data-movieid="4niQ-lCTTv4" class="ytPlayerReady">
        <img src="https://img.youtube.com/vi/4niQ-lCTTv4/mqdefault.jpg" alt="" width="640" height="390">
      </div>
    </div>
    <div class="controller" data-playerid="sample04" style="display:none">
      <p>
        <span class="y_start">スタート</span> |
        <span class="y_pause">一時停止</span>
      </p>
    </div>
  </div>
</main>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


              
            
!

CSS

              
                    html,
    body {
/*       width: 100%; */
      box-sizing: border-box;
    }

    main {
      width: 100%;
      max-width: 780px;
      margin: 0 auto 20px;
      padding: 0 10px;
      box-sizing: border-box;
    }

    p,
    pre {
      margin-bottom: 10px;
      line-height: 1.6;
      overflow: hidden;
    }

    .mesBox {
      font-size: 80%;
      background: #eee;
      padding: 10px;
      margin: 10px 0 0;
      max-height: 25%;
      left: 50%;
      transform: translate(-50%, -0);
      overflow: auto;
      width: 760px;
      max-width: 100%;
      box-sizing: border-box;
      position: fixed;
      bottom: 0;
      z-index: 9999;
    }

    .embed-youtube {
      position: relative;
      height: 0;
      padding-top: 56.25%;
    }

    .embed-youtube iframe,
    .embed-youtube img {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }

    .ytPlayerReady {
      cursor: pointer;
    }

    .ytPlayerReady img {
      border-radius: 5px;
    }

    .ytPlayerReady:before {
      position: absolute;
      content: "";
      color: #fff;
      text-align: center;
      font-size: 22px;
      font-weight: 700;
      line-height: 180px;
      background: rgba(0, 0, 0, .6);
      border-radius: 5px;
      top: 0;
      left: 0;
      width: 780px;
      max-width: 100%;
      height: 100%;
      z-index: 10;
      /* transition: all .3s; */
      transition: .5s;
      background-image: url(./images/yt_icon_mono_light.png);
      background-repeat: no-repeat;
      background-position: center;
      background-size: 80px;
    }

    .ytPlayerReady:hover::before {
      background-image: url(./images/yt_icon_rgb.png);
    }

    .controller {
      margin: 10px auto;
      text-align: center;
    }

    .controller .y_start,
    .controller .y_pause {
      cursor: pointer;
      text-decoration: underline;
      color: rgb(64, 31, 253);
    }

    iframe {
      max-width: 100%;
    }

    .mb-y {
      margin-bottom: 400px;
    }
              
            
!

JS

              
                // 各プレーヤーの格納
var ytPlayer = [];
// プレーヤーのサイズ
var ytWidth = 640;
var ytHeight = 390;

var ua = navigator.userAgent;
if (ua.indexOf('iPhone') > 0 || ua.indexOf('iPad') > 0 || ua.indexOf('iPod') > 0) {
  var os = "ios";
} else if (ua.indexOf('Android') > 0) {
  var os = "android";
} else {
  var os = "pc";
}
logmes("OS:" + os);

// iOSはlazyload的に処理
if (os == "ios") {
  var thisOffset = [];
  $(window).on('load', function() {
    //要素の位置取得
    $(".ytPlayerReady").each(function(i, elm) {
      thisOffset[i] = [];
      thisOffset[i]["playerId"] = $(elm).attr("id");
      thisOffset[i]["movieId"] = $(elm).attr("data-movieid");
      thisOffset[i]["ytWidth"] = ytWidth;
      thisOffset[i]["ytHeight"] = ytHeight;
      thisOffset[i]["height"] = $(elm).offset().top + $(elm).outerHeight();
      thisOffset[i]["lazyStatus"] = false;

      logmes("位置:" + $(elm).attr("id") + ":" + thisOffset[i]["height"]);
    });

    // console.log(thisOffset);

    //スクロール開始前にも、ファーストビュー中に動画があったらYouTube起動処理
    youtubeLazy(ytPlayer, thisOffset);
  });

  //lazyでのYouTube起動処理 スクロール中
  $(window).scroll(function() {
    youtubeLazy(ytPlayer, thisOffset);
  });
}

function youtubeLazy(ytPlayer, thisOffset) {
  var nowScrollTop;
  for (var i = 0; i < thisOffset.length; i++) {

    nowScrollTop = $(window).scrollTop() + $(window).height();

    if (nowScrollTop > thisOffset[i]["height"] && !thisOffset[i]["lazyStatus"]) {
      logmes("lazyload:" + thisOffset[i]["playerId"]);

      $("#" + thisOffset[i]["playerId"]).removeClass("ytPlayerReady");
      $("#" + thisOffset[i]["playerId"]).parent().parent().find(".controller").show();

      youTubeIframeAPIReady(ytPlayer, thisOffset[i]["playerId"], thisOffset[i]["movieId"], thisOffset[i]["ytWidth"], thisOffset[i]["ytHeight"]);

      thisOffset[i]["lazyStatus"] = true;
    }
  }
}

//画像クリックでのYouTube起動処理
$(".ytPlayerReady").click(function() {
  var playerId = $(this).attr("id");
  var movieId = $(this).attr("data-movieid");
  $(this).removeClass("ytPlayerReady");
  $(this).parent().parent().find(".controller").show();

  // logmes("---");
  logmes("起動クリック:" + playerId + ":" + movieId);

  youTubeIframeAPIReady(ytPlayer, playerId, movieId, ytWidth, ytHeight);

});

$(".y_start").click(function() {
  // logmes("---");
  // logmes("■ボタン:" + playerId + ":playVideo");
  var playerId = $(this).parents('.controller').data("playerid");

  videoControl("playVideo", playerId);
  // playVideo();
});
$(".y_pause").click(function() {
  // logmes("---");
  // logmes("■ボタン:" + playerId + ":pauseVideo");
  var playerId = $(this).parents('.controller').data("playerid");

  videoControl("pauseVideo", playerId);
});

// 各プレーヤーの埋め込み
function youTubeIframeAPIReady(ytPlayer, playerId, movieId, ytWidth, ytHeight) {
  ytPlayer[playerId] = new YT.Player(playerId, {
    width: ytWidth,
    height: ytHeight,
    videoId: movieId,
    playerVars: {
      rel: 0
    },
    events: {
      'onReady': onPlayerReady,
      'onStateChange': onPlayerStateChange
    }
  });

}

// 各プレーヤー準備完了後の処理
function onPlayerReady(event) {
  playerId = event.target.getIframe().id;
  logmes(playerId + ":onPlayerReady");

  if (os != "ios") {
    // videoControl("mute", playerId);
    videoControl("playVideo", playerId);
  }

}

function onPlayerStateChange(event) {
  playerId = event.target.getIframe().id;
  // logmes("---");
  logmes(playerId + ":onPlayerStateChange");
  playerStateView(event, playerId);
}

function playerStateView(event, playerId) {
  var ytStatus = ytPlayer[playerId].getPlayerState();

  if (ytStatus == 1) {
    logmes('ステータス:再生中');
  } else if (ytStatus == 0) {
    logmes('ステータス:終了');
  } else if (ytStatus == 2) {
    logmes('ステータス:一時停止中');
  } else if (ytStatus == 3) {
    logmes('ステータス:バッファリング中');
  } else if (ytStatus == 5) {
    logmes('ステータス:頭出し済み');
  } else if (ytStatus == -1) {
    logmes('ステータス:未開始');
  } else {
    logmes('ステータス:該当なし?ログ確認');
    console.log(ytStatus);
  }
}

function videoControl(action, playerId) {
  // playVideo or pauseVideo or mute or unMute

  logmes("操作:" + playerId + ":" + action);
  var $playerWindow = $("#" + playerId)[0].contentWindow;
  if ($playerWindow) {
    $playerWindow.postMessage('{"event":"command","func":"' + action + '","args":""}', '*');
  }
}

function logmes(log) {
  if (log != "") {
    var mes = $("#mes").html();
    mes = mes + "<p>" + log + "</p>";
    $("#mes").html(mes);

    var scrollPoint = $('#mes')[0].scrollHeight;

    // $(".mesBox").scrollTop(scrollPoint);
    $('.mesBox').animate({
      scrollTop: $('#mes')[0].scrollHeight
    });
  }
}

              
            
!
999px

Console