Skip to content

Commit 8603bd8

Browse files
committed
added options hash to function definition
1 parent 295b830 commit 8603bd8

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

jquery.flexverticalcenter.js

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
/*global jQuery */
2-
/*!
2+
/*!
33
* FlexVerticalCenter.js 1.0
44
*
55
* Copyright 2011, Paul Sprangers http://paulsprangers.com
6-
* Released under the WTFPL license
6+
* Released under the WTFPL license
77
* http://sam.zoy.org/wtfpl/
88
*
99
* Date: Fri Oct 28 19:12:00 2011 +0100
1010
*/
1111
(function( $ ){
12-
13-
$.fn.flexVerticalCenter = function( onAttribute ) {
14-
12+
13+
$.fn.flexVerticalCenter = function( onAttribute, options ) {
14+
var attribute = onAttribute || 'margin-top'; // the attribute to put the calculated value on
15+
var settings = $.extend({
16+
verticalOffset: 0, // the number of pixels to offset the vertical alignment by
17+
parentSelector: null // a selector representing the parent to vertically center this element within
18+
}, options || {});
19+
1520
return this.each(function(){
16-
var $this = $(this); // store the object
17-
var attribute = onAttribute || 'margin-top'; // the attribute to put the calculated value on
18-
21+
var $this = $(this); // store the object
22+
1923
// recalculate the distance to the top of the element to keep it centered
2024
var resizer = function () {
21-
// get parent height minus own height and devide by 2
25+
var parentHeight = (settings.parentSelector) ? $this.parents(settings.parentSelector).first().height() : $this.parent().height();
26+
2227
$this.css(
23-
attribute, ( ( $this.parent().height() - $this.height() ) / 2 )
28+
attribute, ( ( ( parentHeight - $this.height() ) / 2 ) + parseInt(settings.verticalOffset) )
2429
);
2530
};
2631

2732
// Call once to set.
2833
resizer();
29-
30-
// Call on resize. Opera debounces their resize by default.
31-
$(window).resize(resizer);
32-
33-
// Apply a load event to images within the element so it fires again after an image is loaded
34-
$this.find('img').load(resizer);
35-
34+
35+
// Call on resize. Opera debounces their resize by default.
36+
$(window).resize(resizer);
37+
38+
// Apply a load event to images within the element so it fires again after an image is loaded
39+
$this.find('img').load(resizer);
40+
3641
});
3742

3843
};
3944

40-
})( jQuery );
45+
})( jQuery );

0 commit comments

Comments
 (0)