简体   繁体   English

根据高度计算应用比率宽度

[英]Calculate apsect ratio width based on height

I have an inline img that is 1280(w) x 1024(h) and is contained within a div. 我有一个内联img,它是1280(w)x 1024(h)并包含在div中。 The divs dimensions are set 100%(w/h) of the viewport with overflow hidden. div尺寸设置为视口的100%(w / h),且隐藏了溢出。

I want to use the image as a fullscreen background and require that it fills the viewport completely regardless of dimensions with no borders showing. 我想将图像用作全屏背景,并且要求其完全填充视口,而不考虑尺寸,不显示任何边框。 It must keep its aspect ratio and I would also like the image to be centered at all times. 它必须保持其纵横比,我也希望图像始终处于中心。 I am aware that to achieve this some of the image will be cropped. 我知道要实现这一点,某些图像将被裁剪掉。

I believe if make the image height match the viewport height and calculate the width based on the images aspect ratio this will work but I am unsure how to do this. 我相信,如果使图像高度与视口高度匹配,并根据图像宽高比计算宽度,则可以使用,但是我不确定如何做到这一点。 Can anyone help? 有人可以帮忙吗?

Many thanks in advance. 提前谢谢了。

Use backstretch http://srobbin.com/jquery-plugins/backstretch/ 使用backstretch http://srobbin.com/jquery-plugins/backstretch/

$("#demo").backstretch("http://urltoimage.jpg");'

set #demo to 100/100% width and height 将#demo设置为100/100%的宽度和高度

Not really an answer to your question but a potential alternative -- have you tried the min-height / min-width CSS properties? 并不是真正解决您问题的方法,而是一种可能的替代方法-您是否尝试过min-height / min-width CSS属性?

HTML: HTML:

   <div class="container">
       <div class="image-wrapper">
           <img src="..." />
       </div>
   </div>

CSS: CSS:

.container { 
    width: 100vw;
    height: 100vh;
    background-color: grey;
}

.image-wrapper {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

.image-wrapper img {
    position: absolute;
    top: 50%;
    left: 50%;
    min-width: 100%;
    min-height: 100%;
    transform: translateX(-50%) translateY(-50%);
}

Fiddle: https://jsfiddle.net/mkjoyep1/ 小提琴: https//jsfiddle.net/mkjoyep1/

In the example, .container (your full width/height div), fills the page width vw/wh and the .image-wrapper has its width/height set to 100% which will match the parent's width and height. 在示例中, .container (您的全宽/高度div)填充页面宽度vw/wh.image-wrapperwidth/height设置为100% ,与父级的宽度和高度匹配。 With overflow: hidden acting as the crop mechanism you can then stretch your image to fill it's container. 使用overflow: hidden作为裁剪机制,然后可以拉伸图像以填充其容器。 I've positioned the image in the center with the method explored here . 我将图像定位在此处探索的中心位置。

This appears to work for me on Chrome and Firefox. 在Chrome和Firefox上,这似乎对我有用。

*Edited to include html/css *编辑后包含html / css

If you're not 100% glued to using an img tag, you can do the following: 如果您不是100%坚持使用img标签,则可以执行以下操作:

HTML 的HTML

<div id="background"></div>

CSS 的CSS

#background {
  background: url('path/to/img.jpg');
  background-size: cover;
  background-position: center center;
  width: 100vw;
  height: 100vh;
}

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

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