Skip to content

Commit

Permalink
added update callback events
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Oct 16, 2014
1 parent 57ee64a commit 0b31e21
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ See the [jquery.matchHeight.js demo](http:https://brm.io/jquery-match-height-demo).
- data attributes API
- can be removed when needed
- maintain scroll position correctly
- callback events
- tested in IE8+, Chrome, Firefox, Chrome Android

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

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

#### Callback events

Since matchHeight automatically handles updating the layout after certain window events, you can supply functions as global callbacks if you need to be notified:

$.fn.matchHeight._beforeUpdate = function(event, groups) {
// do something before any updates are applied
}

$.fn.matchHeight._afterUpdate = function(event, groups) {
// do something after all updates are applied
}

Where `event` a jQuery event object (e.g. `load`, `resize`, `orientationchange`) and `groups` is a reference to `$.fn.matchHeight._groups` (see below).

#### Removing

It is possible to remove any matchHeight bindings for a given set of elements like so
Expand Down
10 changes: 5 additions & 5 deletions jquery.matchHeight-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions jquery.matchHeight.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@
$.fn.matchHeight._groups = [];
$.fn.matchHeight._throttle = 80;
$.fn.matchHeight._maintainScroll = false;
$.fn.matchHeight._beforeUpdate = null;
$.fn.matchHeight._afterUpdate = null;

/*
* $.fn.matchHeight._apply
Expand Down Expand Up @@ -242,10 +244,16 @@
* updates matchHeight on all current groups with their correct options
*/

var _update = function() {
var _update = function(event) {
if ($.fn.matchHeight._beforeUpdate)
$.fn.matchHeight._beforeUpdate(event, $.fn.matchHeight._groups);

$.each($.fn.matchHeight._groups, function() {
$.fn.matchHeight._apply(this.elements, this.byRow);
});

if ($.fn.matchHeight._afterUpdate)
$.fn.matchHeight._afterUpdate(event, $.fn.matchHeight._groups);
};

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

// throttle updates
if (!throttle) {
_update();
_update(event);
} else if (_updateTimeout === -1) {
_updateTimeout = setTimeout(function() {
_update();
_update(event);
_updateTimeout = -1;
}, $.fn.matchHeight._throttle);
}
Expand All @@ -279,7 +287,7 @@

// update heights on load and resize events
$(window).bind('load', function(event) {
$.fn.matchHeight._update();
$.fn.matchHeight._update(false, event);
});

// throttled update heights on resize events
Expand Down
11 changes: 11 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@
$('.btn-hidden').click(function() {
$('body').removeClass('test-hidden');
});

// example of update callbacks (uncomment to test)
$.fn.matchHeight._beforeUpdate = function(event, groups) {
//var eventType = event ? event.type + ' event, ' : '';
//console.log("beforeUpdate, " + eventType + groups.length + " groups");
}

$.fn.matchHeight._afterUpdate = function(event, groups) {
//var eventType = event ? event.type + ' event, ' : '';
//console.log("afterUpdate, " + eventType + groups.length + " groups");
}
});

})();
Expand Down

0 comments on commit 0b31e21

Please sign in to comment.