Last active
April 12, 2021 12:30
-
-
Save cezarignat/33a32ae4424e43e9faf0253ded367520 to your computer and use it in GitHub Desktop.
Make all the elements the same height (using the biggest value)
This file contains hidden or 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
/* | |
* Dependencies: jQuery 1.x / 2.x / 3.x | |
* | |
* Make all the elements the height of the biggest one | |
* elementsSelector is the general selector used to get all the elements | |
* that you want to equalise. e.g '#foo li' | |
* | |
* Use the extra argument to add extra pixels to the final height | |
* | |
* | |
*/ | |
function equalElements(elementsSelector, extra) { | |
if(typeof(extra) == 'undefined') { | |
extra = 0; | |
} | |
// Get an array of all element heights | |
var elementHeights = jQuery(elementsSelector).map(function() { | |
return jQuery(this).height(); | |
}).get(); | |
// Math.max takes a variable number of arguments | |
// `apply` is equivalent to passing each height as an argument | |
// Set each height to the max height | |
jQuery(elementsSelector).height(Math.max.apply(null, elementHeights) + extra); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment