diff options
author | liabru <liabru@brm.io> | 2014-10-16 18:48:54 +0100 |
---|---|---|
committer | liabru <liabru@brm.io> | 2014-10-16 18:48:54 +0100 |
commit | 0b31e21ad88a6078896e22c4bfdab72f32fb82a2 (patch) | |
tree | 4ccddf7e36d76907818cb74855bd033cbdc67831 | |
parent | 57ee64a333353bbaf1827d545bbdb73a9d698f79 (diff) | |
download | jquery-match-height-0b31e21ad88a6078896e22c4bfdab72f32fb82a2.zip jquery-match-height-0b31e21ad88a6078896e22c4bfdab72f32fb82a2.tar.gz jquery-match-height-0b31e21ad88a6078896e22c4bfdab72f32fb82a2.tar.bz2 |
added update callback events
-rw-r--r-- | README.md | 15 | ||||
-rw-r--r-- | jquery.matchHeight-min.js | 10 | ||||
-rw-r--r-- | jquery.matchHeight.js | 16 | ||||
-rw-r--r-- | test.html | 11 |
4 files changed, 43 insertions, 9 deletions
@@ -23,6 +23,7 @@ See the [jquery.matchHeight.js demo](http://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 @@ -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 diff --git a/jquery.matchHeight-min.js b/jquery.matchHeight-min.js index 26aea18..5817cae 100644 --- a/jquery.matchHeight-min.js +++ b/jquery.matchHeight-min.js @@ -4,8 +4,8 @@ * License: MIT */ (function(b){var f=-1,e=-1,m=function(a){var d=null,c=[];b(a).each(function(){var a=b(this),h=a.offset().top-g(a.css("margin-top")),k=0<c.length?c[c.length-1]:null;null===k?c.push(a):1>=Math.floor(Math.abs(d-h))?c[c.length-1]=k.add(a):c.push(a);d=h});return c},g=function(b){return parseFloat(b)||0};b.fn.matchHeight=function(a){if("remove"===a){var d=this;this.css("height","");b.each(b.fn.matchHeight._groups,function(b,a){a.elements=a.elements.not(d)});return this}if(1>=this.length)return this;a="undefined"!== -typeof a?a:!0;b.fn.matchHeight._groups.push({elements:this,byRow:a});b.fn.matchHeight._apply(this,a);return this};b.fn.matchHeight._groups=[];b.fn.matchHeight._throttle=80;b.fn.matchHeight._maintainScroll=!1;b.fn.matchHeight._apply=function(a,d){var c=b(a),e=[c],h=b(window).scrollTop(),k=b("html").outerHeight(!0),f=c.parents().filter(":hidden");f.css("display","block");d&&(c.each(function(){var a=b(this),c="inline-block"===a.css("display")?"inline-block":"block";a.data("style-cache",a.attr("style")); -a.css({display:c,"padding-top":"0","padding-bottom":"0","margin-top":"0","margin-bottom":"0","border-top-width":"0","border-bottom-width":"0",height:"100px"})}),e=m(c),c.each(function(){var a=b(this);a.attr("style",a.data("style-cache")||"").css("height","")}));b.each(e,function(a,c){var e=b(c),f=0;d&&1>=e.length||(e.each(function(){var a=b(this),c="inline-block"===a.css("display")?"inline-block":"block";a.css({display:c,height:""});a.outerHeight(!1)>f&&(f=a.outerHeight(!1));a.css("display","")}), -e.each(function(){var a=b(this),c=0;"border-box"!==a.css("box-sizing")&&(c+=g(a.css("border-top-width"))+g(a.css("border-bottom-width")),c+=g(a.css("padding-top"))+g(a.css("padding-bottom")));a.css("height",f-c)}))});f.css("display","");b.fn.matchHeight._maintainScroll&&b(window).scrollTop(h/k*b("html").outerHeight(!0));return this};b.fn.matchHeight._applyDataApi=function(){var a={};b("[data-match-height], [data-mh]").each(function(){var d=b(this),c=d.attr("data-match-height")||d.attr("data-mh"); -a[c]=c in a?a[c].add(d):d});b.each(a,function(){this.matchHeight(!0)})};var l=function(){b.each(b.fn.matchHeight._groups,function(){b.fn.matchHeight._apply(this.elements,this.byRow)})};b.fn.matchHeight._update=function(a,d){if(d&&"resize"===d.type){var c=b(window).width();if(c===f)return;f=c}a?-1===e&&(e=setTimeout(function(){l();e=-1},b.fn.matchHeight._throttle)):l()};b(b.fn.matchHeight._applyDataApi);b(window).bind("load",function(a){b.fn.matchHeight._update()});b(window).bind("resize orientationchange", -function(a){b.fn.matchHeight._update(!0,a)})})(jQuery);
\ No newline at end of file +typeof a?a:!0;b.fn.matchHeight._groups.push({elements:this,byRow:a});b.fn.matchHeight._apply(this,a);return this};b.fn.matchHeight._groups=[];b.fn.matchHeight._throttle=80;b.fn.matchHeight._maintainScroll=!1;b.fn.matchHeight._beforeUpdate=null;b.fn.matchHeight._afterUpdate=null;b.fn.matchHeight._apply=function(a,d){var c=b(a),e=[c],h=b(window).scrollTop(),k=b("html").outerHeight(!0),f=c.parents().filter(":hidden");f.css("display","block");d&&(c.each(function(){var a=b(this),c="inline-block"===a.css("display")? +"inline-block":"block";a.data("style-cache",a.attr("style"));a.css({display:c,"padding-top":"0","padding-bottom":"0","margin-top":"0","margin-bottom":"0","border-top-width":"0","border-bottom-width":"0",height:"100px"})}),e=m(c),c.each(function(){var a=b(this);a.attr("style",a.data("style-cache")||"").css("height","")}));b.each(e,function(a,c){var e=b(c),f=0;d&&1>=e.length||(e.each(function(){var a=b(this),c="inline-block"===a.css("display")?"inline-block":"block";a.css({display:c,height:""});a.outerHeight(!1)> +f&&(f=a.outerHeight(!1));a.css("display","")}),e.each(function(){var a=b(this),c=0;"border-box"!==a.css("box-sizing")&&(c+=g(a.css("border-top-width"))+g(a.css("border-bottom-width")),c+=g(a.css("padding-top"))+g(a.css("padding-bottom")));a.css("height",f-c)}))});f.css("display","");b.fn.matchHeight._maintainScroll&&b(window).scrollTop(h/k*b("html").outerHeight(!0));return this};b.fn.matchHeight._applyDataApi=function(){var a={};b("[data-match-height], [data-mh]").each(function(){var d=b(this),c= +d.attr("data-match-height")||d.attr("data-mh");a[c]=c in a?a[c].add(d):d});b.each(a,function(){this.matchHeight(!0)})};var l=function(a){b.fn.matchHeight._beforeUpdate&&b.fn.matchHeight._beforeUpdate(a,b.fn.matchHeight._groups);b.each(b.fn.matchHeight._groups,function(){b.fn.matchHeight._apply(this.elements,this.byRow)});b.fn.matchHeight._afterUpdate&&b.fn.matchHeight._afterUpdate(a,b.fn.matchHeight._groups)};b.fn.matchHeight._update=function(a,d){if(d&&"resize"===d.type){var c=b(window).width(); +if(c===f)return;f=c}a?-1===e&&(e=setTimeout(function(){l(d);e=-1},b.fn.matchHeight._throttle)):l(d)};b(b.fn.matchHeight._applyDataApi);b(window).bind("load",function(a){b.fn.matchHeight._update(!1,a)});b(window).bind("resize orientationchange",function(a){b.fn.matchHeight._update(!0,a)})})(jQuery);
\ No newline at end of file diff --git a/jquery.matchHeight.js b/jquery.matchHeight.js index 12d6743..d0679c5 100644 --- a/jquery.matchHeight.js +++ b/jquery.matchHeight.js @@ -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 @@ -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) { @@ -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); } @@ -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 @@ -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"); + } }); })(); |