Skip to content

Instantly share code, notes, and snippets.

@netsi1964
Created February 25, 2014 11:16
Show Gist options
  • Save netsi1964/9207088 to your computer and use it in GitHub Desktop.
Save netsi1964/9207088 to your computer and use it in GitHub Desktop.
jQuery sameHeight(selector)
// https://gist.github.com/9207088
function sameHeight(settings) {
var defaults = {
"selector": "",
"ignoreZero": true,
// If minHeight <= 0 and ignoreZero==true, no action taken
"minHeight": -1
};
var usedSettings = $.extend(defaults, settings);
if (arguments.length !== 0) {
var aElements = $(usedSettings.selector).remove("[style*='display'][style*='none']");
var bDiff = false;
var iFound = usedSettings.found = aElements.length;
var i = 0;
for (i = 0; i < iFound; i++) {
var $current = $(aElements[i]);
if ($current.css('display') !== 'none')
{
if ($current.attr("data-sameheight")==="true") {
// Remove previous set height from inline style
var style = $current.attr("style");
$current.attr("style", style.replace(/min-height:.*;/, ""));
console.log(style.replace(/min-height:.*;/, ""))
}
var iHeight = parseInt($current.css("height"), 10);
if (usedSettings.minHeight < iHeight) {
usedSettings.minHeight = iHeight;
bDiff = true;
}
}
}
if (bDiff || (usedSettings.ignoreZero) || (!usedSettings.ignoreZero && usedSettings.minHeight > 0)) {
for (i = 0; i < iFound; i++) {
$(aElements[i]).css("min-height", usedSettings.minHeight).attr("data-sameheight", true);
}
}
}
return usedSettings;
}
$(function() {
$('.tab-item').each(function(i) {
sameHeight({
"selector": ".tab-item:nth-child(" + (i + 1) + ") .description"
});
});
window.lastChanged = new Date();
$(window).on("resize", function() {
if ((new Date() - lastChanged) > 500) {
sameHeight({
"selector": ".tab-item .description"
});
window.lastChanged = new Date();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment