javascript - 在移动状态下调整 Bootstrap 轮播中的图像高度

标签 javascript jquery html css twitter-bootstrap

我能够将 bootstrap 4 轮播和图像从平板电脑转移到桌面,但是当手机处于纵向 View 时,图像会拉伸(stretch)到高。高度:100vh;在其他地方正确定位图像,但如何在纵向移动状态下覆盖它?

演示页面 http://dninstalls.com/blankbootstrap/

CSS

    .carousel {
    width: 100% !important;
    max-height: calc(100vh - 81px);
    overflow: hidden;

}

.carousel,
.carousel-item img {
    max-width: 100%;
    height: auto;
    width: 100%;
    height: 100vh;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    @media (min-width: 576px) {
        .container {
            max-width: 540px;
            min-height: auto;
            max-height: auto;
        }
    }
    @media (max-width: 575px) {
        .container {
            max-width: 100%;
            min-height: auto;
            max-height: auto;
        }
    }
}

HTML

<header id="carouselHeader" class="page-header">
        <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
            <ol class="carousel-indicators">
                <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
                <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
                <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
                <li data-target="#carouselExampleIndicators" data-slide-to="3"></li>
            </ol>
            <!--                    Carousel image 1 -->
            <div class="carousel-inner">
                <div class="carousel-item active">
                    <img class="d-block w-100" src="assets/mbbj%20kids.png" alt="First slide">
                </div>
                <!--                    Carousel image 2 -->
                <div class="carousel-item">
                    <img class="d-block w-100" src="assets/mbjj%20kids2.jpeg" alt="Second slide">
                </div>
                <!--                    Carousel image 3 -->
                <div class="carousel-item">
                    <img class="d-block w-100" src="assets/mbjj%20adult.jpeg" alt="Third slide">
                </div>
                <!--                    Carousel image 4 -->
                <div class="carousel-item">
                    <img class="d-block w-100" src="assets/mbjj%20comp.png" alt="Third slide">
                </div>
            </div>
            <a class="carousel-control-prev" href="#carouselExampleIndicators" 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="#carouselExampleIndicators" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
        </div>
    </header>

最佳答案

您需要删除 vh 属性并使用 max-heignt、min-height 属性和适当的媒体查询 ...

/* Portrait and Landscape */
@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 568px)
  and (-webkit-min-device-pixel-ratio: 2) {

}

/* Portrait */
@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 568px)
  and (-webkit-min-device-pixel-ratio: 2)
  and (orientation: portrait) {
}

/* Landscape */
@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 568px)
  and (-webkit-min-device-pixel-ratio: 2)
  and (orientation: landscape) {

}

.carousel, .carousel-item img 这个类需要使用 max-height 和 min-height 属性。根据设备尺寸,应更改图像比例...请参阅以下链接,将在此处获取所有代码 https://css-tricks.com/snippets/css/media-queries-for-standard-devices/

关于javascript - 在移动状态下调整 Bootstrap 轮播中的图像高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48739130/

相关文章:

javascript - jquery 查找并替换元素中的关键字

javascript - Yii2:如何在 View 中使用像 onchange() 这样的 JavaScript 函数

javascript - 重置 javascript 函数

jquery - 如何将元素定义为 "this"

javascript - Javascript 中不存在时如何声明 CONST

JavaScript click() 返回未定义

java - HtmlUnit 属性未定义脚本错误

html - 文本悬停时出现奇怪的背景颜色

javascript - ng-table 教程的范围问题

javascript - 检查变量中的元素在当前 DOM 中是否有引用