How to embed youtube video in magnific popup?

前端 未结 3 2045
夕颜
夕颜 2021-02-01 06:49

i have magnific popup plugin.but it not showing the video on popup How to embed youtube video in magnific popup?

3条回答
  •  孤城傲影
    2021-02-01 07:25

    Excellent starting point Roy but lets extend this a bit further since Youtube has start from specific time embeds and nowadays gives users the youtu.be links to embed. So to match both cases including starting video from specific timeline I do this. Note that I also added the markup override, remove it if you dont need it.

    function extendMagnificIframe(){
    
        var $start = 0;
        var $iframe = {
            markup: '
    ' + '
    ' + '' + '
    ' + '
    ' + '
    ' + '
    ', patterns: { youtube: { index: 'youtu', id: function(url) { var m = url.match( /^.*(?:youtu.be\/|v\/|e\/|u\/\w+\/|embed\/|v=)([^#\&\?]*).*/ ); if ( !m || !m[1] ) return null; if(url.indexOf('t=') != - 1){ var $split = url.split('t='); var hms = $split[1].replace('h',':').replace('m',':').replace('s',''); var a = hms.split(':'); if (a.length == 1){ $start = a[0]; } else if (a.length == 2){ $start = (+a[0]) * 60 + (+a[1]); } else if (a.length == 3){ $start = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); } } var suffix = '?autoplay=1'; if( $start > 0 ){ suffix = '?start=' + $start + '&autoplay=1'; } return m[1] + suffix; }, src: '//www.youtube.com/embed/%id%' }, vimeo: { index: 'vimeo.com/', id: function(url) { var m = url.match(/(https?:\/\/)?(www.)?(player.)?vimeo.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/); if ( !m || !m[5] ) return null; return m[5]; }, src: '//player.vimeo.com/video/%id%?autoplay=1' } } }; return $iframe; } $('.my-selector').magnificPopup({ type: 'iframe', iframe: extendMagnificIframe() });

提交回复
热议问题