标签搜索

Javascript中使用Youtube

sunshine
2022-11-12 / 0 评论 / 19 阅读
温馨提示:
本文最后更新于2022年11月12日,已超过740天没有更新,若内容或图片失效,请留言反馈。

CSS部分

<style>
  * {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }

  :root {
    --container-title-fontsize: 30px;
    --article-content-fontsize: 14px;
    --article-link-fontsize: 13px;
    --video-title-fontsize: 14px;
    --colorTitle: #000;
  }
  .video-container {
    width: 99%;
    margin: auto;
  }

  .video-container-title {
    width: 100%;
    text-align: center;
    font-size: var(--container-title-fontsize);
    font-weight: bold;
    margin-top: 50px;
    color:var(--colorTitle);
  }

  .video-group {
    display: flex;
    justify-content: space-between;
    margin: 10px 0;
    flex-wrap: wrap;
  }

  .video-group:after {
    width: calc(100% / 3 - 20px);
    content: '';
    height: 0;
    visibility: hidden;
  }

  .video-item {
    width: calc(100% / 3 - 20px);
    margin-top: 30px;
  }

  .video-content {
    width: 100%;
    text-align: center;
  }

  .video-title {
    font-size: var(--video-title-fontsize);
  }

  .youtube {
    background-color: #000;
    margin-bottom: 20px;
    position: relative;
    padding-top: 56.25%;
    overflow: hidden;
    cursor: pointer;
  }

  .youtube .play-button, .youtube .play-button:before {
    top: 50%;
    left: 50%;
    transform: translate3d(-50%, -50%, 0);
  }

  .youtube img, .youtube iframe, .youtube .play-button, .youtube .play-button:before {
    position: absolute;
  }

  .youtube img, .youtube .play-button {
    cursor: pointer;
  }

  .youtube img {
    width: 100%;
    top: 0;
    left: 0;
    opacity: 0.8;
  }

  .youtube .play-button {
    width: 90px;
    height: 60px;
    background-color: #333;
    box-shadow: 0 0 30px rgb(0 0 0 / 60%);
    z-index: 1;
    opacity: 0.8;
    border-radius: 6px;
  }

  .youtube .play-button:before {
    content: "";
    border-style: solid;
    border-width: 15px 0 15px 26px;
    border-color: transparent transparent transparent #fff;
  }

  .youtube .play-button:hover {
    background-color: #f00;
  }

  .youtube iframe {
    height: 100%;
    width: 100%;
    top: 0;
    left: 0;
  }

  [data-max="false"] img {
    top: -47px;
  }
  </style>

Html部分

<div class="video-container">
  <div class="video-container-title">
    VIDEO REVIEW
  </div>
  <div class="video-group">
    <div class="video-item">
      <div class="video-content">
        <div class="youtube" data-embed="cUdfHgzvHoQ" data-max="false">
          <div class="play-button"></div>
        </div>
      </div>
      <p class="video-title">Full review of EnjoyBot 12v 100Ah LFP battery,Full review of EnjoyBot 12v 100Ah LFP
        battery</p>
    </div>
    <div class="video-item">
      <div class="video-content">
        <div class="youtube" data-embed="0jErmH2iEG4" data-max="false">
          <div class="play-button"></div>
        </div>
      </div>
      <p class="video-title">EnjoyBot 12V 100Ah LiFePO4 Battery Review, Super Cheap, $256/kWh!</p>
    </div>
  </div>
</div>

JS部分

<script>
    var youtube = document.querySelectorAll(".youtube");
    // loop
    for (var i = 0; i < youtube.length; i++) {
        var source = '';
        // thumbnail image source.
        if (youtube[i].dataset.max == 'false') {
            source = "https://img.youtube.com/vi/" + youtube[i].dataset.embed + "/0.jpg";
        } else {
            source = "https://img.youtube.com/vi/" + youtube[i].dataset.embed + "/maxresdefault.jpg";
        }

        var image = new Image();
        image.src = source;
        image.addEventListener("load", function () {
            youtube[i].appendChild(image);
        }(i));

        youtube[i].addEventListener("click", function () {
            console.log('click')

            var iframe = document.createElement("iframe");

            iframe.setAttribute("frameborder", "0");
            iframe.setAttribute("allowfullscreen", "");
            iframe.setAttribute("src", "https://www.youtube.com/embed/" + this.dataset.embed + "?rel=0&autoplay=1&showinfo=1");

            this.innerHTML = "";
            this.appendChild(iframe);
        });

    }


</script>
感觉很棒,欢迎点赞 OR 打赏~
0
分享到:

评论 (0)

取消