Twitter Bootstrap carousel different height images cause bouncing arrows

后端 未结 14 1001
刺人心
刺人心 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:05

    Here is the solution that worked for me; I did it this way as the content in the carousel was dynamically generated from user-submitted content (so we could not use a static height in the stylesheet) - This solution should also work with different sized screens:

    function updateCarouselSizes(){
      jQuery(".carousel").each(function(){
        // I wanted an absolute minimum of 10 pixels
        var maxheight=10; 
        if(jQuery(this).find('.item,.carousel-item').length) {
          // We've found one or more item within the Carousel...
          jQuery(this).carousel(); // Initialise the carousel (include options as appropriate)
          // Now we iterate through each item within the carousel...
          jQuery(this).find('.item,.carousel-item').each(function(k,v){ 
            if(jQuery(this).outerHeight()>maxheight) {
              // This item is the tallest we've found so far, so store the result...
              maxheight=jQuery(this).outerHeight();
            }
          });
          // Finally we set the carousel's min-height to the value we've found to be the tallest...
          jQuery(this).css("min-height",maxheight+"px");
        }
      });
    }
    
    jQuery(function(){
      jQuery(window).on("resize",updateCarouselSizes);
      updateCarouselSizes();
    }
    

    Technically this is not responsive, but for my purposes the on window resize makes this behave responsively.

提交回复
热议问题