Skip to content

Commit 87bc2de

Browse files
committed
Merge pull request #15 from joshuapaling/add-defer-til-window-load-option
Resize immediately by default. Add option to defer til window.load
2 parents e5b909b + 52070d1 commit 87bc2de

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ Usage
99

1010
Simply include this file in your project (after loading jQuery) like this:
1111

12-
<script defer src="js/jquery.flexverticalcenter.js"></script>
12+
```<script defer src="js/jquery.flexverticalcenter.js"></script>```
1313

1414
Then call the plugin on the element which needs to be vertically centered in it's parent.
1515

16+
```
1617
<script>
1718
$(document).ready(function() {
1819
$('#element-to-be-centered').flexVerticalCenter();
1920
});
2021
</script>
22+
```
2123

2224
This will take the parents height, the elements own height and calculate the distance the element should have from the parents top to be vertically centered. This value is applied to the top margin of the element by default.
2325

@@ -30,17 +32,20 @@ You can pass an options hash to the plugin.
3032
- `onAttribute` - the css attribute that the value should be set on (default: 'margin-top')
3133
- `verticalOffset` - the number of pixels to offset the vertical alignment by, ie. 10, "50px", -100 (default: 0)
3234
- `parentSelector` - a selector representing the parent to vertically center this element within, ie. ".parent" (default: the element's immediate parent)
35+
- `debounceTimeout` - in milliseconds, used to rate-limit the vertical centering when resizing the browser window (default: 25)
36+
- `deferTilWindowLoad` - if true, no action will be taken until jQuery's window.load event fires. If false, the vertical centering will take place immediately, and then once again on window.load (default: false)
3337

3438
Examples:
3539

40+
```
3641
<script>
3742
$(document).ready(function() {
3843
$('#element-to-be-centered').flexVerticalCenter();
3944
$('#element-to-be-centered').flexVerticalCenter({ cssAttribute: 'padding-top', verticalOffset: '50px' });
4045
$('#element-to-be-centered').flexVerticalCenter({ cssAttribute: 'padding-top', parentSelector: '.parent' });
4146
});
4247
</script>
43-
48+
```
4449

4550
Non-jQuery version
4651
------------------
@@ -51,4 +56,4 @@ Matthew Nessworthy has made a vanilla javascript version of this plugin. Great i
5156
Note
5257
----
5358

54-
The initial code was more or less borrowed from the awesome FitText plugin. http://fittextjs.com/
59+
The initial code was more or less borrowed from the awesome FitText plugin. http://fittextjs.com/

jquery.flexverticalcenter.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
cssAttribute: 'margin-top', // the attribute to apply the calculated value to
1616
verticalOffset: 0, // the number of pixels to offset the vertical alignment by
1717
parentSelector: null, // a selector representing the parent to vertically center this element within
18-
debounceTimeout: 25 // a default debounce timeout in milliseconds
18+
debounceTimeout: 25, // a default debounce timeout in milliseconds
19+
deferTilWindowLoad: false // if true, nothing will take effect until the $(window).load event
1920
}, options || {});
2021

2122
return this.each(function(){
@@ -39,16 +40,18 @@
3940
debounce = setTimeout(resizer, settings.debounceTimeout);
4041
});
4142

42-
// Call once to set after window loads.
43+
if (!settings.deferTilWindowLoad) {
44+
// call it once, immediately.
45+
resizer();
46+
}
47+
48+
// Call again to set after window (frames, images, etc) loads.
4349
$(window).load(function () {
4450
resizer();
4551
});
4652

47-
// Apply a load event to images within the element so it fires again after an image is loaded
48-
$this.find('img').load(resizer);
49-
5053
});
5154

5255
};
5356

54-
})( jQuery );
57+
})( jQuery );

0 commit comments

Comments
 (0)