Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

jquery - Fancybox - Video auto close function

i'm using a Fancybox Media. I want to when the video complete, close the fancybox..

Here is my code:

$('.fancybox-media').attr('rel', 'media-gallery').fancybox({
    openEffect : 'none',
    closeEffect : 'none',
    prevEffect : 'none',
    nextEffect : 'none',
    arrows : false,
    padding : '0',
    margin: '0',
    width: '100%',
    height: '100%',
    helpers : {
        media : {
            youtube : {
                params : {
                    autoplay : 1,
                    rel : 0,
                    controls : 0,
                    showinfo : 0,
                    autohide : 1
                }
            }},
        buttons : {}
    }
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here it's the recipe :

1). Load the youtube's player API into your page.

2). Create 3 functions :

  • onYouTubePlayerAPIReady - listens for youtube's API :

    function onYouTubePlayerAPIReady() { .. }
    

    then place your fancybox initialization code (including the .ready() method) inside this function like :

    function onYouTubePlayerAPIReady() {
        $(document).ready(function () {
            $('.fancybox-media').attr('rel', 'media-gallery').fancybox({
                // fancybox API options
                ... etc.
            }); // fancybox
        }); // ready
    }
    
  • onPlayerReady - listens for youtube's player :

    function onPlayerReady(event) {
        event.target.playVideo();
    }
    
  • onPlayerStateChange - listens for youtube's player changes,?including the video end. Here we call the fancybox close method :

    function onPlayerStateChange(event) {
        if (event.data === 0) {
            $.fancybox.close();
        }
    }
    

3). Now use the fancybox beforeShow callback to bind the last 2 functions to your fancybox (youtube) content like :

beforeShow: function () {
    var id = $.fancybox.inner.find('iframe').attr('id');
    var player = new YT.Player(id, {
        events: {
            onReady: onPlayerReady,
            onStateChange: onPlayerStateChange
        }
    });
}

See JSFIDDLE


EDIT (Feb 3rd. 2014):

@fightstarr20 said :

This method doesn't appear to work on iPhone or iPad, any idea why?

It seems like youtube converts videos played in iOS devices into HTML5 format. Somehow, the autoplay option inside the function

function onPlayerReady(event) {
    event.target.playVideo();
}

... doesn't let youtube to convert the video properly and it just hangs.

The alternative solution is to detect mobile devices and only perform event.target.playVideo(); for desktop agents so, you could do this

// 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();
    }
}

See forked JSFIDDLE that should work for iOS and desktop devices alike.

NOTE: you could add your preferred device detection method. I found mine simple and reliable so far.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...