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 : {} } });
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...
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 :then place your fancybox initialization code (including the
.ready()
method) inside this function like :onPlayerReady
- listens for youtube's player :onPlayerStateChange
- listens for youtube's player changes, including the video end. Here we call the fancybox close method :3). Now use the fancybox
beforeShow
callback to bind the last 2 functions to your fancybox (youtube) content like :See JSFIDDLE
EDIT (Feb 3rd. 2014):
@fightstarr20 said :
It seems like youtube converts videos played in iOS devices into HTML5 format. Somehow, the autoplay option inside the function
... 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 thisSee 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.
for those who are trying to achieve for fancybox version 3.3.5