【问题标题】:google + and facebook style iframe youtube video. by default shows video image, when image clicked video playsgoogle + 和 facebook 风格的 iframe youtube 视频。默认显示视频图像,点击图像时播放视频
【发布时间】:2012-02-12 17:37:50
【问题描述】:

我已经有一个插件可以在我的网站上嵌入视频,名为 oembed。我需要的是一个 javascript 代码,当单击图像时,视频播放器会出现并播放,它将 youtube iframe 或其他嵌入转换为视频的默认图像。 Google+ 和 facebook 已经实现了这个想法。请告诉我如何做到这一点。

这是我的 youtube iframe 的示例:

<iframe width="267" height="200" src="http://www.youtube.com/embed/YOUTUBEID;feature=oembed" frameborder="0" allowfullscreen=""></iframe>

我找到了两个代码,修改成这样:

var RE = /embed\/([a-zA-Z0-9_-]{11})/;

jQuery( "iframe" ).each(function(){

var id = ( this.src.match( RE ) || [] )[1];

if( id ) {
jQuery( this ).replaceWith( 
                            '<img   width="420" height="215"  src="http://i1.ytimg.com/vi/'+id+'/hqdefault.jpg"'+
                            ' class="youtube" />' );
}
});
var youtubeIFRAME = '<p class="close">CLOSE</span><iframe data-address="$1" src="http://www.youtube.com/embed/$1?rel=0" frameborder="0" allowfullscreen></iframe>';

jQuery("img.youtube").click(function(){
var $this = $(this);   
$this.replaceWith('<iframe width="420" height="315" src="http://www.youtube.com/embed/$1?rel=0" frameborder="0" allowfullscreen></iframe>'                  
                  .replace("$1", $this.attr("data-address")));
});

问题现在是嵌入工作,但捕捉视频 ID 时出现问题。

【问题讨论】:

    标签: java javascript youtube embed thumbnails


    【解决方案1】:

    问题在于您的 iframe 周围有一个链接元素。我相信您可以通过删除链接并使用其他东西(例如 div)来解决此问题:

    function changeiframe() {
    objs = document.getElementsByTagName('iframe');
    for (i in objs) {
            if (objs[i].src) {
            vidid = objs[i].src.split('/')[4].split('?')[0];
            linkurl = 'http://www.youtube.com/watch?v='+vidid;
            bgurl = 'http://img.youtube.com/vi/'+vidid+'/0.jpg';
            imgurl = 'playsh.png';
            container = document.createElement('div');
            container.style.backgroundImage = 'url('+bgurl+')';
            container.className += "youvid";
            img = document.createElement('img');
            img.src = imgurl;
            container.appendChild(img);
            objs[i].outerHTML = container.outerHTML;
            }
        }
    }
    

    【讨论】:

    • 您好,感谢您的回复!该代码仅适用于谷歌浏览器。
    猜你喜欢
    • 2017-11-05
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2011-08-26
    • 2014-02-12
    相关资源
    最近更新 更多