具有固定高度的自举转盘:根据图像尺寸水平或垂直居中放置图像 [英] Bootstrap carousel with fixed height: center image horizontally or vertically based on image dimensions

查看:83
本文介绍了具有固定高度的自举转盘:根据图像尺寸水平或垂直居中放置图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个固定高度的自转传送带。这是CSS:

I have a bootstrap carousel with a fixed height. Here is the CSS:

.carousel-custom .carousel-outer {
  position: relative;
}


@media(min-width: 992px){
    .carousel-custom {
      margin-top: 20px;

    .carousel-inner {
      height: auto;
    .item {
        height:500px;
        line-height:300px;
      }
    }
  }
}

@media(max-width: 991px){
.carousel-custom {
      margin-top: 20px;

    .carousel-inner {
      height: auto;
    .item {
        height:300px;
        line-height:300px;
        text-align:center;

      }
    }
  }
}

这是我的标记:

    <div id="carousel-custom-1188" class="carousel slide carousel-custom" data-ride="carousel">
      <div class="carousel-outer">
        <!-- Wrapper for slides -->
        <div class="carousel-inner" role="listbox">
            <div class="item active">
              <img class="img-responsive" src="http://res.cloudinary.com/dltbqhact/image/upload/v1459261752/w8edcuvz1yl3uc4g4o34.jpg" alt="Jinallzupvfazqqr67nd">
              <div class="carousel-caption"></div>
            </div>
            <div class="item">
              <img class="img-responsive" src="http://res.cloudinary.com/dltbqhact/image/upload/v1459175684/w4ueot8o49dh2fyulv0x.jpg" alt="K1yov5hpur8mgsb9r15p">
              <div class="carousel-caption"></div>
            </div>
            <div class="item">
              <img class="img-responsive" src="http://res.cloudinary.com/dltbqhact/image/upload/v1459178926/fwlmuroj2wlz7czrsha0.jpg" alt="Lqfandhmutdkppjrl932">
              <div class="carousel-caption"></div>
            </div>
        </div>

        <!-- Controls -->
        <a class="left carousel-control" href="#carousel-custom-1188" 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="#carousel-custom-1188" role="button" data-slide="next">
          <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
          <span class="sr-only">Next</span>
        </a>

        <!-- Indicators -->
        <ol class="carousel-indicators  mCustomScrollbar">
            <li data-target="#carousel-custom-1188" data-slide-to="0" class="active">
              <img style="height:50px; width: 50px;" class="img-responsive" src="http://res.cloudinary.com/dltbqhact/image/upload/v1459268139/jinallzupvfazqqr67nd.png" alt="Jinallzupvfazqqr67nd">
            </li>
            <li data-target="#carousel-custom-1188" data-slide-to="1" class="">
              <img style="height:50px; width: 50px;" class="img-responsive" src="http://res.cloudinary.com/dltbqhact/image/upload/v1459268146/k1yov5hpur8mgsb9r15p.png" alt="K1yov5hpur8mgsb9r15p">
            </li>
            <li data-target="#carousel-custom-1188" data-slide-to="2" class="">
              <img style="height:50px; width: 50px;" class="img-responsive" src="http://res.cloudinary.com/dltbqhact/image/upload/v1459268157/lqfandhmutdkppjrl932.png" alt="Lqfandhmutdkppjrl932">
            </li>
        </ol>
      </div>
    </div>

问题是我的图像非常宽,其他图像非常狭窄和高,而其他图像具有在同一轮播带子中具有良好的口粮高度/宽度。

The issue is that I have images that are very wide, others very narrow and high, and others with a good ration height/width in the same carousel.

我希望宽图像垂直居中(宽度为100%),高图像水平居中(高度为100%),并且正常图像(高/宽比适当)填充了所有轮播。

I'd like to have the wide images centered vertically (with a width of a 100%), high images centered horizontally (with a height of 100%) and "normal" images (with a decent ratio height/width) filling all the carousel.

这是我要尝试的操作(第一个图像是一个具有非常宽的图像的示例,第二个图像是具有很高的图像的示例,最后一个具有体面的比率)。

Here is what I'm trying to do (first image is an example with a very wide image, second image with a very high one, and last one has a "decent" ratio).

如何实现这个 ?我已经尝试过使Bootstrap的Carousel既居中又响应?等在Google上发现了一些技巧,但没有做到这一点。

How could I achieve this ? I've tried Make Bootstrap's Carousel both center AND responsive? and other tricks found on Google but none did this.

推荐答案

由于 .item 元素是相对定位的,因此您只需定位 img 元素。将 .item 元素设置为固定高度,将有助于垂直和水平对齐内部的图像。

Since the .item elements are relative positioned you could just position the img elements absolutely. Giving the .item elements a fixed height, will help to vertically and horizontally align the images inside.

I'确保有很多不同的方法可以做到这一点,这是一个示例。希望能帮助到你。

I'm sure there are lots of different ways to do this, here is one example. Hope it helps.

@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');

body {
  background-color: black;
}

.carousel-inner > .item {
  height: 100vh;
}

.carousel-inner > .item > img {
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  max-height: 800px;
  width: auto;
}

.carousel-control.left,
.carousel-control.right {
  background-image: none;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<div class="container">

  <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">

    <!-- indicators -->
    <ol class="carousel-indicators">
      <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
      <li data-target="#carousel-example-generic" data-slide-to="1"></li>
      <li data-target="#carousel-example-generic" data-slide-to="2"></li>
    </ol>

    <!-- inner -->
    <div class="carousel-inner" role="listbox">
      <div class="item active">
        <img src="http://placehold.it/1200x600/2c3e50/000" alt="">
      </div>
      <div class="item">
        <img src="http://placehold.it/600x1200/d35400/000" alt="">
      </div>
      <div class="item">
        <img src="http://placehold.it/600x600/c0392b/000" alt="">
      </div>
    </div>

    <!-- controls -->
    <a class="left carousel-control" href="#carousel-example-generic" 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="#carousel-example-generic" role="button" data-slide="next">
      <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
    
  </div>

</div>

这篇关于具有固定高度的自举转盘:根据图像尺寸水平或垂直居中放置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