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

问题描述 投票:0回答:1

我在 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>

twitter-bootstrap bootstrap-4
1个回答
10
投票

这背后的原因

让我们先看看你在这里做什么:

<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>

仔细查看您在图像元素上添加的类,

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>

现在让我们添加一些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 */
}

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

是时候在页面加载的初始时间检查轮播的高度了,因此,每当文档准备好时,我们就使用

#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'))
});

片段

现在看一下片段:

$(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'))
});
.carousel-item img {
  margin: 0 auto;
  width: auto;
}
<!-- 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" 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>

© www.soinside.com 2019 - 2024. All rights reserved.