summaryrefslogtreecommitdiffstats
path: root/jquery.matchHeight.js
diff options
context:
space:
mode:
Diffstat (limited to 'jquery.matchHeight.js')
-rw-r--r--jquery.matchHeight.js98
1 files changed, 58 insertions, 40 deletions
diff --git a/jquery.matchHeight.js b/jquery.matchHeight.js
index 0f55b6c..25d0e6a 100644
--- a/jquery.matchHeight.js
+++ b/jquery.matchHeight.js
@@ -13,6 +13,16 @@
_updateTimeout = -1;
/*
+ * _parse
+ * value parse utility function
+ */
+
+ var _parse = function(value) {
+ // parse value and convert NaN to 0
+ return parseFloat(value) || 0;
+ };
+
+ /*
* _rows
* utility function returns array of jQuery selections representing each row
* (as displayed after float wrapping applied by browser)
@@ -51,16 +61,6 @@
};
/*
- * _parse
- * value parse utility function
- */
-
- var _parse = function(value) {
- // parse value and convert NaN to 0
- return parseFloat(value) || 0;
- };
-
- /*
* _parseOptions
* handle plugin options
*/
@@ -68,8 +68,9 @@
var _parseOptions = function(options) {
var opts = {
byRow: true,
- remove: false,
- property: 'height'
+ property: 'height',
+ target: null,
+ remove: false
};
if (typeof options === 'object') {
@@ -110,8 +111,9 @@
return this;
}
- if (this.length <= 1)
+ if (this.length <= 1 && !options.target) {
return this;
+ }
// keep track of this group so we can re-apply later on load and resize events
matchHeight._groups.push({
@@ -162,7 +164,7 @@
$hiddenParents.css('display', 'block');
// get rows if using byRow, otherwise assume one row
- if (opts.byRow) {
+ if (opts.byRow && !opts.target) {
// must first force an arbitrary equal height so floating elements break evenly
$elements.each(function() {
@@ -196,37 +198,48 @@
$.each(rows, function(key, row) {
var $row = $(row),
- maxHeight = 0;
+ targetHeight = 0;
- // skip apply to rows with only one item
- if (opts.byRow && $row.length <= 1) {
- $row.css(opts.property, '');
- return;
- }
+ if (!opts.target) {
+ // skip apply to rows with only one item
+ if (opts.byRow && $row.length <= 1) {
+ $row.css(opts.property, '');
+ return;
+ }
- // iterate the row and find the max height
- $row.each(function(){
- var $that = $(this),
- display = $that.css('display') === 'inline-block' ? 'inline-block' : 'block';
+ // iterate the row and find the max height
+ $row.each(function(){
+ var $that = $(this),
+ display = $that.css('display') === 'inline-block' ? 'inline-block' : 'block';
- // ensure we get the correct actual height (and not a previously set height value)
- var css = { 'display': display };
- css[opts.property] = '';
- $that.css(css);
+ // ensure we get the correct actual height (and not a previously set height value)
+ var css = { 'display': display };
+ css[opts.property] = '';
+ $that.css(css);
- // find the max height (including padding, but not margin)
- if ($that.outerHeight(false) > maxHeight)
- maxHeight = $that.outerHeight(false);
+ // find the max height (including padding, but not margin)
+ if ($that.outerHeight(false) > targetHeight) {
+ targetHeight = $that.outerHeight(false);
+ }
- // revert display block
- $that.css('display', '');
- });
+ // revert display block
+ $that.css('display', '');
+ });
+ } else {
+ // if target set, use the height of the target element
+ targetHeight = opts.target.outerHeight(false);
+ }
// iterate the row and apply the height to all elements
$row.each(function(){
var $that = $(this),
verticalPadding = 0;
+ // don't apply to a target
+ if ($that.is(opts.target)) {
+ return;
+ }
+
// handle padding and border correctly (required when not using border-box)
if ($that.css('box-sizing') !== 'border-box') {
verticalPadding += _parse($that.css('border-top-width')) + _parse($that.css('border-bottom-width'));
@@ -234,7 +247,7 @@
}
// set the height (accounting for padding and border)
- $that.css(opts.property, maxHeight - verticalPadding);
+ $that.css(opts.property, targetHeight - verticalPadding);
});
});
@@ -245,8 +258,9 @@
});
// restore scroll position if enabled
- if (matchHeight._maintainScroll)
+ if (matchHeight._maintainScroll) {
$(window).scrollTop((scrollTop / htmlHeight) * $('html').outerHeight(true));
+ }
return this;
};
@@ -262,7 +276,8 @@
// generate groups by their groupId set by elements using data-match-height
$('[data-match-height], [data-mh]').each(function() {
var $this = $(this),
- groupId = $this.attr('data-match-height') || $this.attr('data-mh');
+ groupId = $this.attr('data-mh') || $this.attr('data-match-height');
+
if (groupId in groups) {
groups[groupId] = groups[groupId].add($this);
} else {
@@ -282,15 +297,17 @@
*/
var _update = function(event) {
- if (matchHeight._beforeUpdate)
+ if (matchHeight._beforeUpdate) {
matchHeight._beforeUpdate(event, matchHeight._groups);
+ }
$.each(matchHeight._groups, function() {
matchHeight._apply(this.elements, this.options);
});
- if (matchHeight._afterUpdate)
+ if (matchHeight._afterUpdate) {
matchHeight._afterUpdate(event, matchHeight._groups);
+ }
};
matchHeight._update = function(throttle, event) {
@@ -299,8 +316,9 @@
// fixes an event looping bug in IE8
if (event && event.type === 'resize') {
var windowWidth = $(window).width();
- if (windowWidth === _previousResizeWidth)
+ if (windowWidth === _previousResizeWidth) {
return;
+ }
_previousResizeWidth = windowWidth;
}