【问题标题】:Bootstrap Carousel height adjust as browser heightBootstrap Carousel 高度调整为浏览器高度
【发布时间】:2017-12-18 09:00:58
【问题描述】:

我正在尝试将我的 Bootstrap 滑块高度调整为浏览器屏幕高度,但无法正确调整。

我知道这个问题已经在 StackOverflow 上提出过,但没有找到更好的解决方案。

我通过设置 height = 85vh 找到了一种解决方案,但滑块图像在大屏幕上看起来被拉伸了。

我附上了下面的滑块截图,

我还在我当前的代码下面添加了sn-p

$('.carousel-1').carousel();
 .carousel {
            left: 0px;
            height: 85vh;
        }

        .carousel-inner>.item {
            height: 85vh;
        }
        .sliderBG>.item>img {
            height: 85vh;
        }
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div id="carousel-1" class="carousel slide" data-ride="carousel">
            <div class="carousel-inner sliderBG">
                <div class="item active"><img src="https://images.pond5.com/multi-ethnic-business-team-meeting-footage-008969212_prevstill.jpeg" alt="Nshama Properties" style="width:100%;">
                   
                </div>
                <div class="item"><img src="https://images.pond5.com/multi-ethnic-business-team-meeting-footage-008969212_prevstill.jpeg" alt="Nshama Properties" style="width:100%;">
                    
                </div>
                <div class="item"><img src="https://images.pond5.com/multi-ethnic-business-team-meeting-footage-008969212_prevstill.jpeg" alt="Nshama Properties" style="width:100%;">
                   
                </div>
                <div class="item"><img src="https://images.pond5.com/multi-ethnic-business-team-meeting-footage-008969212_prevstill.jpeg" alt="Nshama Properties" style="width:100%;">
                   
                </div>
            </div>

            <a class="carousel-control left" href="#carousel-1" data-slide="prev">
                <i class="fa fa-angle-left" aria-hidden="true"></i>
                <span class="sr-only">Previous</span>
            </a>
            <a class="carousel-control right" href="#carousel-1" data-slide="next">
                <i class="fa fa-angle-right" aria-hidden="true"></i>
                <span class="sr-only">Next</span>
            </a>
        </div>

【问题讨论】:

  • 也许可以试试.sliderBG&gt;.item&gt;img { height: auto; }
  • 这就是 img-fluid 的用途:img-fluid class

标签: javascript jquery css twitter-bootstrap bootstrap-carousel


【解决方案1】:

您可以只更改您的 CSS 部分并获得乐趣..!

.sliderBG>.item>img {
    display: block;
    height: 100%;
    max-width: inherit;
    width: auto !important;
    margin: 0 auto;
}

【讨论】:

  • 谢谢@Md。 Abu Sayed 工作正常,但下方区域的图像会隐藏在大屏幕上。
  • @mudassar 我不明白您的“区域下方的图像隐藏在大屏幕上”评论。请描述我..!
  • 请查看以下截图链接,不同屏幕上的相同图像。
  • @Mudassar,我理解你的问题。您不知道 HTML 块图像行为,当您谈论图像 100% 宽度时,图像会自动扩展图像比例的高度。任何人都自动然后图像不会拉伸。如果您完全宽度和高度,则使用背景图像和背景尺寸覆盖。谢谢。
  • 谢谢@Md。 Abu Sayed 但还有另一个问题是它在几个屏幕的底部显示黑色空白区域。
【解决方案2】:

那是因为你是trying to make height responsive but at the same time stretching the image with respect to its width 而不是身高。

你能做什么?

你可以设置height to 100%而不是滑块中图像的宽度,并设置margin to auto使其居中对齐;

$('.carousel-1').carousel();
.carousel {
  left: 0px;
  height: 85vh;
}

.carousel-inner{
  height: 100%;
}

.carousel-inner .item{
  height: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div id="carousel-1" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner sliderBG">
    <div class="item active"><img src="https://images.pond5.com/multi-ethnic-business-team-meeting-footage-008969212_prevstill.jpeg" alt="Nshama Properties" style="height:100%;margin:0 auto;">

    </div>
    <div class="item"><img src="https://images.pond5.com/multi-ethnic-business-team-meeting-footage-008969212_prevstill.jpeg" alt="Nshama Properties" style="height:100%;margin:0 auto;">

    </div>
    <div class="item"><img src="https://images.pond5.com/multi-ethnic-business-team-meeting-footage-008969212_prevstill.jpeg" alt="Nshama Properties" style="height:100%;margin:0 auto;">

    </div>
    <div class="item"><img src="https://images.pond5.com/multi-ethnic-business-team-meeting-footage-008969212_prevstill.jpeg" alt="Nshama Properties" style="height:100%;margin:0 auto;">

    </div>
  </div>

  <a class="carousel-control left" href="#carousel-1" data-slide="prev">
    <i class="fa fa-angle-left" aria-hidden="true"></i>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control right" href="#carousel-1" data-slide="next">
    <i class="fa fa-angle-right" aria-hidden="true"></i>
    <span class="sr-only">Next</span>
  </a>
</div>

但如果您想填充整个幻灯片并且不介意剪掉图像,那么:

$('.carousel-1').carousel();
.carousel {
  left: 0px;
  height: 85vh;
}

.carousel-inner{
  height: 100%;
}

.carousel-inner item img{
height:100%;margin:0 auto;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div id="carousel-1" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner sliderBG">
    <div class="item active"><img src="https://images.pond5.com/multi-ethnic-business-team-meeting-footage-008969212_prevstill.jpeg" alt="Nshama Properties" style="">

    </div>
    <div class="item"><img src="https://images.pond5.com/multi-ethnic-business-team-meeting-footage-008969212_prevstill.jpeg" alt="Nshama Properties" style="">

    </div>
    <div class="item"><img src="https://images.pond5.com/multi-ethnic-business-team-meeting-footage-008969212_prevstill.jpeg" alt="Nshama Properties" style="">

    </div>
    <div class="item"><img src="https://images.pond5.com/multi-ethnic-business-team-meeting-footage-008969212_prevstill.jpeg" alt="Nshama Properties">

    </div>
  </div>

  <a class="carousel-control left" href="#carousel-1" data-slide="prev">
    <i class="fa fa-angle-left" aria-hidden="true"></i>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control right" href="#carousel-1" data-slide="next">
    <i class="fa fa-angle-right" aria-hidden="true"></i>
    <span class="sr-only">Next</span>
  </a>
</div>

【讨论】:

  • 感谢@Highdef,它看起来不错,但下方区域的图像会隐藏在大屏幕上。
  • @Mudassar 你什么意思?你能显示大屏幕的输出屏幕截图吗?
  • 请查看以下截图链接,不同屏幕上的相同图像。
  • @Mudassar 您尝试了我给出的第一个解决方案还是第二个解决方案?
  • 目前使用第一个,但我都试过了,结果一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-22
  • 2016-01-13
  • 2018-12-30
  • 2015-12-28
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多