bootstrap图像滑块不同的图像大小

时间:2017-01-20 07:49:11

标签: css image twitter-bootstrap image-resizing

我有一些使用bootstrap的经验,我想使用以下方法创建图像滑块:

http://www.w3schools.com/bootstrap/bootstrap_carousel.asp

https://startbootstrap.com/template-overviews/business-casual/

示例中的一切都很好。如果我想使用不同大小的图像,就会出现问题:

  • 宽度还可以
  • 高度根据图像而变化。

我发现了许多类似的主题,但没有一个适合我。

是否可以有固定的比例 - 不过照片尺寸?只使用css图像拉伸甚至“裁剪”图像?

代码

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <style>
  .carousel-inner > .item > img,
  .carousel-inner > .item > a > img {
      width: 70%;
      margin: auto;
  }
  </style>
</head>
<body>

<div class="container">
  <br>
  <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>
      <li data-target="#myCarousel" data-slide-to="3"></li>
    </ol>

    <!-- Wrapper for slides -->
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <img src="img_chania.jpg" alt="Chania" width="460" height="345">
      </div>

      <div class="item">
        <img src="img_chania2.jpg" alt="Chania" width="460" height="345">
      </div>

      <div class="item">
        <img src="img_flower.jpg" alt="Flower" width="460" height="345">
      </div>

      <div class="item">
        <img src="img_flower2.jpg" alt="Flower" width="460" height="345">
      </div>
    </div>

    <!-- Left and right controls -->
    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
      <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</div>

</body>
</html>

1 个答案:

答案 0 :(得分:5)

对于某些网站,我不得不为这类问题制作一个技巧。

我只是删除了img标签,并将其设置为项目背景。

Bootply http://www.bootply.com/AVNXeL4Jtw

<强> HTML

 <div class="carousel-inner" role="listbox">
      <div class="item active" style="background-image: url(//placehold.it/1024x700)  ">
      </div>

      <div class="item" style="background-image: url(//placehold.it/100x100)  ">
      </div>

      <div class="item" style="background-image: url(//placehold.it/1024x700)  ">
      </div>

      <div class="item" style="background-image: url(//placehold.it/200x200)  ">
      </div>
    </div>

<强> CSS

.carousel-inner .item{ 
   height:500px; 
   background-size:cover;
   background-position: center center;
}