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/
| 归档时间: |
|
| 查看次数: |
9130 次 |
| 最近记录: |