Twitter Bootstrap carousel different height images cause bouncing arrows

后端 未结 14 1019
刺人心
刺人心 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 16:48

    Note that the jQuery solutions that normalize height on here may break with IE.

    After some testing (with Bootstrap 3) it looks like IE doesn't bother resizing images unless they're actually being rendered. If you make the window smaller, the image of the active item gets resized but the background images preserve their height, so the "tallest" height stays the same.

    In my case we were only rendering images on these items, and the images were only ever a few pixels off. So I opted to style all of the images with the height of the active image. Everything else gets resized to fit the height, so keep that in mind if your aspect ratios vary a lot.

        function carouselNormalization() {
            var items = $('.carousel .item');
    
            if (items.length) {
                function normalizeHeights() {
                    let activeImageHeight = items.filter('.active').find('img').height();
                    items.each(function() {
                        $(this).find('img').css('height',  activeImageHeight + 'px');
                    });
                };
                normalizeHeights();
    
                $(window).on('resize orientationchange', function() {
                    items.each(function() {
                        $(this).find('img').removeAttr('style');
                    });
                    normalizeHeights();
                });
            }
        }
        $(window).on('load', carouselNormalization);
    

提交回复
热议问题