将flash youtube更改为embeded对象

时间:2016-12-11 15:06:15

标签: javascript jquery flash youtube

我正在为我的youtube预告片使用延迟加载但是,我注意到它在移动设备上不起作用我认为这是因为它的flash不是对象?

这个功能床可以变成嵌入物体吗?

  $('a.youtube-video').click(function(e) {
    e.preventDefault();
    var videoId = $(this).attr('data-video');
    var params = { allowScriptAccess: "always", allowFullScreen: "true" };
    var atts = { id: 'player_'+videoId };
    $(this).hide();
    swfobject.embedSWF(
        "http://www.youtube.com/v/"+videoId+"?enablejsapi=1&playerapiid=ytplayer&version=3", 
        'placeholder_'+videoId, "100%", "315", "8", null, null, params, atts);

});

任何帮助都会很棒......

这是完整的代码:

<script>$(document).ready(function() {
$('a.youtube-video').each(function() {
    var videoId = $(this).attr('data-video');
    var videoThumbnail = "http://img.youtube.com/vi/" + videoId + "/0.jpg";
    var videoBackground = $('<span class="youtube-thumbnail"></span>');
    videoBackground.css({
        background:"#fff url('"+videoThumbnail+"') no-repeat",          
backgroundSize: 'cover'

    });

    // also set the parent container size to prevent flicker
    $(this).add($(this).parent()).css({
        height:315,
        width:"100%",
        position:"relative",
        display:"block",
        textAlign:"center",
        color:"#fff",
        fontSize:26


    });
    $(this).text('Click to load video');
    $(this).append(videoBackground);
});

$('a.youtube-video').click(function(e) {
    e.preventDefault();
    var videoId = $(this).attr('data-video');
    var params = { allowScriptAccess: "always", allowFullScreen: "true" };
    var atts = { id: 'player_'+videoId };
    $(this).hide();
    swfobject.embedSWF(
        "http://www.youtube.com/v/"+videoId+"?enablejsapi=1&playerapiid=ytplayer&version=3", 
        'placeholder_'+videoId, "100%", "315", "8", null, null, params, atts);

});

});

1 个答案:

答案 0 :(得分:2)

$('a.youtube-video').click(function(e) {
    e.preventDefault();
    var videoId = $(this).attr('data-video');
    $("#player_" + videoId).html('<iframe width="100%" height="315" src="https://www.youtube.com/embed/' + videoId + '" frameborder="0" allowfullscreen></iframe>');
});