简体   繁体   English

在div中裁剪居中图像

[英]Crop centered image inside div

I have a div containing an image (img) element, which extends for 100% width inside it. 我有一个包含图像(img)元素的div,它在其内部延伸100%宽度。 I would like to specify a maximum height for the div, and hide the parts of the image exceeding this height. 我想指定div的最大高度,并隐藏超过此高度的图像部分。 But I also want to keep this image centered vertically inside the div to show only its central part. 但我也想让这个图像垂直居中于div内,只显示它的中心部分。

For example, if browser width is 1200px and image aspect ratio is 4:3, image should display (1200x900)px. 例如,如果浏览器宽度为1200像素且图像宽高比为4:3,则应显示图像(1200x900)px。 But if we want to crop height to 300px only and center vertically, image should position at -300px inside the div (and the div should hide 0-300 and 600-900 of the image height). 但是如果我们想要将高度裁剪为仅300px并垂直居中,则图像应位于div内的-300px处(并且div应隐藏图像高度的0-300和600-900)。 Similar thoughts can be done for other widhts. 其他想法也可以做类似的想法。

I'm pretty sure this can be easily done with javascript, but I would like to know if there is a way to do it with CSS too. 我很确定这可以通过javascript轻松完成,但我想知道是否有办法用CSS也可以。 Thanks in advance! 提前致谢!

My take on this: http://codepen.io/vsync/pen/DpmnK 我对此的看法: http : //codepen.io/vsync/pen/DpmnK

HTML HTML

<div class='box'>
  <img src="http://www.biztalk360.com/Events/BizTalk-Innovation-day-2014-Norway/images/banner.jpg">
</div>

SCSS SCSS

.box{
  // this is the image container distentions 
  width:100%;
  height:100px;

  // The magic
  > img{
    position:absolute;
    z-index:-1;
    top:50%;
    left:50%;
    width:100%;
    transform:translate(-50%, -50%); 
    &.max{ width:auto; height:100%; }
  }
}

javascript (this is only for responsiveness) javascript(这仅用于响应)

var photo     = document.images[0],
    container = document.querySelector('.box');

$(window).on('resize.coverPhoto', function(){
  requestAnimationFrame(checkRatio);
});

function checkRatio(){
  var state = photo.clientHeight <= container.clientHeight && 
              photo.clientWidth >= container.clientWidth;

  photo.classList[state ? 'add' : 'remove']('max');
}

You may want to look at this question : Resizeing an oversized image using overflow:hidden and keep the aspect ratio 您可能需要查看以下问题: 使用overflow:hidden调整超大图像的大小并保持纵横比

http://codepen.io/gcyrillus/pen/Grbxg http://codepen.io/gcyrillus/pen/Grbxg

.grid_3 { width:260px; margin:0 20px; float:left;  text-align:center;
  overflow:hidden;background:rgba(255,255,255,0.02);}

.grid_3 a {
  display:block;  
  height:171px; border:solid 2px #FFFFFF;
  line-height:168px;
  overflow:hidden;
  margin-bottom:10px;
}
.max-img-border {  width:100%; margin:-100% 0;vertical-align:middle;
}

here is another pen , exploring this , vertical-align:middle and an image with virtually no height in the flux. 这是另一支笔,探索这个,垂直对齐:中间和一个几乎没有高度的图像。 http://codepen.io/gc-nomade/pen/DxCgv http://codepen.io/gc-nomade/pen/DxCgv

Of course , image set in background center is easy if it has no meaning in your content. 当然,如果在您的内容中没有任何意义,在背景中心设置的图像很容易。

So you want the div to function as a viewing window for your image? 因此,您是否希望div用作图像的查看窗口? This sounds like image sprites (a large pic of icons put together where each icon is displayed individually) but with a larger image: 这听起来像图像精灵(每个图标单独显示的大图片放在一起),但图像更大:

http://www.w3schools.com/css/css_image_sprites.asp http://www.w3schools.com/css/css_image_sprites.asp

If you provide a JSFiddle, I can give you something more specific. 如果你提供一个JSFiddle,我可以给你一些更具体的东西。

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

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