如何在轮播中使横向和纵向图像完全响应且高度相同?

Posted

技术标签:

【中文标题】如何在轮播中使横向和纵向图像完全响应且高度相同?【英文标题】:How to make landscape and portrait images compeletely responsive and same height within carousel? 【发布时间】:2021-01-22 20:58:38 【问题描述】:

我有一个引导轮播,里面有纵向和横向图像。(宽屏)

正如您在上面看到的,每个图像都有不同的大小。轮播高度自动设置为最高图像的高度。这种行为不是我想要的,因为在这种情况下,当我缩小窗口时,顶部有空白区域并且如下图的底部(手机屏幕) 为了避免图像周围的这些间隙,我认为所有图像的高度应该相同,并且轮播的高度应该设置为图像的最低高度而不是最高。但我不知道该怎么做

期望状态(宽屏和移动)

<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/4.5.2/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
  <style>
  /* Make the image fully responsive */
  .carousel-inner img 
    width: 100%;
    height: 100%;
  
  
  div.carousel-item 
  background-size:contain;
  background-position:center center;
  background-repeat:no-repeat no-repeat;
  width:100%;
  height:100%;
  
  </style>
</head>
<body>

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

  <!-- Indicators -->
  <ul class="carousel-indicators">
    <li data-target="#demo" data-slide-to="0" class="active"></li>
    <li data-target="#demo" data-slide-to="1"></li>
    <li data-target="#demo" data-slide-to="2"></li>
  </ul>
  
  <!-- The slideshow -->
  <div class="carousel-inner">
    <div class="carousel-item active" style="background-image:url(https://hips.hearstapps.com/hmg-prod/amv-prod-cad-assets/images/14q3/612022/2015-ford-mustang-gt-instrumented-test-review-car-and-driver-photo-623408-s-original.jpg?fill=2:1&resize=1200:*);" >
    </div>
    <div class="carousel-item" style="background-image:url(https://static.wixstatic.com/media/61eb8a_19b3e8eaa1d44c16b7954f0d0b1ffb15~mv2.jpg/v1/fill/w_530,h_796,al_c,q_90,usm_0.66_1.00_0.01/61eb8a_19b3e8eaa1d44c16b7954f0d0b1ffb15~mv2.webp);" >
    </div>
    <div class="carousel-item" style="background-image:url(https://www.canon.com.tr/media/PCA%20Exercise%20-%20Landscape%20Photography%20exercise-landscape-photos-opener-05_1200%20x%20400_tcm123-1444470.jpg);" >
    </div>
  </div>
  
  <!-- Left and right controls -->
  <a class="carousel-control-prev" href="#demo" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#demo" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>
</div>

</body>
</html>

【问题讨论】:

您是否尝试过将图像包装在 div 类中并已使用 object-fit 类(如 contain)? 是的,我有,但没有工作 如果所有图像的高度都相同,您希望对可用宽度而言太宽的图像发生什么情况?应该只在左右两侧裁剪吗? 为什么不使用固定的像素宽度和高度? @AHaworth 正如我在期望的状态中提到的,太宽的图像应该和肖像的一样小,以保持高度相同 【参考方案1】:

我运行了您的代码,但没有显示任何内容,因此我在每个 carousel-item 中创建了一个新 div,并对其应用了一些 CSS 样式以满足您的要求。但是对于窄图像,我只使用了img 标签。

这是您想要的吗:https://www.codeply.com/p/BLEXWO5Dhq?

        .att2 
            background-size: cover;
            height: 400px;
            width: auto;
            background-repeat: no-repeat;
            background-position: 50% 50%;
        
     <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

</head>

<body>  
       <div class="container">
       <div class="row">
            <div class="col">
                <div id="carouselExampleIndicators1" class="carousel slide" data-ride="carousel">
                    <ol class="carousel-indicators">
                        <li data-target="#carouselExampleIndicators1" data-slide-to="0" class="active"></li>
                        <li data-target="#carouselExampleIndicators1" data-slide-to="1"></li>
                        <li data-target="#carouselExampleIndicators1" data-slide-to="2"></li>
                    </ol>
                    <div class="carousel-inner">
                        <div class="carousel-item active" >
                            <div class="att2" style="background-image: url('https://www.canon.com.tr/media/PCA%20Exercise%20-%20Landscape%20Photography%20exercise-landscape-photos-opener-05_1200%20x%20400_tcm123-1444470.jpg')"></div>
                        </div>
                        <div class="carousel-item" >
                            <img src="https://static.wixstatic.com/media/61eb8a_19b3e8eaa1d44c16b7954f0d0b1ffb15~mv2.jpg/v1/fill/w_530,h_796,al_c,q_90,usm_0.66_1.00_0.01/61eb8a_19b3e8eaa1d44c16b7954f0d0b1ffb15~mv2.webp"
                            class="d-block"  style="margin: auto; height: 400px;">
                            <!-- <div class="att2-x" style="background-image: url('https://static.wixstatic.com/media/61eb8a_19b3e8eaa1d44c16b7954f0d0b1ffb15~mv2.jpg/v1/fill/w_530,h_796,al_c,q_90,usm_0.66_1.00_0.01/61eb8a_19b3e8eaa1d44c16b7954f0d0b1ffb15~mv2.webp')"></div> -->
                        </div>
                        <div class="carousel-item" >
                            <div class="att2" style="background-image: url('https://hips.hearstapps.com/hmg-prod/amv-prod-cad-assets/images/14q3/612022/2015-ford-mustang-gt-instrumented-test-review-car-and-driver-photo-623408-s-original.jpg');"></div>
                        </div>
                    </div>
                    <a class="carousel-control-prev" href="#carouselExampleIndicators1" 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="#carouselExampleIndicators1" role="button" data-slide="next">
                        <span class="carousel-control-next-icon" aria-hidden="true"></span>
                        <span class="sr-only">Next</span>
                    </a>
                </div>
            </div>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
        integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
        crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"
        integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"
        integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
        crossorigin="anonymous"></script>

【讨论】:

在全页模式下查看 sn-p 以正确查看结果

以上是关于如何在轮播中使横向和纵向图像完全响应且高度相同?的主要内容,如果未能解决你的问题,请参考以下文章

图像滑块:保持所有图像的高度相同,同时保持滑块响应

在轮播中反应原生视频

如何固定自定义轮播的大小

Rails 应用程序中的引导轮播,显示所有图像,不显示缩略图。在轮播中显示数据库中的图像

引导轮播中的响应式图像

sencha touch 2 在轮播中滑动到下一张卡片时停止音频?