javascript 放大弹出窗口中的 YouTube 嵌入播放器中的 Rel=0 和 autoplay=1 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30557236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:21:36  来源:igfitidea点击:

Rel=0 and autoplay=1 in YouTube embed player in magnific popup not working

javascriptjqueryhtmlyoutubemagnific-popup

提问by Andreas S?gaard Pedersen

I'm having problems with having both rel=0 and autoplay=1 added in a YouTube embedded player in a magnific popup.

我在 YouTube 嵌入式播放器中同时添加 rel=0 和 autoplay=1 时遇到了问题。

It seems like it is only the first parameter in the url that is working, and i have tried all these versions:

似乎它只是 url 中的第一个参数有效,我已经尝试了所有这些版本:

?rel=0&autoplay=1
?rel=0&autoplay=1
?rel=0?autoplay=1

None of them are working. But if I move autoplay=1 up as the first parameter, then the autoplay works fine - but then the rel=0 doesn't.

他们都没有工作。但是,如果我将 autoplay=1 作为第一个参数向上移动,那么自动播放可以正常工作 - 但 rel=0 则不行。

Here's my code.

这是我的代码。

HTML:

HTML:

<a class="video1" href="https://www.youtube.com/watch?v=_ziUhNerFMI?rel=0&autoplay=1"></a>

CSS:

CSS:

.video1 {
  background-image: url('http://placehold.it/180x180');
  background-repeat: no-repeat;
  background-size: contain;
  height: 180px;
  width: 180px;
  display: block;
  outline: none;
}

JS:

JS:

jQuery(document).ready(function ($) {
    $('.video1').magnificPopup({
        type: 'iframe',

    iframe: {
        markup: '<?php echo $first; ?>'+
                '<div class="mfp-iframe-scaler">'+
                '<div class="mfp-close"></div>'+
                '<iframe class="mfp-iframe" frameborder="0" allowfullscreen></iframe>'+
                '</div>'
        },
        callbacks: {
            markupParse: function(template, values, item) {
            values.title = item.el.attr('title');
        }
        }

    });
});

采纳答案by odedta

This seems to work just fine:

这似乎工作得很好:

jQuery(document).ready(function ($) {
    $('.video1').magnificPopup({
       items: {
          src: '<iframe width="560" height="315" src="https://www.youtube.com/embed/_ziUhNerFMI?autoplay=1&rel=0" frameborder="0" allowfullscreen></iframe>',
          type: 'inline'
      }
    });
});
.video1 {
  background-image: url('http://placehold.it/180x180');
  background-repeat: no-repeat;
  background-size: contain;
  height: 180px;
  width: 180px;
  display: block;
  outline: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.0.0/jquery.magnific-popup.min.js"></script>
<a class="video1" href="https://www.youtube.com/watch?v=_ziUhNerFMI"></a>

回答by Nickfmc

Old post but this may help someone:

旧帖子,但这可能会帮助某人:

The issue is that the script is adding the autoplay for you with the ? so if you have something like your example:

问题是脚本正在为您添加自动播放?所以如果你有类似你的例子:

https://www.youtube.com/watch?v=_ziUhNerFMI?rel=0&autoplay=1

it is rendered as:

它呈现为:

https://www.youtube.com/watch?v=_ziUhNerFMI?rel=0&autoplay=1?autoplay=1

which causes it to error out. you can modify the magnific script and remove this, search for this line:

这导致它出错。您可以修改 magnific 脚本并删除它,搜索此行:

www.youtube.com/embed/%id%?autoplay=1

and replace with this:

并替换为:

www.youtube.com/embed/%id%