1
1
/*global jQuery */
2
- /*!
2
+ /*!
3
3
* FlexVerticalCenter.js 1.0
4
4
*
5
5
* Copyright 2011, Paul Sprangers http://paulsprangers.com
6
- * Released under the WTFPL license
6
+ * Released under the WTFPL license
7
7
* http://sam.zoy.org/wtfpl/
8
8
*
9
9
* Date: Fri Oct 28 19:12:00 2011 +0100
10
10
*/
11
11
( 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
+
15
20
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
+
19
23
// recalculate the distance to the top of the element to keep it centered
20
24
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
+
22
27
$this . css (
23
- attribute , ( ( $this . parent ( ) . height ( ) - $this . height ( ) ) / 2 )
28
+ attribute , ( ( ( parentHeight - $this . height ( ) ) / 2 ) + parseInt ( settings . verticalOffset ) )
24
29
) ;
25
30
} ;
26
31
27
32
// Call once to set.
28
33
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
+
36
41
} ) ;
37
42
38
43
} ;
39
44
40
- } ) ( jQuery ) ;
45
+ } ) ( jQuery ) ;
0 commit comments