弹出窗口中的Youtube视频自动播放

时间:2014-08-07 05:57:35

标签: javascript jquery html css youtube

我做了一个弹出窗口并在其中放置了一个YouTube视频。我将视频设置为自动播放。但问题是当我打开页面时视频播放。它是全局自动播放,我希望它在弹出窗口显示时自动播放。我没有找到任何解决方案。

目前,它像我有背景幽灵说话一样。 有人可以帮忙吗?

视频Html

    <iframe width="800" height="315" src="http://www.youtube.com/embed/?wmode=opaque&amp;autoplay=1&amp;rel=0&amp;color=white" frameborder="0"></iframe><a href="http://www.youtube.com" target="_blank"><img alt="" class="watermark" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQHcbyVSjHQlwCy3tYqOyLwSWDO4tblhxTVVjKV5R0PtFsPy9TwfA" /></a></div>
    </div>

调用弹出窗口

 <a href="#" data-reveal-id="video_pop"><img src="kaow.png"/></a>
    </div>

弹出内容

<div id="video_pop" class="reveal-modal medium">
    <a class="close-reveal-modal"></a>
     <div>

    <iframe width="800" height="315" src="http://www.youtube.com/embed/?wmode=opaque&amp;autoplay=1&amp;rel=0&amp;color=white" frameborder="0"></iframe><a href="http://www.youtube.com" target="_blank"><img alt="" class="watermark" src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQHcbyVSjHQlwCy3tYqOyLwSWDO4tblhxTVVjKV5R0PtFsPy9TwfA" /></a></div>
    </div>

JS

