How make fancybox auto close when youtube video id done?

你说的曾经没有我的故事 提交于 2019-12-21 17:29:08

问题


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


回答1:


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.




回答2:


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>



回答3:


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>  



回答4:


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



来源:https://stackoverflow.com/questions/7541890/how-make-fancybox-auto-close-when-youtube-video-id-done

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!