Twitter Bootstrap carousel different height images cause bouncing arrows

后端 未结 14 988
刺人心
刺人心 2020-12-07 16:11

I\'ve been using Bootstrap\'s carousel class and it has been straightforward so far, however one problem I\'ve had is that images of different heights cause the arrows to bo

14条回答
  •  抹茶落季
    2020-12-07 17:10

    This jQuery function worked best for me. I'm using bootstrap 4 within a WordPress theme and I've used the full jQuery instead of jQuery slim.

    // 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('#latest-blog-posts .carousel-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('#latest-blog-posts .carousel-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();
    });
    

    Source:

    https://gist.github.com/mbacon40/eff3015fe96582806da5

提交回复
热议问题