【问题标题】:Bootstrap Thumbnail Cropping and PositioningBootstrap 缩略图裁剪和定位
【发布时间】:2015-08-24 11:56:21
【问题描述】:

我有这 3 张不同尺寸的图片。当我将它们显示在这样的 3 个容器中时:

@foreach (var item in Model) {   
    <div class="avatar-container">
        <img class="avatar img-thumbnail" src="@Href("~/Content/Avatars/",item.Name+".jpg")" />  
    </div>
}

这是我的 CSS 文件:

div.avatar-container {
    display: inline-block;
    max-height: 50px;
    overflow: hidden;
    width: 70px;
}

.avatar {
    width: 100%;
    height:auto;
    overflow: hidden;
}

img-thumbnail 也来自 bootstrap.css(第 368 行)。一段时间后,我设法裁剪图像(使用 overflow 属性),以便每个头像显示为 70x50 缩略图。

看看这 3 个返回的缩略图:image
[问题]

image1. 它还裁剪了我缩略图的漂亮底部。
image2. 我认为缩略图是正方形,而不是矩形。
image3.如何裁剪到图像的中间(垂直和水平方式)?

【问题讨论】:

    标签: css twitter-bootstrap position overflow crop


    【解决方案1】:

    这是我想出的,使用 JavaScript 和另一个缩略图容器。

    @*HTML-----------------------------------------------*@
    @foreach (var item in Model) {   
        <div class="avatar-container">
            <div class="img-thumbnail">
                <img class="avatar img-thumbnail" src="@Href("~/Content/Avatars/",item.Name+".jpg")" />  
            </div>
        </div>
    }
    @*JavaScript-----------------------------------------*@
    //If the image has greater Width => display as landscape
    <script>
            $(".avatar").each(function () {
                if (this.naturalWidth > this.naturalHeight) {
                    $(this).addClass("landscape");
                }
            });
    </script>
    @*CSS------------------------------------------------*@
    //That big container outside
    div.avatar-container {
        display: inline-block;
        position: relative;
        width: 70px;
        height:70px;
        overflow: hidden;
    }
    //And here's the extra thumbnail container
    div.img-thumbnail{
        width:100% !important;
        height:100%;
        background-color:#e1dada;
    }
    //This is for the actual image
    .img-thumbnail{
        padding:1px !important;
    }
    //This one sets the image as a portrait
    .avatar {
        position: absolute;
        top: 50%;
        left: 50%;
        height: 100%;
        width: auto;
        -webkit-transform: translate(-50%,-50%);
        -ms-transform: translate(-50%,-50%);
        -moz-transform: translate(-50%,-50%);
        -o-transform: translate(-50%,-50%);
        transform: translate(-50%,-50%);
    }
    //This one helps setting up the image horizontally
    .landscape{
        width:100%;
        height:auto;
    }
    

    最终结果看起来像this

    【讨论】:

      猜你喜欢
      • 2012-10-25
      • 1970-01-01
      • 2016-11-09
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 2013-10-22
      • 1970-01-01
      相关资源
      最近更新 更多