forked from malsup/cycle2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.cycle2.video.js
68 lines (59 loc) · 2.2 KB
/
jquery.cycle2.video.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*! youtube plugin for Cycle2; version: 20130708 */
(function($) {
"use strict";
var template = '<div class=cycle-youtube><object width="640" height="360">' +
'<param name="movie" value="{{url}}"></param>' +
'<param name="allowFullScreen" value="{{allowFullScreen}}"></param>' +
'<param name="allowscriptaccess" value="always"></param>' +
'<embed src="{{url}}" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="{{allowFullScreen}}"></embed>' +
'</object></div>';
$.extend($.fn.cycle.defaults, {
youtubeAllowFullScreen: true,
youtubeAutostart: false,
youtubeAutostop: true
});
$(document).on( 'cycle-bootstrap', function( e, opts ) {
if ( ! opts.youtube )
return;
// don't hide inactive slides; hiding video causes reload when it's shown again
opts.hideNonActive = false;
opts.container.find( opts.slides ).each(function(i) {
// convert anchors to template markup
if ( !this.href )
return;
var markup, slide = $(this), url = slide.attr( 'href' );
var fs = opts.youtubeAllowFullScreen ? 'true' : 'false';
url += ( /\?/.test( url ) ? '&' : '?') + 'enablejsapi=1';
if ( opts.youtubeAutostart && opts.startingSlide === i )
url += '&autoplay=1';
markup = opts.API.tmpl( template, { url: url, allowFullScreen: fs });
slide.replaceWith( markup );
});
opts.slides = opts.slides.replace(/(\b>?a\b)/,'div.cycle-youtube');
if ( opts.youtubeAutostart ) {
opts.container.on( 'cycle-initialized cycle-after', function( e, opts ) {
var index = e.type == 'cycle-initialized' ? opts.currSlide : opts.nextSlide;
$( opts.slides[ index ] ).find('object,embed').each( play );
});
}
if ( opts.youtubeAutostop ) {
opts.container.on( 'cycle-before', function( e, opts ) {
$( opts.slides[ opts.currSlide ] ).find('object,embed').each( pause );
});
}
});
function play() {
/*jshint validthis:true */
try {
this.playVideo();
}
catch( ignore ) {}
}
function pause() {
/*jshint validthis:true */
try {
this.pauseVideo();
}
catch( ignore ) {}
}
})(jQuery);