如何使Bootstrap轮播图像响应式?

21

我希望保持图片的相同比例,但问题是当浏览器宽度增加时,它会被拉伸,而在缩小时则会被压缩。

我已经查看了这里的所有SO问题,但大多数都没有帮助我。

这是标记:

<!-- Carousel
================================================== -->
<div id="myCarousel" class="carousel slide" data-ride="carousel">
  <!-- 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 class="carousel-inner">
    <div class="item active">
      <img src="images/female/IMG_5053-2.jpg" 
           data-src="images/female/IMG_5053-2.jpg" alt="First slide">

这里是CSS

.carousel .item>img {
  position: absolute;
  top: 0;
  left: 0;
  min-width: 100%;
  height: 100%;
}

我尝试制作一个jsfiddle,但是无法制作。这是该页面的链接:http://maanastore.com/home.php

6个回答

14

从各自的文件中删除以下CSS规则:

在home.php文件中

.carousel .item>img {
    position: absolute;
    top: 0;
    left: 0;
    max-width: 100%;
    height: 100%;
}

在carousel.css中

.carousel-inner > .item > img {
    position: absolute;
    top: 0;
    left: 0;
    min-width: 100%;
    height: 500px;
}

同时,在carousel.css文件中,将.carousel类添加margin-top: 51px;属性,并从该类中删除height:500px属性,因为您已经有了固定的导航栏。


1
将位置设为绝对定位,轮播图从页面中消失。 - Rehan
position:relative添加到父元素。 - Ravimallya

7
我发现最好移除以下内容。
.carousel-inner > .item > img {
    position: absolute;
    top: 0;
    left: 0;
    min-width: 100%;
    height: 500px;
}

并且在.carousel中删除:

height: 500px;

在 .carousel .item 中删除

 height: 500px;

对我来说,这使得图片按照我的意愿工作,但请确保首先在你的html中插入一张图片,否则如果你在本地工作,它将会坍塌成无物。

编辑:另一个用户建议我添加以下内容
请注意,如果图片两侧甚至一侧显示灰色条纹,我建议保留

.carousel-inner > .item > img {
    min-width: 100%;
}

1
我最终所做的是在内部添加一个div,然后将div的图片背景设置为响应式。
<div class="carousel-inner" role="listbox">
    <div class="carousel-item active">
        <div class="img1">
        </div>
<!--<img class=" imgCstm " src="images/home/bng_00.jpg" alt="First slide">-->
    </div> 
</div> 


<style>
  .carousel-inner { 
     height: 100vh;
     <! --  custom hight -->
  }

  .carousel-item  { 
     height: 100%;
  }

  .img1 {
     background-image: url('../images/home/bng_00.jpg');
     height: 100%;
     width: 100%;
     background-repeat: no-repeat;
     background-attachment: fixed !important;
     background-size: cover !important;   
     background-position: center !important;
    }
</style>

0

我一直在开发这样一个项目,它可能会帮助需要响应式浏览器滑块的人。

@import url("https://fonts.googleapis.com/css?family=Josefin+Sans:300,400,600,700|Open+Sans:400,400i,700");
body {
  font-family: "Open Sans", sans-serif;
  color:black;
}

slider{
  display: block;
  width: 100%;
  height: 80%;
  overflow: hidden;
  position: absolute;
}
 
 slider > *{
  position: absolute;
  display: block;
  width:100%;
  height: 100%;
  animation: slide 8s infinite;
 }

 slide:nth-child(1){
  left:0%;
  animation-delay: -10s;
  background-image: url(../images/slider7.jpeg);
  background-size: cover;
  background-position: center;
 }

 slide:nth-child(2){
  animation-delay: -12s;
  background-image: url(../images/slider2.jpeg);
  background-size: cover;
  background-position: center;
 }


 slide:nth-child(3){
  animation-delay: -14s;
  background-image: url(../images/slider6.jpeg);
  background-size: cover;
  background-position: center;
 }
  slide:nth-child(4){
  animation-delay: -16s;
  background-image: url(../images/slider0.jpeg);
  background-size: cover;
  background-position: center;
 }


slide p{
  font-family: Comfortaa;
  font-size: 70px;
  text-align: center;
  display: inline-block;
  width: 100%;
  margin:10%;
  color:#fff;
}
slide h1{
  font-family: Comfortaa;
  font-size: 60px;
  text-align: center;
  display: inline-block;
  color:#fff;
  margin-top: 20%;
  margin-left: 10%;
}
@keyframes slide {
  0% {left:100%; width:100%;}
  5% {left:0%;}
  25% {left:0%;}
  30% {left: -100%; width: 100%;}
  30.0001%{left: -100%; width:0%;}
  100%{left: 100%; width: 0%}

}
<section class="container">
  <slider>
   <slide></slide>
      <slide><h1>Slide Text one</h1></slide>
      <slide><h1>Slide Text Two</h1></slide>
       <slide><h1>Slide Text Three</h1></slide>
  </slider> 
 </section>   

这个链接也可以为您提供更多的见解 制作响应式图片


0
您可以通过典型的使用 margin 来改善图像的居中效果。
.carousel-inner > .carousel-item > img {
margin: 0; //If you have images of very different sizes.    
max-width: 100%; }

-1

我在 bootstrap.css 的 5835 - 5847 行进行了这些调整:

.carousel-inner {
  position: relative;
  /* Removed height here */
  overflow: hidden;
}

.carousel-item {
  position: relative;
  width: 100vh;
  height: 100vh;
  display: none;
  width: 100%;
}

非常顺利地工作了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接