Skip to content

Instantly share code, notes, and snippets.

@ykhs
Created May 23, 2012 02:54
Show Gist options
  • Save ykhs/2773001 to your computer and use it in GitHub Desktop.
Save ykhs/2773001 to your computer and use it in GitHub Desktop.
要素の高さ揃えたい
function flattenHeight(elements) {
var maxHeight, i, l, defaultView;
maxHeight = 0;
i = 0;
l = elements.length;
defaultView = document.defaultView || {};
if (defaultView.getComputedStyle) {
for (i = 0; i < l; i++) {
maxHeight = Math.max(
maxHeight,
parseInt(document.defaultView.getComputedStyle(elements[i], '').getPropertyValue('height'), 10)
);
}
for (i = 0; i < l; i++) {
elements[i].style.height = maxHeight -
parseInt(document.defaultView.getComputedStyle(elements[i], '').getPropertyValue('border-top-width'), 10) -
parseInt(document.defaultView.getComputedStyle(elements[i], '').getPropertyValue('border-bottom-width'), 10) + 'px';
}
} else if (elements[0].currentStyle) {
for (i = 0; i < l; i++) {
maxHeight = Math.max(maxHeight, (function() {
var element = elements[i],
rect = element.getBoundingClientRect();
return (element.offsetHeight || rect.bottom - rect.top);
})());
}
for (i = 0; i < l; i++) {
elements[i].style.height = (function() {
var element = elements[i],
style = element.currentStyle;
return maxHeight -
(parseInt(style.borderTopWidth, 10) || 0) -
(parseInt(style.borderBottomWidth, 10) || 0) -
(parseInt(style.paddingTop, 10) || 0) -
(parseInt(style.paddingBottom, 10) || 0);
})();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment