Skip to content

Commit 9a55a36

Browse files
konturJohannes Neumeier
authored andcommitted
added debounce by default
1 parent 0cdd543 commit 9a55a36

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

jquery.flexverticalcenter.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@
1414
var settings = $.extend({
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
17-
parentSelector: null // a selector representing the parent to vertically center this element within
17+
parentSelector: null, // a selector representing the parent to vertically center this element within
18+
debounceTimeout: 25 // a default debounce timeout in milliseconds
1819
}, options || {});
20+
var debounce;
1921

2022
return this.each(function(){
2123
var $this = $(this); // store the object
2224

2325
// recalculate the distance to the top of the element to keep it centered
2426
var resizer = function () {
27+
2528
var parentHeight = (settings.parentSelector) ? $this.parents(settings.parentSelector).first().height() : $this.parent().height();
2629

2730
$this.css(
@@ -33,7 +36,10 @@
3336
resizer();
3437

3538
// Call on resize. Opera debounces their resize by default.
36-
$(window).resize(resizer);
39+
$(window).resize(function () {
40+
clearTimeout(debounce);
41+
debounce = setTimeout(resizer, settings.debounceTimeout);
42+
});
3743

3844
// Apply a load event to images within the element so it fires again after an image is loaded
3945
$this.find('img').load(resizer);

0 commit comments

Comments
 (0)