在 zoom/scale 转换中计算 div width/height

Calculate div width/height when inside a zoom/scale transform

我在另一个 div 中有一个 div,应用了变换比例。

我需要在应用缩放后得到这个 div 的宽度。 .width() 的结果是元素的原始宽度。

请看这个代码笔: https://codepen.io/anon/pen/ZMpBMP

问题图片:

希望你说得够清楚了,谢谢。代码如下:


HTML

<div class="outer">
  <div class="inner">
  </div>
</div>

CSS

.outer {
  height: 250px;
  width: 250px;
  background-color: red;
  position: relative;
  overflow: hidden;
}

.inner {
  background-color: green;
  height: 10px;
  width: 10px;
  transform: translate(-50%);
  position: absolute;
  top: 50%;
  left: 50%;

  transform: scale(13.0);
}

JS

$(function() {

   var width = $('.inner').width();

   // I expect 130px but it returns 10px
   //
   // I.e. It ignores the zoom/scale
   //      when considering the width
   console.log( width );

});

您需要计算值。这可以在 CSS.

中完成

使用 calc() 计算 <div> 元素的宽度,可以是任何元素:

#div1 {
    position: absolute;
    left: 50px;
    width: calc(100% - 100px);
    border: 1px solid black;
    background-color: yellow;
    padding: 5px;
    text-align: center;
}

我找到了关于这个主题的。

Can i use Calc inside Transform:Scale function in CSS?


对于 JS:

How do I retrieve an HTML element's actual width and height?

我通过这个达到了你的 130

var x = document. getElementsByClassName('inner');
var v = x.getBoundingClientRect();
width = v.width;

使用getBoundingClientRect()

$(function() {

   var width = $('.inner')[0].getBoundingClientRect();
   // I expect 130px but it returns 10px
   //
   // I.e. It ignores the zoom/scale
   //      when considering the width
   console.log(width.width);

});

https://jsfiddle.net/3aezfvup/3/