如何使用过渡和规模转换时如何将DIV集中

发布于 2025-01-17 08:34:01 字数 2141 浏览 0 评论 0原文

我已经为我的 React 应用程序构建了一个弹出窗口,并且我不想使用任何外部库。

现在我尝试使用过渡和缩放变换,然后计算弹出窗口的宽度和高度并将其在屏幕上居中。

问题是,我无法在缩放完成之前进行计算,这会导致弹出窗口在缩放操作后显示在一处,然后回到中心。

弹出窗口的宽度和高度取决于屏幕尺寸和内容,因此无法使用 CSS 来实现,所以我认为。

这是下面的问题:

const scaleUp = (element, callback) => {
  if (element) {
     element.style.transform = "scale(1)";
    element.style.transition = "transform 0.5s ease-in-out";
 
    setTimeout(() => {
      callback()
    }, 502);

  }
}


const centerScreen = () => {
   const element= document.querySelector(".popup")
  if (element) {
    var size = element.getBoundingClientRect();
    element.style.marginTop = -size.height / 2 + "px";
    element.style.marginLeft = -size.width / 2 + "px";
  }
}


function clickCenterfirst(){
centerScreen() //If i do this, it gets very wrong, testit you selef
scaleUp(document.querySelector(".popup"), centerScreen)
}

function clicktransitionfirst(){
scaleUp(document.querySelector(".popup"), centerScreen)
}
.popup {
  background: white;
  border: 1px solid gray;
  border-radius: 5px;
  position: fixed;
  margin-top: -100px;
  margin-left: -154px;
  display: block;
  min-height: 35vh;
  box-shadow: 1px 1px #f40000, 0em 0em 0.4em red;
  left: 50%;
  top: 50%;
  max-width: 90vw;
  max-height: 90vh;
  overflow: hidden;
  min-width: 300px;
  z-index: 200;
  -webkit-transform: scale(0);
  -moz-transform: scale(0);
  -ms-transform: scale(0);
  transform: scale(0);
  transition: transform 0.5s ease-in-out;
}

.content{
    padding: 10px;
    max-width: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    height: 35vh;
    max-height: 60vh;
}
<button onclick="clickCenterfirst()"> with center call first </button>
<button onclick="clicktransitionfirst()"> with transition call first </button>
<div class="popup">
  <div class="content">
  </div>
</div>

I have build a popup for my React app and I do not want to use any external library.

Now I am trying to use transition and transform with scale and then calculate the popup width and height and center it on the screen.

The problem is, I can't make calculations before the scale is done, and that cause that the popup shows in one place after the scale operation and then gets back in the center.

The popup width and height depend on the screen size and content so can't make it with CSS or so I think.

Here is the problem below:

const scaleUp = (element, callback) => {
  if (element) {
     element.style.transform = "scale(1)";
    element.style.transition = "transform 0.5s ease-in-out";
 
    setTimeout(() => {
      callback()
    }, 502);

  }
}


const centerScreen = () => {
   const element= document.querySelector(".popup")
  if (element) {
    var size = element.getBoundingClientRect();
    element.style.marginTop = -size.height / 2 + "px";
    element.style.marginLeft = -size.width / 2 + "px";
  }
}


function clickCenterfirst(){
centerScreen() //If i do this, it gets very wrong, testit you selef
scaleUp(document.querySelector(".popup"), centerScreen)
}

function clicktransitionfirst(){
scaleUp(document.querySelector(".popup"), centerScreen)
}
.popup {
  background: white;
  border: 1px solid gray;
  border-radius: 5px;
  position: fixed;
  margin-top: -100px;
  margin-left: -154px;
  display: block;
  min-height: 35vh;
  box-shadow: 1px 1px #f40000, 0em 0em 0.4em red;
  left: 50%;
  top: 50%;
  max-width: 90vw;
  max-height: 90vh;
  overflow: hidden;
  min-width: 300px;
  z-index: 200;
  -webkit-transform: scale(0);
  -moz-transform: scale(0);
  -ms-transform: scale(0);
  transform: scale(0);
  transition: transform 0.5s ease-in-out;
}

.content{
    padding: 10px;
    max-width: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    height: 35vh;
    max-height: 60vh;
}
<button onclick="clickCenterfirst()"> with center call first </button>
<button onclick="clicktransitionfirst()"> with transition call first </button>
<div class="popup">
  <div class="content">
  </div>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满意归宿 2025-01-24 08:34:01

为什么不简单地使用translate从开始中心。
弹出窗口的大小可以使用CSS制成,您已经通过使用相对视口单元进行宽度和高度来制作。

附带说明,我不会为包装器和内容元素设置宽度和高度,而只需将填充物应用于父元素即可。

const popup = document.querySelector('.popup');

function showPopUp() {
  popup.classList.toggle('show');
}
* {box-sizing: border-box;}

.popup {
  background: white;
  border: 1px solid gray;
  border-radius: 5px;
  position: fixed;
  display: block;
  min-height: 35vh;
  box-shadow: 1px 1px #f40000, 0em 0em 0.4em red;
  left: 50%;
  top: 50%;
  max-width: 90vw;
  max-height: 90vh;
  overflow: hidden;
  min-width: 300px;
  z-index: 0;
  transform: translate(-50%, -50%) scale(0);
  transition: transform 0.5s ease;
}

.popup.show {
  z-index: 200;
  transform: translate(-50%, -50%) scale(1);
}

.content{
    padding: 10px;
    max-width: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    height: 35vh;
    max-height: 60vh;
}
<button onclick="showPopUp()"> toggle pop up </button>

<div class="popup">
  <div class="content">
  content
  </div>
</div>

Why not simply center it from start using translate.
The size of your popup can be made with CSS, which you are already doing by using relative viewport units for width and height.

On a side note, I wouldn't set both width and height for the wrapper and the content element, but simply apply padding to the parent element.

const popup = document.querySelector('.popup');

function showPopUp() {
  popup.classList.toggle('show');
}
* {box-sizing: border-box;}

.popup {
  background: white;
  border: 1px solid gray;
  border-radius: 5px;
  position: fixed;
  display: block;
  min-height: 35vh;
  box-shadow: 1px 1px #f40000, 0em 0em 0.4em red;
  left: 50%;
  top: 50%;
  max-width: 90vw;
  max-height: 90vh;
  overflow: hidden;
  min-width: 300px;
  z-index: 0;
  transform: translate(-50%, -50%) scale(0);
  transition: transform 0.5s ease;
}

.popup.show {
  z-index: 200;
  transform: translate(-50%, -50%) scale(1);
}

.content{
    padding: 10px;
    max-width: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    height: 35vh;
    max-height: 60vh;
}
<button onclick="showPopUp()"> toggle pop up </button>

<div class="popup">
  <div class="content">
  content
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文