HTML / CSS - 在不调整视口大小的情况下定位粘性和变换

HTML / CSS - Position Sticky and transform without resizing viewport

在我的网站中,我有使用转换的元素。我注意到这些转换会调整视口的大小。通常我会通过给 html 和 body overflow-x: hidden 来解决这个问题,但是我的 header 和 position: sticky 不起作用。

如何防止转换后的元素调整视口大小并继续使用 position: sticky

我在下面添加了一个示例。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.scale {
  padding: 100px;
  width: 100%;
  height: 400px;
  background-color: green;
  transform: scale(2);
  position: relative;
  z-index 1;
}

header {
  position: sticky;
  position: -webkit-sticky;
  top: 0;
  left 0;
  right 0;
  height: 50px;
  background-color: red;
  z-index: 2;
}
<header>I want this header to remain sticky</header>
<div class="scale">
But I don't want this div to resize the x-axis of the viewport.
</div>

使用另一个可以应用的容器overflow:hidden

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.scale {
  padding: 100px;
  width: 100%;
  height: 400px;
  background-color: green;
  transform: scale(2);
  position: relative;
  z-index 1;
}

.container {
  overflow: hidden;
}

header {
  position: sticky;
  position: -webkit-sticky;
  top: 0;
  left 0;
  right 0;
  height: 50px;
  background-color: red;
  z-index: 2;
}
<header>I want this header to remain sticky</header>
<div class="container">
  <div class="scale">
    But I don't want this div to resize the x-axis of the viewport.
  </div>
</div>