如何使引导旋转木马保持图像纵横比
How to make Bootstrap Carousel maintain image aspect ratio?
目前我使用Bootstrap模板中的开箱即用旋转木马,效果很好,但当我添加图像并调整浏览器窗口大小时,它不会保持图像纵横比。。。相反,它水平地挤压图像。
现在,我已经尝试了许多变体,以在调整窗口大小时保持图像纵横比,下面的代码是我得到的最接近的代码。也就是说,这个代码确实保持了纵横比,但当窗口被调整为手机大小时,图像高度基本上是50px,你无法判断图像是什么。
当你调整浏览器大小时,我希望旋转木马的行为与本网站上的一样:http://teekay.com/
我的当前代码:旋转木马:
<!-- carousel-->
<div id="myCarousel" data-ride="carousel" class="carousel slide">
<!-- Indicators-->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<div role="listbox" class="carousel-inner">
<div class="item active"><img src="../images/carousel_resize_3.jpg" alt="First slide" class="img-responsive"/>
<div class="container">
<div class="carousel-caption">
<h1>text1</h1>
<p></p>
<p><a href="/login" role="button" class="btn btn-lg btn-warning">Sign up today</a></p>
</div>
</div>
</div>
<div class="item"><img src="../images/carousel_resize_1.jpg" alt="Second slide" class="second-slide"/>
<div class="container">
<div class="carousel-caption">
<h1>text2</h1>
<p></p>
<p><a href="#moreInfo" role="button" class="btn btn-lg btn-warning">Learn more</a></p>
</div>
</div>
</div>
<div class="item"><img src="../images/carousel_resize_2.jpg" alt="Third slide" class="third-slide"/>
<div class="container">
<div class="carousel-caption">
<h1>text3</h1>
<p></p>
<p><a href="#appInfo" role="button" class="btn btn-lg btn-warning">Browse jobs</a></p>
</div>
</div>
</div>
</div><a href="#myCarousel" role="button" data-slide="prev" class="left carousel-control"><span aria-hidden="true" class="glyphicon glyphicon-chevron-left"></span><span class="sr-only">Previous</span></a><a href="#myCarousel" role="button" data-slide="next" class="right carousel-control"><span aria-hidden="true" class="glyphicon glyphicon-chevron-right"></span><span class="sr-only">Next</span></a>
</div>
<!-- /.carousel-->
我的CSS:
/* CUSTOMIZE THE CAROUSEL
-------------------------------------------------- */
/* Carousel base class */
.carousel {
/*height: 500px;*/
margin-bottom: 60px;
}
/* Since positioning the image, we need to help out the caption */
.carousel-caption {
z-index: 10;
}
/* Declare heights because of positioning of img element */
.carousel .item {
/*height: 500px;*/
background-color: #777;
}
/*.carousel-inner > .item > img {
position: absolute;
top: 0;
left: 0;
min-width: 100%;
height: 500px;
}*/
我在许多Stack Overflow帖子中尝试过答案,但我似乎无法让旋转木马缩小,但也无法保持纵横比。我错过了什么?
谢谢你的帮助!
编辑:
我用下面的代码设法使它保持了纵横比,但现在当浏览器调整大小时,旋转木马并没有整体变小,有什么办法吗?
/* CUSTOMIZE THE CAROUSEL
-------------------------------------------------- */
/* Carousel base class */
.carousel {
height: 500px;
margin-bottom: 60px;
}
/* Since positioning the image, we need to help out the caption */
.carousel-caption {
z-index: 10;
}
/* Declare heights because of positioning of img element */
.carousel .item {
height: 500px;
background-color: #777;
}
.carousel-inner > .item > img {
position: absolute;
top: 0;
left: 0;
max-width: 100%;
height: 500px;
}
img {
height: 500px;
width: 1920px;
/*object-fit: contain;*/
object-fit: cover;
}
在最新的Bootstrap 3 { display: block; max-width: 100%; height: auto; }
中,旋转木马图像默认情况下是响应的。因此,没有必要将.img-responsive
类添加到图像中。(此外,不需要将.carousel-caption
封装在.container
中)
如果您从这里的示例标记开始,并删除额外的样式覆盖,您可以看到在不同的屏幕大小上正确地保持了纵横比。您可以使用上面的相同链接对此进行验证。
相关文章:
- 如何预加载这些图像以使此功能更平滑
- 旋转图像,使其看起来像在旋转
- Javascript创建一个图像并使其移动
- 预加载图像,使其不会't在加载过程中重新调整大小
- 如何预加载悬停状态的图像以使背景立即更改,而不是在几秒钟后更改
- HTML 5 画布:在游戏中使用绘制图像会使循环非常慢
- 如何降低谷歌地图的整体亮度,使引脚更显眼
- 在rails-ember应用程序中,我如何在css中渲染bg图像并使其适合浏览器
- 如何在kinectjs中旋转图像,使图像与左上角对齐
- 使引导弹出窗口出现在与其触发器不同的元素上
- 悬停图像会使其变暗
- 是否有可能用JavaScript/CSS动态编辑图像,使其背景变得透明?
- 图像库使第一个图像变大
- 旋转图像并使文本稍后出现在图像上
- 如何淡化背景图像,使链接/文本可见
- 我如何嵌入街景图像,使其显示视图从街道(而不是从建筑物内部)
- 如何隐藏图像并使其显示在计时器上
- 如何使引导弹出窗口出现一次页面加载
- 如何缩放背景图像,使其与浏览器大小的工作
- 如何从Flickr嵌入图像,使图像出现在网站上