2017-08-17 34 views

回答

2

方法1

你可以嘗試延遲加載嵌入視頻:加載它後,他們已經滾動到足以真正看到視頻或僅加載視頻播放一次被點擊。當您的網頁上嵌入的視頻較低時,此方法當然僅適用。 Here's a great guide on how to lazy load embedded YouTube videos.

方法2

加載整個頁面。加載後,使用Javascript生成視頻嵌入代碼。 jQuery示例:

$(document).ready(function() { 
    // Page is fully loaded, generate the iframe and set attributes 
    var aFrame = $('<iframe>'); 

    $(aFrame).attr('src', 'https://www.youtube.com/embed/EU7PRmCpx-0'); 
    $(aFrame).attr('width', '560'); 
    $(aFrame).attr('height', '315'); 

    // iframe element is created, append it to the body (or another element) 
    $('body').append(aFrame); 
}); 
+1

很好的答案。你可以添加一個工作的例子嗎? – lumio

+1

@lumio我爲第一個方法添加了一個示例,併爲方法2擴展了我的示例。 – Musa

相關問題