如何将bootstrap4 modal与轮播图片匹配到页面?

时间:2019-05-05 14:27:51

标签: css css3 bootstrap-4 bootstrap-modal bootstrap-carousel

我正在尝试使垂直屏幕图像适合模式内部的屏幕 像

enter image description here

但是,相反,如果图像大于屏幕尺寸,则它会开始滚动并具有滚动功能,而水平图像都可以。

我尝试过玩.imageHeight类。

我已添加图片:

  • 当我添加max-height: 300px;时,它会挤压图像,
  • max-height 100%根本什么都没做,
  • 添加image-fluid类似乎根本不起作用,
  • 尝试了object-fit: scale-down;,但似乎使图片变小了 但屏幕仍不适合。

我在这里创建了Codepen:https://codepen.io/anon/pen/mYdyrO

.imageHeight {
  max-height: 100%;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<div id="block4">
  <div class="row mt-5">
    <div class="col-sm text-center align-middle">
      <h1>Doors</h1>
    </div>
  </div>
  <div class="row py-2">
    <div class="col-sm text-center align-self-center py-2">
      <a href="#carousel4" data-slide-to="0">
        <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
      </a>
    </div>
    <div class="col-sm text-center align-self-center py-2">
      <a href="#carousel4" data-slide-to="1">
        <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
      </a>
    </div>
    <div class="col-sm text-center align-self-center py-2">
      <a href="#carousel4" data-slide-to="2">
        <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
      </a>
    </div>
    <div class="col-sm text-center align-self-center py-2">
      <a href="#carousel4" data-slide-to="3">
        <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
      </a>
    </div>
    <div class="col-sm text-center align-self-center py-2">
      <a href="#carousel4" data-slide-to="4">
        <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
      </a>
    </div>
    <div class="col-sm text-center align-self-center py-2">
      <a href="#carousel4" data-slide-to="5">
        <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
      </a>
    </div>
  </div>
  <div id="lightbox4" class="modal fade" role="dialog" style="display: none;" aria-hidden="true">
    <div class="modal-dialog modal-xl modalCenter" role="document">
      <!--modal-xl modal-lg-->
      <div class="modal-content">
        <div class="modal-body">
          <div class="carousel slide" data-ride="carousel" id="carousel4" data-interval="false">
            <ol class="carousel-indicators">
              <li data-target="#carousel4" data-slide-to="0" class="pointer car_item"></li>
              <li data-target="#carousel4" data-slide-to="1" class="pointer car_item"></li>
              <li data-target="#carousel4" data-slide-to="2" class="pointer car_item active"></li>
              <li data-target="#carousel4" data-slide-to="3" class="pointer car_item"></li>
              <li data-target="#carousel4" data-slide-to="4" class="pointer car_item"></li>
              <li data-target="#carousel4" data-slide-to="5" class="pointer car_item"></li>
            </ol>
            <div class="carousel-inner">
              <div class="carousel-item">
                <img class="d-block w-100 img-fluid imageHeight" src="https://thebig.co/images/blogs/vertical_panorama02.jpg">
              </div>
              <div class="carousel-item">
                <img class="d-block w-100 img-fluid imageHeight" src="https://media-cdn.tripadvisor.com/media/photo-s/09/f4/fe/38/horizontal.jpg">
              </div>
              <div class="carousel-item active">
                <img class="d-block w-100 img-fluid imageHeight" src="https://media-cdn.tripadvisor.com/media/photo-s/09/f4/fe/38/horizontal.jpg">
              </div>
              <div class="carousel-item">
                <img class="d-block w-100 img-fluid imageHeight" src="https://thebig.co/images/blogs/vertical_panorama02.jpg">
              </div>
              <div class="carousel-item">
                <img class="d-block w-100 img-fluid imageHeight" src="https://thebig.co/images/blogs/vertical_panorama02.jpg">
              </div>
              <div class="carousel-item">
                <img class="d-block w-100 img-fluid imageHeight" src="https://thebig.co/images/blogs/vertical_panorama02.jpg">
              </div>
            </div>
            <a class="carousel-control-prev" href="#carousel4" role="button" data-slide="prev">
              <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            </a>
            <a class="carousel-control-next" href="#carousel4" role="button" data-slide="next">
              <span class="carousel-control-next-icon" aria-hidden="true"></span>
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>

2 个答案:

答案 0 :(得分:0)

  1. 只需对转盘滑块和滑块图像使用max-height

  2. 使用object-fit:cover的图像将填充其框的高度和宽度,再次保持其宽高比,但在此过程中通常会裁剪图像。

可选地,我已使用@media进行响应,以后您可以根据需要调整max-height

.imageHeight {
    max-height: 460px;
    object-fit: cover;
    -o-object-fit: cover;
}

.carousel.slide {
    max-height: 460px;
}

@media (max-width:767px) {
    .imageHeight {
        max-height: 260px;
        object-fit: cover;
        -o-object-fit: cover;
    }
    .carousel.slide {
        max-height: 260px;
    }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<div id="block4">
    <div class="row mt-5">
        <div class="col-sm text-center align-middle">
            <h1>Doors</h1>
        </div>
    </div>
    <div class="row py-2">
        <div class="col-sm text-center align-self-center py-2">
            <a href="#carousel4" data-slide-to="0">
                <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
            </a>
        </div>
        <div class="col-sm text-center align-self-center py-2">
            <a href="#carousel4" data-slide-to="1">
                <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
            </a>
        </div>
        <div class="col-sm text-center align-self-center py-2">
            <a href="#carousel4" data-slide-to="2">
                <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
            </a>
        </div>
        <div class="col-sm text-center align-self-center py-2">
            <a href="#carousel4" data-slide-to="3">
                <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
            </a>
        </div>
        <div class="col-sm text-center align-self-center py-2">
            <a href="#carousel4" data-slide-to="4">
                <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
            </a>
        </div>
        <div class="col-sm text-center align-self-center py-2">
            <a href="#carousel4" data-slide-to="5">
                <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
            </a>
        </div>
    </div>
    <div id="lightbox4" class="modal fade" role="dialog" style="display: none;" aria-hidden="true">
        <div class="modal-dialog modal-xl modalCenter" role="document">
            <!--modal-xl modal-lg-->
            <div class="modal-content">
                <div class="modal-body">
                    <div class="carousel slide" data-ride="carousel" id="carousel4" data-interval="false">
                        <ol class="carousel-indicators">
                            <li data-target="#carousel4" data-slide-to="0" class="pointer car_item"></li>
                            <li data-target="#carousel4" data-slide-to="1" class="pointer car_item"></li>
                            <li data-target="#carousel4" data-slide-to="2" class="pointer car_item active"></li>
                            <li data-target="#carousel4" data-slide-to="3" class="pointer car_item"></li>
                            <li data-target="#carousel4" data-slide-to="4" class="pointer car_item"></li>
                            <li data-target="#carousel4" data-slide-to="5" class="pointer car_item"></li>
                        </ol>
                        <div class="carousel-inner">
                            <div class="carousel-item">
                                <img class="d-block w-100 img-fluid imageHeight" src="https://thebig.co/images/blogs/vertical_panorama02.jpg">
                            </div>
                            <div class="carousel-item">
                                <img class="d-block w-100 img-fluid imageHeight" src="https://media-cdn.tripadvisor.com/media/photo-s/09/f4/fe/38/horizontal.jpg">
                            </div>
                            <div class="carousel-item active">
                                <img class="d-block w-100 img-fluid imageHeight" src="https://media-cdn.tripadvisor.com/media/photo-s/09/f4/fe/38/horizontal.jpg">
                            </div>
                            <div class="carousel-item">
                                <img class="d-block w-100 img-fluid imageHeight" src="https://thebig.co/images/blogs/vertical_panorama02.jpg">
                            </div>
                            <div class="carousel-item">
                                <img class="d-block w-100 img-fluid imageHeight" src="https://thebig.co/images/blogs/vertical_panorama02.jpg">
                            </div>
                            <div class="carousel-item">
                                <img class="d-block w-100 img-fluid imageHeight" src="https://thebig.co/images/blogs/vertical_panorama02.jpg">
                            </div>
                        </div>
                        <a class="carousel-control-prev" href="#carousel4" role="button" data-slide="prev">
                            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                        </a>
                        <a class="carousel-control-next" href="#carousel4" role="button" data-slide="next">
                            <span class="carousel-control-next-icon" aria-hidden="true"></span>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

使用position:absolute方法:

.carousel-item {
  padding-bottom: 56%;
  overflow: hidden;
  position: relative;
}
.imageHeight {
    object-fit: cover;
    -o-object-fit: cover;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: auto;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<div id="block4">
    <div class="row mt-5">
        <div class="col-sm text-center align-middle">
            <h1>Doors</h1>
        </div>
    </div>
    <div class="row py-2">
        <div class="col-sm text-center align-self-center py-2">
            <a href="#carousel4" data-slide-to="0">
                <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
            </a>
        </div>
        <div class="col-sm text-center align-self-center py-2">
            <a href="#carousel4" data-slide-to="1">
                <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
            </a>
        </div>
        <div class="col-sm text-center align-self-center py-2">
            <a href="#carousel4" data-slide-to="2">
                <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
            </a>
        </div>
        <div class="col-sm text-center align-self-center py-2">
            <a href="#carousel4" data-slide-to="3">
                <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
            </a>
        </div>
        <div class="col-sm text-center align-self-center py-2">
            <a href="#carousel4" data-slide-to="4">
                <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
            </a>
        </div>
        <div class="col-sm text-center align-self-center py-2">
            <a href="#carousel4" data-slide-to="5">
                <img src="https://clayui.com/images/thumbnail_placeholder.gif" class="image-detail" data-toggle="modal" data-target="#lightbox4">
            </a>
        </div>
    </div>
    <div id="lightbox4" class="modal fade" role="dialog" style="display: none;" aria-hidden="true">
        <div class="modal-dialog modal-xl modalCenter" role="document">
            <!--modal-xl modal-lg-->
            <div class="modal-content">
                <div class="modal-body">
                    <div class="carousel slide" data-ride="carousel" id="carousel4" data-interval="false">
                        <ol class="carousel-indicators">
                            <li data-target="#carousel4" data-slide-to="0" class="pointer car_item"></li>
                            <li data-target="#carousel4" data-slide-to="1" class="pointer car_item"></li>
                            <li data-target="#carousel4" data-slide-to="2" class="pointer car_item active"></li>
                            <li data-target="#carousel4" data-slide-to="3" class="pointer car_item"></li>
                            <li data-target="#carousel4" data-slide-to="4" class="pointer car_item"></li>
                            <li data-target="#carousel4" data-slide-to="5" class="pointer car_item"></li>
                        </ol>
                        <div class="carousel-inner">
                            <div class="carousel-item">
                                <img class="d-block w-100 img-fluid imageHeight" src="https://thebig.co/images/blogs/vertical_panorama02.jpg">
                            </div>
                            <div class="carousel-item">
                                <img class="d-block w-100 img-fluid imageHeight" src="https://media-cdn.tripadvisor.com/media/photo-s/09/f4/fe/38/horizontal.jpg">
                            </div>
                            <div class="carousel-item active">
                                <img class="d-block w-100 img-fluid imageHeight" src="https://media-cdn.tripadvisor.com/media/photo-s/09/f4/fe/38/horizontal.jpg">
                            </div>
                            <div class="carousel-item">
                                <img class="d-block w-100 img-fluid imageHeight" src="https://thebig.co/images/blogs/vertical_panorama02.jpg">
                            </div>
                            <div class="carousel-item">
                                <img class="d-block w-100 img-fluid imageHeight" src="https://thebig.co/images/blogs/vertical_panorama02.jpg">
                            </div>
                            <div class="carousel-item">
                                <img class="d-block w-100 img-fluid imageHeight" src="https://thebig.co/images/blogs/vertical_panorama02.jpg">
                            </div>
                        </div>
                        <a class="carousel-control-prev" href="#carousel4" role="button" data-slide="prev">
                            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                        </a>
                        <a class="carousel-control-next" href="#carousel4" role="button" data-slide="next">
                            <span class="carousel-control-next-icon" aria-hidden="true"></span>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

答案 1 :(得分:0)

好吧,我现在知道如何解决这个问题:首先,我们必须在图片周围添加div,将其放入轮播中,并在该div内定位图像的位置。