YouTube 视频 ID 完成后如何让 fancybox 自动关闭?

发布于 2024-12-06 17:44:39 字数 41 浏览 1 评论 0原文

当 youtube 视频结束时,如何让 fancybox 自动关闭?

How i can make fancybox auto close when youtube video ended?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

叶落知秋 2024-12-13 17:44:39

这并不像听起来那么简单:

  • 首先确定您是否要使用嵌入播放器或 iframe 播放器。
  • 请遵循嵌入播放器 APIiframe 播放器 API 示例,用于初始化 API。
  • 弹出窗口打开后,使用 "onComplete" fancybox 回调 函数来设置播放器。
  • 在回调中,确保添加一个 onStateChange 事件侦听器,以便您可以确定视频何时完成(零值表示视频已结束)
  • 一旦发现视频已结束,则使用$.fancybox.close 方法来关闭弹出窗口

,或者,您可以让用户关闭弹出窗口。

This isn't as simple as it sounds:

  • First determine if you are going to use the embed or iframe player.
  • Follow the embed player API or iframe player API examples to initialize the API.
  • Use the "onComplete" fancybox callback functon to set up the player once the popup is open.
  • In the callback, make sure you add an onStateChange event listener so you can determine when the video has completed (the value of zero means the video has ended)
  • Once you find that the video has ended, then use the $.fancybox.close method to close the popup

or, you could just let the user close the popup.

知足的幸福 2024-12-13 17:44:39

这是 Fancybox-Youtube-Cookie-Autoclose-Autopopup 的完整脚本,只需下载 css 中所需的图像,将它们放入根目录中的 /fancybox 文件夹中,然后替换为您的视频 ID。经过充分测试确实有效...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/helpers/jquery.fancybox-media.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css" media="screen" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="http://www.youtube.com/player_api"></script>
 <script type="text/javascript"> 
// detect mobile devices features
var isTouchSupported = 'ontouchstart' in window,
    isTouchSupportedIE10 = navigator.userAgent.match(/Touch/i) != null;
function onPlayerReady(event) {
    if (!(isTouchSupported || isTouchSupportedIE10)) {
        // this is NOT a mobile device so autoplay     
         event.target.playVideo();
    }
}
function onPlayerStateChange(event) {
    if (event.data === 0) {
        $.fancybox.close();
    }
}
function onYouTubePlayerAPIReady() {
$(function() {
    if ($.cookie('welcome_video')) {
        // it hasn't been three days yet
    } else {
        // set cookie to expire in 3 days
        $.cookie('welcome_video', 'true', { expires: 3});
    $(document).ready(function () {
        $.fancybox.open({
		href: "https://www.youtube.com/embed/qm1RjPM9E-g", /*YOUR VIDEO ID*/
            helpers: {
                media: {
                    youtube: {
                        params: {
                            autoplay: 1,
                            rel: 0,
                            // controls: 0,
                            showinfo: 0,
                            autohide: 1,
                        }
                    }
                },
                buttons: {}
            },
            beforeShow: function () {
                var id = $.fancybox.inner.find('iframe').attr('id');
                var player = new YT.Player(id, {
                    events: {
                        onReady: onPlayerReady,
                        onStateChange: onPlayerStateChange
                    }
                });
            }
        }); // fancybox	
    }); // ready
   } // cookie else ready
}); // function for cookie
} // youtube API ready
</script>

this is the full script with Fancybox-Youtube-Cookie-Autoclose-Autopopup just download the images that required in css put them in /fancybox folder in your root and replace with your video id. Really works fully tested...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/helpers/jquery.fancybox-media.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css" media="screen" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="http://www.youtube.com/player_api"></script>
 <script type="text/javascript"> 
// detect mobile devices features
var isTouchSupported = 'ontouchstart' in window,
    isTouchSupportedIE10 = navigator.userAgent.match(/Touch/i) != null;
