Skip to content

Commit 0b31e21

Browse files
committed
added update callback events
1 parent 57ee64a commit 0b31e21

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ See the [jquery.matchHeight.js demo](http://brm.io/jquery-match-height-demo).
2323
- data attributes API
2424
- can be removed when needed
2525
- maintain scroll position correctly
26+
- callback events
2627
- tested in IE8+, Chrome, Firefox, Chrome Android
2728

2829
### Status
@@ -80,6 +81,20 @@ All elements with the same group name will be set to the same height when the pa
8081

8182
Note that `byRow` will be enabled when using the data API, if you don't want this then use the alternative method above.
8283

84+
#### Callback events
85+
86+
Since matchHeight automatically handles updating the layout after certain window events, you can supply functions as global callbacks if you need to be notified:
87+
88+
$.fn.matchHeight._beforeUpdate = function(event, groups) {
89+
// do something before any updates are applied
90+
}
91+
92+
$.fn.matchHeight._afterUpdate = function(event, groups) {
93+
// do something after all updates are applied
94+
}
95+
96+
Where `event` a jQuery event object (e.g. `load`, `resize`, `orientationchange`) and `groups` is a reference to `$.fn.matchHeight._groups` (see below).
97+
8398
#### Removing
8499

85100
It is possible to remove any matchHeight bindings for a given set of elements like so

jquery.matchHeight-min.js

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jquery.matchHeight.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
$.fn.matchHeight._groups = [];
110110
$.fn.matchHeight._throttle = 80;
111111
$.fn.matchHeight._maintainScroll = false;
112+
$.fn.matchHeight._beforeUpdate = null;
113+
$.fn.matchHeight._afterUpdate = null;
112114

113115
/*
114116
* $.fn.matchHeight._apply
@@ -242,10 +244,16 @@
242244
* updates matchHeight on all current groups with their correct options
243245
*/
244246

245-
var _update = function() {
247+
var _update = function(event) {
248+
if ($.fn.matchHeight._beforeUpdate)
249+
$.fn.matchHeight._beforeUpdate(event, $.fn.matchHeight._groups);
250+
246251
$.each($.fn.matchHeight._groups, function() {
247252
$.fn.matchHeight._apply(this.elements, this.byRow);
248253
});
254+
255+
if ($.fn.matchHeight._afterUpdate)
256+
$.fn.matchHeight._afterUpdate(event, $.fn.matchHeight._groups);
249257
};
250258

251259
$.fn.matchHeight._update = function(throttle, event) {
@@ -261,10 +269,10 @@
261269

262270
// throttle updates
263271
if (!throttle) {
264-
_update();
272+
_update(event);
265273
} else if (_updateTimeout === -1) {
266274
_updateTimeout = setTimeout(function() {
267-
_update();
275+
_update(event);
268276
_updateTimeout = -1;
269277
}, $.fn.matchHeight._throttle);
270278
}
@@ -279,7 +287,7 @@
279287

280288
// update heights on load and resize events
281289
$(window).bind('load', function(event) {
282-
$.fn.matchHeight._update();
290+
$.fn.matchHeight._update(false, event);
283291
});
284292

285293
// throttled update heights on resize events

test.html

+11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
$('.btn-hidden').click(function() {
4141
$('body').removeClass('test-hidden');
4242
});
43+
44+
// example of update callbacks (uncomment to test)
45+
$.fn.matchHeight._beforeUpdate = function(event, groups) {
46+
//var eventType = event ? event.type + ' event, ' : '';
47+
//console.log("beforeUpdate, " + eventType + groups.length + " groups");
48+
}
49+
50+
$.fn.matchHeight._afterUpdate = function(event, groups) {
51+
//var eventType = event ? event.type + ' event, ' : '';
52+
//console.log("afterUpdate, " + eventType + groups.length + " groups");
53+
}
4354
});
4455

4556
})();

0 commit comments

Comments
 (0)