简体   繁体   English

保持子div的纵横比与父div的高度和宽度变化

[英]maintain aspect ratio of a child div with both height and width changes of the parent

I want to create a div that maintains aspect ratio with height and width changes of the parent.我想创建一个 div 来保持纵横比与父级的高度和宽度变化。

我的问题截图

In the above gif, you can see that I was able to maintain the box div's aspect ratio when changing width but I'm unable to maintain it when changing height of the parent.在上面的 gif 中,您可以看到我能够在更改宽度时保持框 div 的纵横比,但在更改父级的高度时无法保持它。

 .box { width: 100px; background-color: #dfdfdf; max-width: 100%; } .box:after { content: ''; display: block; width: 100%; height: 100%; padding-bottom: 160%; } .wrap9 { width: 125px; height: 190px; }
 <div class="wrap9"> <div class="box"></div> </div>

I want the grey box to behave like the following:我希望灰色框的行为如下:

在此处输入图片说明

With your default width and height:使用默认宽度和高度:

 .box { width: 100px; background-color: #dfdfdf; max-width: 100%; max-height:100%; } .box:after { content: ''; display: block; width: 100%; height: 100%; padding-bottom: 160%; } .wrap9 { width: 150px; height: 200px; border: 1px solid black; }
 <div class="wrap9"> <div class="box"></div> </div>

width lower width and height:宽度下宽度和高度:

 .box { width: 100px; background-color: #dfdfdf; max-width: 100%; max-height:100%; } .box:after { content: ''; display: block; width: 100%; height: 100%; padding-bottom: 160%; } .wrap9 { width: 100px; height: 120px; border: 1px solid black; }
 <div class="wrap9"> <div class="box"></div> </div>

Is this the desired effect?这是想要的效果吗?

I'd have to say that this is not currently possible.我不得不说这目前是不可能的。 While you can use calc() and variables, there is no way to keep a dynamic reference to object's current width nor height .虽然您可以使用calc()和变量,但无法保持对对象当前widthheight的动态引用。

When I looked at doing this with js there was no clean way either.当我看着用js做这件事时,也没有干净的方法。 You'll just have to write an interval to periodically check if width or height of parent element has changed.您只需要编写一个间隔来定期检查父元素的宽度或高度是否已更改。

Google is pushing a dom element observer but the spec is currently very limited. Google 正在推动dom 元素观察器,但目前规范非常有限。

The funny thing is, just like in your example, images do this by default.有趣的是,就像在您的示例中一样,默认情况下图像会执行此操作。 However, there seems to be absolutely no clean way of doing this with another element.但是,似乎绝对没有使用另一个元素来做到这一点的干净方法。

You can use JavaScript to calculate the ratio of the width and the height between you object and the parent object.您可以使用 JavaScript 来计算对象与父对象之间的宽高比。 Then your can either scale to FILL the parent object (think CSS background-size: contain;) or scale to FIT the parent object (CSS: background-size: cover;)然后您可以缩放以填充父对象(想想 CSS 背景大小:包含;)或缩放以适应父对象(CSS:背景大小:封面;)

在此处输入图片说明

Example of the two type of scaling methods.两种缩放方法的示例。 (A) The left shows SCALE-TO-FILL and (B) on the right we are using SCALE-TO-FIT. (A) 左侧显示 SCALE-TO-FILL,右侧 (B) 显示我们正在使用 SCALE-TO-FIT。

// Find the ratio between the destination width and source width.
RATIO-X = DESTINATION-WIDTH / SOURCE-WIDTH

// Find the ratio between the destination height and source height.
RATIO-Y = DESTINATION-HEIGHT / SOURCE-HEIGHT

// To use the SCALE TO FILL method, 
// we use the greater of the two ratios, 
// let's assume the RATIO-X is greater than RATIO-Y.
SCALE-W = RATIO-X * SOURCE-WIDTH
SCALE-H = RATIO-X * SOURCE-HEIGHT

// To use the SCALE TO FIT method, 
// we use the lesser of the two ratios, 
// let's assume the RATIO-Y is less than RATIO-X.
SCALE-W = RATIO-Y * SOURCE-WIDTH
SCALE-H = RATIO-Y * SOURCE-HEIGHT

When your parent object scales, you will need to determine these ratios.当您的父对象缩放时,您需要确定这些比率。

Take a look at the embedded example and you can see how it works.看一下嵌入式示例,您就可以看到它是如何工作的。

 var $parent = $('.parent'), $child = $('.child'), w = $parent.width(), h = $parent.height(), cw = $child.width(), ch = $child.height(), winc = -10, hinc = -5; /* This function is what you need.{ First we grab the ratio for the width (rx). Then the ratio for the height (ry). To scale to FIT, we want the MIN of the two. To scale to FILL, we want the MAX of the two. r is used to calculate the new size for ther child obj. */ function calcChildSize() { var rx = w / cw, ry = h / ch, r1 = Math.min(rx, ry), r2 = Math.max(rx, ry); $('.child.fit').css({ width: r1 * cw, height: r1 * ch }); $('.child.fill').css({ width: r2 * cw, height: r2 * ch }); } // just a simple function to change the size // of the parent object window.setInterval(function() { if (w < 70) { winc = 10; } else if (w > 200) { winc = -10; } if (h < 50) { hinc = 5; } else if (h > 200) { hinc = -5; } w += winc; h += hinc; $parent.css({ width: w, height: h }); calcChildSize(); }, 100);
 .parent { display: inline-block; width: 200px; height: 200px; border: 1px solid #aaa; margin-right: 50px; } .child { box-sizing: border-box; width: 50px; height: 70px; background: rgba(0, 0, 0, 0.2); border: 1px solid rgba(255, 0, 0, 0.5); text-align: center; font-family: monospace; font-size: 11px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="parent fit"> <div class="child fit">FIT</div> </div> <div class="parent fill"> <div class="child fill">FILL</div> </div>

Edit #2: I also went ahead and added an example of the two different scale methods in action.编辑 #2:我还继续添加了两个不同比例方法的示例。

Add max-width: 100% and object-fit:cover, here's a link https://jsfiddle.net/jv1f4bhL/1/添加 max-width: 100% 和 object-fit:cover,这是一个链接https://jsfiddle.net/jv1f4bhL/1/

 body { position: relative; background-color: wheat; padding: 0; margin: 0; } img { object-fit: cover; max-width: 100%; width: auto; }
 <img src="https://mdbootstrap.com/img/Others/documentation/img%20(7)-mini.jpg" />

    .box {
        max-height: 100%;
      }

....worked perfect. ....工作完美。

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

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