如何使用 CSS 变换比例使对象居中

How to center object with CSS transform scale

我正在尝试实现缩放 in/out 功能,就像您在 google docs 等文档编辑器或任何文字处理器应用程序上使用的功能一样。我遇到的问题是我无法将 "document" 居中,也无法将其所有部分滚动到视图中。这是问题的一个小演示:https://codepen.io/liviu_vasut/pen/dyGbwwO

document.getElementById('objectToScale').style.transform = "scale(3)";
.container {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 5px;
  width: 300px;
  height: 200px;
  border: 1px solid #000000;
  left: 0px;
  top: 0px;
  margin: 10px;
  overflow: auto;
}

.object {
  position: relative;
  width: 120px;
  height: 120px;
  display: inline-block;
  background-color: grey;
  border-radius: 25px;
  padding: 5px;
  transform-origin: center;
  transform: scale(1);
}
<div class="container">
  <div id="objectToScale" class="object">x</div>
</div>

感谢您的宝贵时间。

实际上,使用 transform-origin: 0 0; 并手动将滚动条设置为中心:

var scaled = false;

function toggleScale() {
  [...document.getElementsByClassName('object')].forEach(e => {
    e.classList.toggle('scaled');
    e.parentElement.scrollTop = (e.parentElement.scrollHeight - e.parentElement.clientHeight) / 2;
    e.parentElement.scrollLeft = (e.parentElement.scrollWidth - e.parentElement.clientWidth) / 2;
  });
}
* {
  box-sizing: border-box;
}

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 5px;
  width: 300px;
  height: 200px;
  border: 1px solid #000000;
  left: 0px;
  top: 0px;
  margin: 10px;
  overflow: auto;
}

.object {
  position: relative;
  width: 120px;
  height: 120px;
  display: inline-block;
  background-color: grey;
  border-radius: 25px;
  padding: 5px;
  transform-origin: 0 0;
}

.scaled {
  transform: scale(3);
}
<input type="button" onClick="toggleScale()" value="Toggle Scale" />

<div class="container">
   <div class="object">cannot see the entire object when scaled</div>
</div>

您只缩放整个框内的一些内部元素,但期望看到整个框缩放。如果你想缩放白色填充和所有内部内容以保持可见(并且能够滚动到)你应该在里面添加一些包装 width: 100%height: 100%,并缩放它,所以整个内容被缩放。

此外,正如@HaoWu 提到的,您应该将 transform-origin 设置为 0 0

最终产品应该看起来像这样:

var scaled = false;

function toggleScale() {
  var objects = document.getElementsByClassName('wrapper');
  for (var i = 0; i < objects.length; i++) {
    objects[i].style.transform = scaled ? 'scale(1)' : 'scale(3)';
  }
  scaled = !scaled;
}
    .container {
      width: 300px;
      height: 200px;
      border: 1px solid #000000;
      left: 0px;
      top: 0px;
      margin: 10px;
      overflow: auto;
    }
    .wrapper {
      width: 100%;
      height: 100%;
      display: flex;
      box-sizing: border-box;
      align-items: center;
      justify-content: center;
      padding: 5px;
      transform-origin: 0 0;
    }
    .object {
      position: relative;
      width: 120px;
      height: 120px;
      display: inline-block;
      background-color: grey;
      border-radius: 25px;
      padding: 5px;
      transform-origin: center;
      transform: scale(1);
    }
<input type="button" onclick="toggleScale()" value="Toggle Scale" />

<div class="container">
    <div class="wrapper">
        <div id="objectToScale" class="object">x</div>
    </div>
</div>