Last active
August 19, 2018 22:12
-
-
Save marcbacon/eff3015fe96582806da5 to your computer and use it in GitHub Desktop.
jQuery function to set all Bootstrap Carousel items to the same height.
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
| // Set all carousel items to the same height | |
| function carouselNormalization() { | |
| window.heights = [], //create empty array to store height values | |
| window.tallest; //create variable to make note of the tallest slide | |
| function normalizeHeights() { | |
| jQuery('.sc_slides .item').each(function() { //add heights to array | |
| window.heights.push(jQuery(this).outerHeight()); | |
| }); | |
| window.tallest = Math.max.apply(null, window.heights); //cache largest value | |
| jQuery('.sc_slides .item').each(function() { | |
| jQuery(this).css('min-height',tallest + 'px'); | |
| }); | |
| } | |
| normalizeHeights(); | |
| jQuery(window).on('resize orientationchange', function () { | |
| window.tallest = 0, window.heights.length = 0; //reset vars | |
| jQuery('.sc_slides .item').each(function() { | |
| jQuery(this).css('min-height','0'); //reset min-height | |
| }); | |
| normalizeHeights(); //run it again | |
| }); | |
| } | |
| jQuery( document ).ready(function() { | |
| carouselNormalization(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This didn't quite work for me because the outerHeight() reads as 0 since display:none is on a parent item. If you add class "active" to the inactive ones, then measure height, then remove active from the ones you added it to (not the first one, which is supposed to be active) it works. Your way only works if the first item has the largest height.
So this goes into the first loop:
Cheers, thanks for the code.