forked from malsup/cycle2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.cycle2.center.js
More file actions
52 lines (43 loc) · 1.37 KB
/
Copy pathjquery.cycle2.center.js
File metadata and controls
52 lines (43 loc) · 1.37 KB
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
/*! center plugin for Cycle2; version: BETA-20120910 */
(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', function() {
clearTimeout( timeout );
timeout = setTimeout( adjustActive, 50 );
});
opts.container.on( 'cycle-slide-added', function( e, opts, slideOpts, slide ) {
adjustSlide.apply(slide);
});
adjustAll();
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);