Foundation.libs.reveal = {
    name: "reveal",
    version: "4.2.2",
    locked: !1,
    settings: {
        animation: "fadeAndPop",
        animationSpeed: 250,
        closeOnBackgroundClick: !0,
        closeOnEsc: !0,
        dismissModalClass: "close-reveal-modal",
        bgClass: "reveal-modal-bg",
        open: function() {},
        opened: function() {},
        close: function() {},
        closed: function() {},
        bg: a(".reveal-modal-bg"),
        css: {
            open: {
                opacity: 0,
                visibility: "visible",
                display: "block"
            },
            close: {
                opacity: 1,
                visibility: "hidden",
                display: "none"
            }
        }
    },
    init: function(b, c, d) {
        return Foundation.inherit(this, "data_options delay"), "object" == typeof c ? a.extend(!0, this.settings, c) : "undefined" != typeof d && a.extend(!0, this.settings, d), 
        "string" != typeof c ? (this.events(), this.settings.init) : this[c].call(this, d);
    },
    events: function() {
        var b = this;
        return a(this.scope).off(".fndtn.reveal").on("click.fndtn.reveal", "[data-reveal-id]", function(c) {
            c.preventDefault();
            if (!b.locked) {
                var d = a(this), e = d.data("reveal-ajax");
                b.locked = !0;
                if ("undefined" == typeof e) b.open.call(b, d); else {
                    var f = e === !0 ? d.attr("href") : e;
                    b.open.call(b, d, {
                        url: f
                    });
                }
            }
        }).on("click.fndtn.reveal", this.close_targets(), function(c) {
            c.preventDefault();
            if (!b.locked) {
                var d = a.extend({}, b.settings, b.data_options(a(".reveal-modal.open")));
                if (a(c.target)[0] === a("." + d.bgClass)[0] && !d.closeOnBackgroundClick) return;
                b.locked = !0, b.close.call(b, a(this).closest(".reveal-modal"));
            }
        }).on("open.fndtn.reveal", ".reveal-modal", this.settings.open).on("opened.fndtn.reveal", ".reveal-modal", this.settings.opened).on("opened.fndtn.reveal", ".reveal-modal", this.open_video).on("close.fndtn.reveal", ".reveal-modal", this.settings.close).on("closed.fndtn.reveal", ".reveal-modal", this.settings.closed).on("closed.fndtn.reveal", ".reveal-modal", this.close_video), 
        a("body").bind("keyup.reveal", function(c) {
            var d = a(".reveal-modal.open"), e = a.extend({}, b.settings, b.data_options(d));
            27 === c.which && e.closeOnEsc && d.foundation("reveal", "close");
        }), !0;
    },
    open: function(b, c) {
        if (b) if ("undefined" != typeof b.selector) var d = a("#" + b.data("reveal-id")); else {
            var d = a(this.scope);
            c = b;
        } else var d = a(this.scope);
        if (!d.hasClass("open")) {
            var e = a(".reveal-modal.open");
            "undefined" == typeof d.data("css-top") && d.data("css-top", parseInt(d.css("top"), 10)).data("offset", this.cache_offset(d)), 
            d.trigger("open"), e.length < 1 && this.toggle_bg(d);
            if ("undefined" == typeof c || !c.url) this.hide(e, this.settings.css.close), this.show(d, this.settings.css.open); else {
                var f = this, g = "undefined" != typeof c.success ? c.success : null;
                a.extend(c, {
                    success: function(b, c, h) {
                        a.isFunction(g) && g(b, c, h), d.html(b), a(d).foundation("section", "reflow"), 
                        f.hide(e, f.settings.css.close), f.show(d, f.settings.css.open);
                    }
                }), a.ajax(c);
            }
        }
    },
    close: function(b) {
        var b = b && b.length ? b : a(this.scope), c = a(".reveal-modal.open");
        c.length > 0 && (this.locked = !0, b.trigger("close"), this.toggle_bg(b), this.hide(c, this.settings.css.close));
    },
    close_targets: function() {
        var a = "." + this.settings.dismissModalClass;
        return this.settings.closeOnBackgroundClick ? a + ", ." + this.settings.bgClass : a;
    },
    toggle_bg: function(b) {
        0 === a(".reveal-modal-bg").length && (this.settings.bg = a("<div />", {
            "class": this.settings.bgClass
        }).appendTo("body")), this.settings.bg.filter(":visible").length > 0 ? this.hide(this.settings.bg) : this.show(this.settings.bg);
    },

编辑:

我正在使用基础5显示模型进行弹出式广告:http://foundation.zurb.com/docs/components/reveal.html

4 个答案:

答案 0 :(得分:9)

我想你想要这样的东西:

LIVE EXAMPLE

我基本上使用API​​ Javascript向视频添加动作playpause

我使用Foundation的默认功能向玩家添加事件。

<强> HTML

 <a href="#" data-reveal-id="myModal">Click Me For A Modal</a>

  <div id="myModal" class="reveal-modal" data-reveal>
    <h2>Awesome</h2>
    <div id="player"></div>
    <a class="close-reveal-modal">&#215;</a>
  </div>

JS

var tag = document.createElement('script');

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
//    after the API code downloads.
var player;

function onYouTubeIframeAPIReady() {
    player = new YT.Player('player', {
        height: '315',
        width: '560',
        videoId: 'l-gQLqv9f4o',
        events: {
            'onStateChange': onPlayerStateChange
        }
    });
}


function onPlayerStateChange(event) {
    if (event.data == YT.PlayerState.PLAYING) {
      //player is playing
    } else {
      //player is paused
    }
}

function stopVideo() {
    player.stopVideo();
}

function playVideo() {
  player.playVideo();
}

function pauseVideo() {
  player.pauseVideo();
}


$(document).on('opened.fndtn.reveal', '[data-reveal]', function () {
    playVideo();
});

$(document).on('closed.fndtn.reveal', '[data-reveal]', function () {
  pauseVideo();
});

答案 1 :(得分:3)

  

From the YouTube JavaScript Player API Reference

     

JavaScript API允许用户控制YouTube无边框或   嵌入式视频播放器通过JavaScript。可以打电话,   暂停,在视频中寻找某个时间,设置音量,静音   播放器和其他有用的功能。

您可以使用API​​的player.playVideo()player.pauseVideo()player.stopVideo()方法。

答案 2 :(得分:0)

所以,我已经阅读了很多不同的主题,我看到的大部分内容都非常复杂。我走了一条更简单的路线。

    $('.reveal-overlay').click(function(e){
    var loc = document.getElementById('myFrame').src;
    var stoploc = loc.replace("?autoplay=1", "");
    document.getElementById('myFrame').setAttribute('src', stoploc);
    });

    $('.playVideo').click(function(e){
    var loc = document.getElementById('myFrame').src;
    var autoloc = loc + "?autoplay=1";
    document.getElementById('myFrame').setAttribute('src', autoloc);
    });

一旦打开模态,这基本上只会将?autoplay = 1附加到YouTube视频。类.playVideo是用于打开模态窗口的任何按钮。

类.reveal-overlay由Foundation创建。因此,当点击叠加层时,它会从视频网址中删除任何?autoplay = 1。

希望这可以帮助任何遇到此问题的人。这很简单,应该适用于多个不同的视频,另外,如果有人关闭js,它就不会在后台播放任何内容。

还有一件事,您加载的视频应该是常规的非自动播放视频。这将在点击后将其转换为自动播放,并在关闭时将其返回到非自动播放。

答案 3 :(得分:0)

感谢@Jarod Means脚本!它工作,我使用它,因为它只需要jQuery(简单的解决方案=伟大的解决方案)。但是我为多个视频剪辑开发了它,这在幻灯片中很棒,作为一个例子,或者当你需要这个解决方案在同一个dom中的多个剪辑时。我也做了它,所以它完全在jQuery中。

只需发布即可,您可以随心所欲地使用它。

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <meta charset="utf-8">
    <title>Youtube-player test</title> 
    <style>
        .youtube-player iframe {
            display: none;
        }

        .youtube-player.active iframe {
            display: block;
        }
    </style>
</head>
<body>
    <div class="youtube-player">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/XzXjuDM_Z54" frameborder="0" allowfullscreen></iframe>
        <a href="#" class="youtube-link-start">Open window</a>
        <a href="#" class="youtube-link-stop">Close window</a>
    </div>
    <div class="youtube-player">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/l2EYyzXJIdE" frameborder="0" allowfullscreen></iframe>
        <a href="#" class="youtube-link-start">Open window</a>
        <a href="#" class="youtube-link-stop">Close window</a>
    </div>

    <script>

        jQuery('.youtube-player').each(function(){
            jQuery(this).on('click', '.youtube-link-start', function(){
                jQuery(this).parent().addClass('active');

                var loc = $(this).siblings('iframe').attr('src');
                var startloc = loc + "?autoplay=1";
                $(this).siblings('iframe').attr('src', startloc);
            });

            jQuery(this).on('click', '.youtube-link-stop', function(){
                jQuery(this).parent().removeClass('active');

                var loc = $(this).siblings('iframe').attr('src');
                var stoploc = loc.replace("?autoplay=1", "");
                $(this).siblings('iframe').attr('src', stoploc);
            });
        });
    </script>
</body>
</html>

我想你可以继续使用html,css和jQuery来改变/开发它,以满足你的需求。 :)