由于嵌入的视频太多,我的网站加载缓慢

Cor*_*ory 7 html css video jquery embedding

由于嵌入的视频太多,我的网站加载缓慢.我看到有图像的地方(嵌入视频的位置)并点击它,此时加载了嵌入的视频.任何人都可以给我一些帮助,想弄清楚如何做到这一点?也许一旦你将鼠标悬停在图像上,如果加载了youtube嵌入?非常感谢!

Abd*_*afa 13

在用户点击图片后,只需使用jQuery插入嵌入代码:

只是示例代码:HTML

<div id="video" style="background-color:red; width:560px; height:315px;"></div>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

$('#video').on('click', function() {
    $(this).html('<iframe width="560" height="315" src="//www.youtube.com/embed/9bZkp7q19f0?autoplay=1" frameborder="0" allowfullscreen></iframe>').css('background', 'none');
});
Run Code Online (Sandbox Code Playgroud)

如果您希望视频自动播放?autoplay=1,请像我一样添加到网址的末尾.

没有缩略图的代码:http://jsfiddle.net/KFcRJ/

要提取视频缩略图:

HTML:

<div id="video" style="background-color:red; width:560px; height:315px;">
<a href="http://www.youtube.com/watch?v=9bZkp7q19f0" class="youtube"></a></div>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

$('#video').on('click', function() {
$(this).html('<iframe width="560" height="315" src="//www.youtube.com/embed/9bZkp7q19f0?autoplay=1" frameborder="0" allowfullscreen></iframe>').css('background', 'none');
});

function getYoutubeID(url) {
    var id = url.match("[\\?&]v=([^&#]*)");
    id = id[1];
    return id;
};
$('a.youtube').each(function() {
    var id = getYoutubeID( this.href );
    this.id = id;
    var thumb_url = "http://img.youtube.com/vi/"+id+"/maxresdefault.jpg";
    $('<img width="100%" src="'+thumb_url+'" />').appendTo($('#video'));
});
Run Code Online (Sandbox Code Playgroud)

带缩略图的代码:http://jsfiddle.net/89uVe/4/

  • 只是旁注.."autoplay"不适用于iDevices. (2认同)