【问题标题】:Can't Defer Youtube CSS with JavaScript无法使用 JavaScript 延迟 Youtube CSS
【发布时间】:2019-01-25 19:05:16
【问题描述】:

出于性能考虑,我正在尝试推迟在我的网站上加载 YouTube CSS 和 JavaScript。但是,使用 Google 的 Page Speed Insights 对我的页面进行扫描表明 YouTube CSS 文件 https://www.youtube.com/yts/cssbin/www-player-sprite-mode-vflixBY8E.css 没有被推迟。我还测试了删除 JavaScript 以确保 CSS 文件没有被其他资源请求,但事实并非如此。

这是我正在使用的代码:

在我的 main.js 文件中(延迟)

function init() {
    var vidDefer = document.getElementsByClassName('youtube');
    for (var i=0; i<vidDefer.length; i++) {
        if(vidDefer[i].getAttribute('data-src')) {
            vidDefer[i].setAttribute('src',vidDefer[i].getAttribute('data-src'));
        } 
    } 
}
window.onload = init;

然后,iframe 看起来像这样:

<iframe class="youtube" id="home-youtube" src="" data-src="https://www.youtube.com/embed/QHfgMrpMOm4" frameborder="0" allowfullscreen></iframe>

提前致谢

【问题讨论】:

    标签: javascript html css youtube


    【解决方案1】:

    您总是可以尝试使用 Youtube 的 javascript API 和

    https://developers.google.com/youtube/iframe_api_reference

    <!DOCTYPE html>
    <html>
      <body>
        <!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
        <div id="player"></div>
    
        <script>
          // 2. This code loads the IFrame Player API code asynchronously.
          var tag = document.createElement('script');
    
          tag.src = "https://www.youtube.com/iframe_api";
          var firstScriptTag = document.getElementsByTagName('script')[0];
          firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    
          // 3. This function creates an <iframe> (and YouTube player)
          //    after the API code downloads.
          var player;
          function onYouTubeIframeAPIReady() {
            player = new YT.Player('player', {
              height: '390',
              width: '640',
              videoId: 'M7lc1UVf-VE',
              events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
              }
            });
          }
    
          // 4. The API will call this function when the video player is ready.
          function onPlayerReady(event) {
            event.target.playVideo();
          }
    
          // 5. The API calls this function when the player's state changes.
          //    The function indicates that when playing a video (state=1),
          //    the player should play for six seconds and then stop.
          var done = false;
          function onPlayerStateChange(event) {
            if (event.data == YT.PlayerState.PLAYING && !done) {
              setTimeout(stopVideo, 6000);
              done = true;
            }
          }
          function stopVideo() {
            player.stopVideo();
          }
        </script>
      </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 1970-01-01
      • 2014-08-12
      • 2022-01-02
      相关资源
      最近更新 更多