Twitter Bootstrap carousel different height images cause bouncing arrows

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

    It looks like bootstrap less/CSS forces an automatic height to avoid stretching the image when the width has to change to be responsive. I switched it around to make the width auto and fix the height.

    <div class="item peopleCarouselImg">
      <img src="http://upload.wikimedia.org/...">
      ...
    </div>
    

    I then define img with a class peopleCarouselImg like this:

    .peopleCarouselImg img {
      width: auto;
      height: 225px;
      max-height: 225px;
    }
    

    I fix my height to 225px. I let the width automatically adjust to keep the aspect ratio correct.

    This seems to work for me.

    0 讨论(0)
  • 2020-12-07 17:00

    You can also use this code to adjust to all carousel images.

    .carousel-item{
        width: 100%; /*width you want*/
        height: 500px; /*height you want*/
        overflow: hidden;
    }
    .carousel-item img{
        width: 100%;
        height: 100%;
        object-fit: cover;
    }
    
    0 讨论(0)
  • 2020-12-07 17:04

    The solution given earlier leads to a situation where images may be too small for the carousel box. A proper solution to the problem of bumping controls is to override Bootstraps CSS.

    Original code:

    .carousel-control {
    top: 40%;
    }
    

    Override the variable with a fixed value inside your own stylesheet (300px worked in my design):

    .carousel-control {
    top: 300px;
    }
    

    Hopefully this will solve your problem.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-07 17:05
     .className{
     width: auto;
      height: 200px;
      max-height: 200px;
       max-width:200px
       object-fit: contain;
    }
    
    0 讨论(0)
  • 2020-12-07 17:10

    Include this JavaScript in your footer (after loading jQuery):

    $('.item').css('min-height',$('.item').height());
    
    0 讨论(0)
提交回复
热议问题