forked from malsup/cycle2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.cycle2.center.js
63 lines (50 loc) · 1.65 KB
/
jquery.cycle2.center.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
/*! center plugin for Cycle2; version: 20130324 */
(function($) {
"use strict";
$.extend($.fn.cycle.defaults, {
centerHorz: false,
centerVert: false
});
$(document).on( 'cycle-pre-initialize', function( e, opts ) {
if ( !opts.centerHorz && !opts.centerVert )
return;
// throttle resize event
var timeout, timeout2;
$(window).on( 'resize orientationchange', resize );
opts.container.on( 'cycle-destroyed', destroy );
opts.container.on( 'cycle-initialized cycle-slide-added cycle-slide-removed', function( e, opts, slideOpts, slide ) {
adjustActive();
});
adjustActive();
function resize() {
clearTimeout( timeout );
timeout = setTimeout( adjustActive, 50 );
}
function destroy( e, opts ) {
clearTimeout( timeout );
clearTimeout( timeout2 );
$( window ).off( 'resize orientationchange', resize );
}
function adjustAll() {
opts.slides.each( adjustSlide );
}
function adjustActive() {
/*jshint validthis: true */
adjustSlide.apply( opts.container.find( opts.slideActiveClass ) );
clearTimeout( timeout2 );
timeout2 = setTimeout( adjustAll, 50 );
}
function adjustSlide() {
/*jshint validthis: true */
var slide = $(this);
var contW = opts.container.width();
var contH = opts.container.height();
var w = slide.width();
var h = slide.height();
if (opts.centerHorz && w < contW)
slide.css( 'marginLeft', (contW - w) / 2 );
if (opts.centerVert && h < contH)
slide.css( 'marginTop', (contH - h) / 2 );
}
});
})(jQuery);