forked from malsup/cycle2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.cycle2.autoheight.js
42 lines (37 loc) · 1.32 KB
/
jquery.cycle2.autoheight.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
/*! Cycle2 autoheight plugin; Copyright (c) M.Alsup, 2012; version: BETA-20120910 */
(function($) {
"use strict";
$.extend($.fn.cycle.defaults, {
autoHeight: 0
});
$(document).on( 'cycle-initialized', function( e, opts ) {
var ratio;
if ( $.type( opts.autoHeight ) == 'number' && opts.autoHeight >= 0 ) {
// use existing slide
opts._sentinel = $( opts.slides[opts.autoHeight] ).clone().css({
position: 'static',
visibility: 'hidden',
display: 'block'
}).prependTo( opts.container ).removeClass().addClass('cycle-sentinel cycle-slide');
}
else if ( $.type( opts.autoHeight ) == 'string' && /\d+\:\d+/.test( opts.autoHeight ) ) {
// use ratio
ratio = opts.autoHeight.match(/(\d+)\:(\d+)/);
ratio = ratio[1] / ratio[2];
$(window).on( 'resize', onResize );
opts._autoHeightOnResize = onResize;
setTimeout(function() {
$(window).triggerHandler('resize');
},15);
}
function onResize() {
opts.container.height( opts.container.width() / ratio );
}
});
$(document).on( 'cycle-destroyed', function( e, opts ) {
if ( opts._sentinel )
opts._sentinel.remove();
if ( opts._autoHeightOnResize )
$(window).off( 'resize', opts._autoHeightOnResize );
});
})(jQuery);