Created
February 5, 2016 18:42
-
-
Save townivan/694814dab87f033c6c1e to your computer and use it in GitHub Desktop.
Match heights with jQuery (because bootstrap won't)
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 getWindowWidth2() { | |
return window.innerWidth || document.body.clientWidth; | |
} | |
$(window).resize(function(){ | |
myMatchHeight('.sameHeight1'); // first group of things I want the same height | |
myMatchHeight('.sameHeight2'); // second group of things I want the same height | |
}); | |
$(window).load(function(){ | |
myMatchHeight('.sameHeight1'); | |
myMatchHeight('.sameHeight2'); | |
}); | |
function myMatchHeight(x){ | |
var w = getWindowWidth2(); // more accurate than $(window).width(); | |
//console.log('current width: ' + w); | |
if (w >= 768){ // no need to resize if stacking on mobile | |
var h = 0; | |
$(x).each(function(){ // loop through each element with this class | |
$(this).css({'height':'auto'}); // reset so it's not based on your adjusted height | |
if($(this).outerHeight() > h){ // check to see if we have a new height champion | |
h = $(this).outerHeight(); | |
} | |
}); | |
$(x).css({'height':h}); // set all the guys with this class to the height of the tallest | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment