From 71a51511b7afa3ba016d908b330751b2b522c123 Mon Sep 17 00:00:00 2001 From: liabru Date: Wed, 17 Sep 2014 00:47:24 +0100 Subject: [PATCH] fix handling of hidden elements by row, closes #28 --- jquery.matchHeight-min.js | 10 +++++----- jquery.matchHeight.js | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/jquery.matchHeight-min.js b/jquery.matchHeight-min.js index ef36c10..b2945b3 100644 --- a/jquery.matchHeight-min.js +++ b/jquery.matchHeight-min.js @@ -3,9 +3,9 @@ * http://brm.io/jquery-match-height/ * License: MIT */ -(function(b){var k=-1,e=-1,m=function(a){var d=null,c=[];b(a).each(function(){var a=b(this),f=a.offset().top-h(a.css("margin-top")),g=0=Math.floor(Math.abs(d-f))?c[c.length-1]=g.add(a):c.push(a);d=f});return c},h=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],f=b(window).scrollTop(),g=b("html").outerHeight(!0);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", -"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;if(!(d&&1>=e.length)){var g=e.parents().add(e).filter(":hidden");g.css({display:"block"});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:""})});g.css({display:""}); -e.each(function(){var a=b(this),c=0;"border-box"!==a.css("box-sizing")&&(c+=h(a.css("border-top-width"))+h(a.css("border-bottom-width")),c+=h(a.css("padding-top"))+h(a.css("padding-bottom")));a.css("height",f-c)})}});b.fn.matchHeight._maintainScroll&&b(window).scrollTop(f/g*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===k)return;k=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, +(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=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","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 diff --git a/jquery.matchHeight.js b/jquery.matchHeight.js index fb3411e..004b10a 100644 --- a/jquery.matchHeight.js +++ b/jquery.matchHeight.js @@ -123,6 +123,10 @@ var scrollTop = $(window).scrollTop(), htmlHeight = $('html').outerHeight(true); + // temporarily must force hidden parents visible + var $hiddenParents = $elements.parents().filter(':hidden'); + $hiddenParents.css('display', 'block'); + // get rows if using byRow, otherwise assume one row if (byRow) { @@ -164,10 +168,6 @@ if (byRow && $row.length <= 1) return; - // ensure elements are visible to prevent 0 height - var hiddenParents = $row.parents().add($row).filter(':hidden'); - hiddenParents.css({ 'display': 'block' }); - // iterate the row and find the max height $row.each(function(){ var $that = $(this), @@ -181,12 +181,9 @@ maxHeight = $that.outerHeight(false); // revert display block - $that.css({ 'display': '' }); + $that.css('display', ''); }); - // revert display block - hiddenParents.css({ 'display': '' }); - // iterate the row and apply the height to all elements $row.each(function(){ var $that = $(this), @@ -203,6 +200,9 @@ }); }); + // revert hidden parents + $hiddenParents.css('display', ''); + // restore scroll position if enabled if ($.fn.matchHeight._maintainScroll) $(window).scrollTop((scrollTop / htmlHeight) * $('html').outerHeight(true));