forked from PaulSpr/jQuery-Flex-Vertical-Center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.flexverticalcenter.js
60 lines (49 loc) · 1.92 KB
/
jquery.flexverticalcenter.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
/*global jQuery */
/*!
* FlexVerticalCenter.js 1.0
*
* Copyright 2011, Paul Sprangers http://paulsprangers.com
* Released under the WTFPL license
* http://sam.zoy.org/wtfpl/
*
* Date: Fri Oct 28 19:12:00 2011 +0100
*/
(function( $ ){
$.fn.flexVerticalCenter = function( options ) {
var settings = $.extend({
cssAttribute: 'margin-top', // the attribute to apply the calculated value to
verticalOffset: 0, // the number of pixels to offset the vertical alignment by
parentSelector: null, // a selector representing the parent to vertically center this element within
debounceTimeout: 25, // a default debounce timeout in milliseconds
deferTilWindowLoad: false // if true, nothing will take effect until the $(window).load event
}, options || {});
return this.each(function(){
var $this = $(this); // store the object
var debounce;
// recalculate the distance to the top of the element to keep it centered
var resizer = function () {
var parentHeight = (settings.parentSelector && $this.parents(settings.parentSelector).length) ?
$this.parents(settings.parentSelector).first().height() : $this.parent().height();
$this.css(
settings.cssAttribute, ( ( ( parentHeight - $this.height() ) / 2 ) + parseInt(settings.verticalOffset) )
);
if (settings.complete !== undefined) {
settings.complete();
}
};
// Call on resize. Opera debounces their resize by default.
$(window).resize(function () {
clearTimeout(debounce);
debounce = setTimeout(resizer, settings.debounceTimeout);
});
if (!settings.deferTilWindowLoad) {
// call it once, immediately.
resizer();
}
// Call again to set after window (frames, images, etc) loads.
$(window).load(function () {
resizer();
});
});
};
})( jQuery );