diff --git a/jquery.matchHeight.js b/jquery.matchHeight.js index b6db590..ee2fa2a 100644 --- a/jquery.matchHeight.js +++ b/jquery.matchHeight.js @@ -5,6 +5,48 @@ */ (function($) { + /* + * rows utility function + * returns array of jQuery selections representing each row + * (as displayed after float wrapping applied by browser) + */ + + var _rows = function(elements) { + var tolerance = 1, + $elements = $(elements), + lastTop = null, + rows = []; + + // group elements by their top position + $elements.each(function(){ + var $that = $(this), + top = $that.offset().top - _parse($that.css('margin-top')), + lastRow = rows.length > 0 ? rows[rows.length - 1] : null; + + if (lastRow === null) { + // first item on the row, so just push it + rows.push($that); + } else { + // if the row top is the same, add to the row group + if (Math.floor(Math.abs(lastTop - top)) <= tolerance) { + rows[rows.length - 1] = lastRow.add($that); + } else { + // otherwise start a new row group + rows.push($that); + } + } + + // keep track of the last row top + lastTop = top; + }); + + return rows; + }; + + var _parse = function(value) { + // parse value and convert NaN to 0 + return parseFloat(value) || 0; + }; $.fn.matchHeight = function(byRow) { @@ -202,48 +244,4 @@ // update heights on load and resize events $(window).bind('load resize orientationchange', $.fn.matchHeight._update); - - /* - * rows utility function - * returns array of jQuery selections representing each row - * (as displayed after float wrapping applied by browser) - */ - - var _rows = function(elements) { - var tolerance = 1, - $elements = $(elements), - lastTop = null, - rows = []; - - // group elements by their top position - $elements.each(function(){ - var $that = $(this), - top = $that.offset().top - _parse($that.css('margin-top')), - lastRow = rows.length > 0 ? rows[rows.length - 1] : null; - - if (lastRow === null) { - // first item on the row, so just push it - rows.push($that); - } else { - // if the row top is the same, add to the row group - if (Math.floor(Math.abs(lastTop - top)) <= tolerance) { - rows[rows.length - 1] = lastRow.add($that); - } else { - // otherwise start a new row group - rows.push($that); - } - } - - // keep track of the last row top - lastTop = top; - }); - - return rows; - }; - - var _parse = function(value) { - // parse value and convert NaN to 0 - return parseFloat(value) || 0; - }; - })(jQuery);