Skip to content

Instantly share code, notes, and snippets.

@imhuytq
Last active December 25, 2015 22:39
Show Gist options
  • Save imhuytq/7051858 to your computer and use it in GitHub Desktop.
Save imhuytq/7051858 to your computer and use it in GitHub Desktop.
Float height auto adjustment
// Require jQuery
function floatHeightAutoAdjustment(selector) {
// index in the row
var _line = new Array;
// Get all div
$(selector).each(function (index, elm) {
var _curElm = $(elm) // Current div
, _curElmOffsetTop = _curElm.offset().top // Position of current row
, _curElmHeight = _curElm.height() || 0
, _enable = false; // Enable adjustment
// If index list does not exist
if (!_line[_curElmOffsetTop]) {
_line[_curElmOffsetTop] = new Array;
}
// If index list count > 0
if (_line[_curElmOffsetTop].length > 0) {
_enable = true;
}
if (_enable) {
$.each(_line[_curElmOffsetTop], function (index, value) {
var _prevElm = $(selector).eq(value) // Prev div
, _prevElmHeight = _prevElm.height(); // Height of prev div
// If height of current div < Height of prev div
if (_curElmHeight < _prevElmHeight) {
_curElm.height(_prevElmHeight);
// Or height of current div > Height of prev div
} else if (_curElmHeight > _prevElmHeight) {
_prevElm.height(_curElmHeight);
}
});
} else {
_curElm.css({
'height': 'auto'
});
}
// Save index of current div to index list
_line[_curElm.offset().top].push(index);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment