简体   繁体   English

如何从中心而不是左上角展开 div

[英]How to expand div from center instead of top left

I have a login form centered in the middle of the screen with this CSS:我在屏幕中间有一个登录表单,上面有这个 CSS:

.login-form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: block;

  align-items: center;
  padding: 50px 40px;
  color: white;
  background: rgba(0,0,0,0.8);
  border-radius: 10px;
  text-align: center;
  box-shadow: 0 0.4px 0.4px rgba(128, 128, 128, 0.109),
    0 1px 1px rgba(128, 128, 128, 0.155),
    0 2.1px 2.1px rgba(128, 128, 128, 0.195),
    0 4.4px 4.4px rgba(128, 128, 128, 0.241),
    0 12px 12px rgba(128, 128, 128, 0.35);
}

After the user logs in I want to expand the div to 100% to show other content.用户登录后,我想将 div 扩展到 100% 以显示其他内容。 After the user has pressed the login button the class of the div changes to this CSS:用户按下登录按钮后,div 的 class 变为此 CSS:

.full{
    width: 100%;
    height: 100%;
    transition: 2s;
}

It does the job, but the div moves to the top left corner and starts expanding from there.它完成了这项工作,但 div 移动到左上角并从那里开始扩展。 How to make it expand from the middle?如何让它从中间展开?

Don't change the class, add it:不要更改 class,添加它:

document.getElementById("div-id").className += " full";

You can try using tranform for this.您可以尝试为此使用转换。 In your initial class, set the X scale to 0 and transform the origin to the center.在您的初始 class 中,将 X 比例设置为 0 并将原点转换为中心。 Then in your animation class, just change thr X scale to 1 and transition the change like this:然后在您的 animation class 中,只需将 thr X 比例更改为 1 并将更改转换如下:

.login-form {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: block;
    transform: scaleX(0);
    transform-origin : 50%;


.full {
transform : scaleX(1);
transition : transform 200ms ease-in;
} 

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

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