简体   繁体   English

试图防止图像在引导轮播中被拉伸

[英]Trying to prevent images being stretched within a bootstrap carousel

I have a bootstrap 4 carousel that is taking up a good portion of the page. 我有一个Bootstrap 4轮播,占了页面的很大一部分。 The images are appended through an ajax request once received. 收到图像后,将通过ajax请求附加图像。

All the photos are taken on the phone, so they are either landscape or portrait style. 所有照片都是在手机上拍摄的,因此它们均为横向或纵向风格。

bootstrap carousel either stretches the image out too far and the image extends beyond the limit of my div or they appear stretched and skews the quality of the photo. bootstrap旋转木马或者将图像拉伸得太远,并且图像超出了我的div的限制,或者它们看起来被拉伸并歪曲了照片的质量。 Is there a way to set a limit to the image and adjust it on the fly so each unique photo fits my div? 有没有一种方法可以设置图像限制并实时调整图像,以使每张独特的照片都适合我的div?

css for the div that contains my carosuel: 包含我的carosuel的div的CSS:

#right_curtain {
    z-index: 1;
    position: fixed;
    left: 20%;
    width: 80%;
    height: 100%

    }

my div for the carosuel: 我对转盘的div:

<div id="right_curtain"  >


<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel" style ="z-index: 0">

    <ol class="carousel-indicators">

    </ol>
    <div class="carousel-inner">

    </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>

and the javascript that appends an image to the carosuel 和将图像附加到转盘的javascript

            for (i = 0; i < count; i++) {
                buttonJSON += '<button type="button" class="list-group-item list-group-item-action" id = "buttonList'+i+'"onclick=listClick(this) data-value ="'+json[i].id+'"><b>'+json[i].department.replace("_"," ")+'</b><br><b>ADDRESS</b>: '+json[i].address+'<br><b>TYPE</b>: '+json[i].type+'<br><b>USER</b>: '+json[i].user+'</button>';
                if(i ===0){
                    $('.carousel-indicators').append($('<li data-target="#carouselExampleIndicators" class="active"> data-slide-to="'+i+' "></li>'));
                    $('.carousel-inner').append($('<div class="carousel-item active"> <img class="d-block w-100" src="'  + json[i].link + '" alt="'+json[i].id+'"  data-value ="'+json[i].id+'" ></div>'));
                }else{
                    $('.carousel-indicators').append($('<li data-target="#carouselExampleIndicators" data-slide-to="'+i+'"></li>'));
                    $('.carousel-inner').append($('<div class="carousel-item"> <img id = "image'+i+'"class="d-block w-100" src="'  + json[i].link  + '" alt="'+json[i].id+'" data-value ="'+json[i].id+'"   ></div>'));
                }
            }

You can try to add this in CSS: 您可以尝试在CSS中添加此代码:

#right_curtain .carousel-inner img {
  object-fit: cover;
}

Though this solution might not work well for IE11, but personally I don't care for IE11: https://caniuse.com/#search=object-fit 尽管此解决方案可能不适用于IE11,但就我个人而言,我并不关心IE11: https ://caniuse.com/#search=object-fit

Change this 改变这个

#right_curtain {
  z-index: 1;
  position: fixed;
  left: 20%;
  width: 80%;
  height: 100%
}

to this 对此

#right_curtain {
  z-index: 1;
  position: fixed;
  width: auto;
  height: 100%;
}

This will cause the image to be the full height of the div its in, and it should size and keep its original image quality. 这将导致图像达到其div的整个高度,并且应该调整大小并保持其原始图像质量。 Hopefully this helps. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM