如何在Bootstrap 4中将不同尺寸的图像放入轮播中?

Gih*_*ann 8 twitter-bootstrap bootstrap-4

我在Bootstrap 4的转盘中放入不同尺寸的图像时遇到麻烦。如何在转盘中放入不同尺寸的图像。

例如,我想放置不同大小的图像,如下面的代码所示。在当前情况下,当我这样做时,它仅占用该图像的大小。

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">


<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous">
</script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous">
</script>




<div class="container">
  <div id="carouselwithIndicators" class="carousel slide w-50" data-ride="carousel">
    <ol class="carousel-indicators">
      <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
      <li data-target="#carouselExampleIndicators" data-slide-to="2s"></li>
    </ol>

    <div class=" carousel-inner">
      <div class="carousel-item active">
        <img class="d-block w-100" src="https://www.tutorialspoint.com/bootstrap/images/slide1.png" alt="First slide">
      </div>

      <div class="carousel-item">
        <img class="d-block w-100" src="https://i.pinimg.com/originals/fb/3f/e8/fb3fe82c671831afb614ac18cd69e11e.jpg" alt="Second slide">
      </div>
      <div class="carousel-item">
        <img class="d-block w-100" src="https://www.tutorialspoint.com/bootstrap/images/slide3.png" alt="Third slide">
      </div>
    </div>

    <a class="carousel-control-prev" href="#carouselwithIndicators" role="button" data-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>

    <a class="carousel-control-next" href="#carouselwithIndicators" role="button" data-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Tow*_*kir 8

背后的原因

首先让我们看看您在做什么:

<div class = "carousel-item">
    <img class = "d-block w-100" src = "https://i.pinimg.com/originals/fb/3f/e8/fb3fe82c671831afb614ac18cd69e11e.jpg" alt = "Second slide">
</div>
Run Code Online (Sandbox Code Playgroud)

仔细查看您在图像元素上添加的类,这d-block意味着图像display将被设置为,block并且w-100宽度将为100%。在这里阅读更多。

现在,您的第二张(狗)图像的高度与宽度相比太大了,并且没有max-height设置,它将扩展轮播。

解:

首先,让我们w-100从狗图像项中删除该类。

<div class = "carousel-item">
    <img class = "d-block" src = "https://i.pinimg.com/originals/fb/3f/e8/fb3fe82c671831afb614ac18cd69e11e.jpg" alt = "Second slide">
</div>
Run Code Online (Sandbox Code Playgroud)

现在让我们添加一些css:

.carousel-item img {
  margin: 0 auto; /* this will align the image to center. */
  width: auto; /* for those images which don't have a width specified, but has a max-height will be auto adjusted */
}
Run Code Online (Sandbox Code Playgroud)

如上所述,您已经了解了哪个属性将处理哪些值。

现在是在页面加载初始时检查转盘高度的时候了,因此,每当文档准备就绪时,我们都会使用#carouselwithIndicators选择器来检查转盘高度。然后将此高度值应用于轮播中的所有图像。

这是JS:

$(document).ready(function(){
  console.log($('#carouselwithIndicators').css('height')); // check the initial height of the carousel;

  // now apply this height as a max-height on all the image items; css will handle the rest;
  $('#carouselwithIndicators').find('.carousel-item img').css('max-height', $('#carouselwithIndicators').css('height'))
});
Run Code Online (Sandbox Code Playgroud)

片段

现在看一下代码片段:

<div class = "carousel-item">
    <img class = "d-block w-100" src = "https://i.pinimg.com/originals/fb/3f/e8/fb3fe82c671831afb614ac18cd69e11e.jpg" alt = "Second slide">
</div>
Run Code Online (Sandbox Code Playgroud)
<div class = "carousel-item">
    <img class = "d-block" src = "https://i.pinimg.com/originals/fb/3f/e8/fb3fe82c671831afb614ac18cd69e11e.jpg" alt = "Second slide">
</div>
Run Code Online (Sandbox Code Playgroud)
.carousel-item img {
  margin: 0 auto; /* this will align the image to center. */
  width: auto; /* for those images which don't have a width specified, but has a max-height will be auto adjusted */
}
Run Code Online (Sandbox Code Playgroud)