function onPlayerReady(event) {
    if (!(isTouchSupported || isTouchSupportedIE10)) {
        // this is NOT a mobile device so autoplay     
         event.target.playVideo();
    }
}
function onPlayerStateChange(event) {
    if (event.data === 0) {
        $.fancybox.close();
    }
}
function onYouTubePlayerAPIReady() {
$(function() {
    if ($.cookie('welcome_video')) {
        // it hasn't been three days yet
    } else {
        // set cookie to expire in 3 days
        $.cookie('welcome_video', 'true', { expires: 3});
    $(document).ready(function () {
        $.fancybox.open({
		href: "https://www.youtube.com/embed/qm1RjPM9E-g", /*YOUR VIDEO ID*/
            helpers: {
                media: {
                    youtube: {
                        params: {
                            autoplay: 1,
                            rel: 0,
                            // controls: 0,
                            showinfo: 0,
                            autohide: 1,
                        }
                    }
                },
                buttons: {}
            },
            beforeShow: function () {
                var id = $.fancybox.inner.find('iframe').attr('id');
                var player = new YT.Player(id, {
                    events: {
                        onReady: onPlayerReady,
                        onStateChange: onPlayerStateChange
                    }
                });
            }
        }); // fancybox	
    }); // ready
   } // cookie else ready
}); // function for cookie
} // youtube API ready
</script>

莫相离 2024-12-13 17:44:39

环顾四周后没有任何运气,这是我想出的解决方案

看看我们做什么,在这里观看视频

<div style="display: none;">  
<div id="player"></div>
</div>  

    <script src="http://www.youtube.com/player_api"></script>

    <script>

        $(document).ready(function () {
              $("a.video_button").fancybox({
                        'titlePosition'     : 'inside',
                        'transitionIn'      : 'none',
                        'transitionOut'     : 'none'
                    });
        });         


        // create youtube player
        var player;
        function onYouTubePlayerAPIReady() {
            player = new YT.Player('player', {
              height: '390',
              width: '640',
              videoId: 's19V_6Ay4No',
              events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
              }
            });
        }

        // autoplay video
        function onPlayerReady(event) {
        }


        // when video ends
        function onPlayerStateChange(event) {        
            if(event.data === 0) {          
              $(document).ready(function () { parent.$.fancybox.close();});
            }
        }

    </script>  

After looking around without any luck here is a solution I came up with

See what we do, watch a video here

<div style="display: none;">  
<div id="player"></div>
</div>  

    <script src="http://www.youtube.com/player_api"></script>

    <script>

        $(document).ready(function () {
              $("a.video_button").fancybox({
                        'titlePosition'     : 'inside',
                        'transitionIn'      : 'none',
                        'transitionOut'     : 'none'
                    });
        });         


        // create youtube player
        var player;
        function onYouTubePlayerAPIReady() {
            player = new YT.Player('player', {
              height: '390',
              width: '640',
              videoId: 's19V_6Ay4No',
              events: {
                'onReady': onPlayerReady,
                'onStateChange': onPlayerStateChange
              }
            });
        }

        // autoplay video
        function onPlayerReady(event) {
        }


        // when video ends
        function onPlayerStateChange(event) {        
            if(event.data === 0) {          
              $(document).ready(function () { parent.$.fancybox.close();});
            }
        }

    </script>  
毁梦 2024-12-13 17:44:39

是的,这是处理 fancybox 或 colorbox 的正确方法。 Youtube Video API 提供了不同的状态来处理这种情况,例如 unstarted=-1、ending=0、playing=1、paused=2、buffering=3、videocued=5

在 fancybox jquery 代码块中获取或捕获此状态可以轻松实现这一点。只需访问这篇带有适当演示的文章即可。访问此处

Yes this is the right way to handle to fancybox or colorbox. Youtube Video API provides different states to handle this like unstarted=-1, ended=0, playing=1, paused=2, buffering=3, video cued=5

Getting or traping this state in fancybox jquery code block one can achieve this easily. Just visit this article with proper demo also.Visit here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文