我在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>
答案 0 :(得分: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>
仔细查看您在图像元素上添加的类,d-block
意味着图像display
将被设置为block
,而w-100
则将宽度设置为100%。了解更多here。
现在,第二张(狗)图像的高度要比宽度大,并且没有设置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>