Created
May 23, 2012 02:54
-
-
Save ykhs/2773001 to your computer and use it in GitHub Desktop.
要素の高さ揃えたい
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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