javascript - div 从指定顶部位置展开
问题描述
我正在尝试显示我的弹出 div,并在指定的顶部位置展开。
但是我应该打开主页中心的弹出窗口,我不知道该怎么做。
目前,我只能从主页的中心扩展到顶部和底部,这取决于弹出窗口的高度。
这就是我想要它做的事情:
┌──────────────────────────┐
│Main Page │
│ ┌─────────────────────┐ │
│ │
│ │
│ │
│ │
└──────────────────────────┘
┌──────────────────────────┐
│Main Page │
│ ┌─────────────────────┐ │
│ │popup div │ │
│ │
│ │
│ │
└──────────────────────────┘
┌──────────────────────────┐
│Main Page │
│ ┌─────────────────────┐ │
│ │popup div │ │
│ │ │ │
│ └─────────────────────┘ │
│ │
└──────────────────────────┘
但现在:
┌──────────────────────────┐
│Main Page │
│ │
│ │
│ │
│ │ │ │
│ │
│ │
└──────────────────────────┘
┌──────────────────────────┐
│Main Page │
│ │
│ │popup div │ │
│ │ │ │
│ │ │ │
│ │
│ │
└──────────────────────────┘
┌──────────────────────────┐
│Main Page │
│ ┌─────────────────────┐ │
│ │popup div │ │
│ │ │ │
│ │ │ │
│ └─────────────────────┘ │
│ │
└──────────────────────────┘
这是我的代码。
.popupLayer {
position: absolute;
width: 76%;
left: 50%;
top: 0%;
transform: translate(-50%, -55%);
outline: 1px solid red;
display: none;
background-color: white;
border-radius: 25px;
overflow: hidden;
transition: height 4s ease-in;
height: 0; // Animation works if you set initial height
}
var expandDiv = document.getElementsByClassName('popupLayer')[0];
expandDiv.style.display = 'block';
expandDiv.style.top = '55%';
setTimeout(function() {
expandDiv.style.height = '55%';
}, 1000);
<body>
... // some elements including popup div..
<div class = 'popupLayer'>
...// popuplayer body
</div>
...
</body>
解决方案
你必须删除transform: translate(-50%, -55%)
你的情况发生了什么
您的transform
属性将 div 设置在父 div 的中心位置,因为高度为 0,它将从确切的中心位置开始,随着高度的增加,浏览器使其成为父 div 的确切中心
您必须指定确切的顶部位置并删除transform
属性
var expandDiv = document.getElementsByClassName('popupLayer')[0];
expandDiv.style.display = 'block';
expandDiv.style.top = '27.5%';
setTimeout(function() {
expandDiv.style.height = '55%';
}, 1000);
.popupLayer {
position: absolute;
width: 76%;
left: 50%;
top: 0%;
transform: translateX(-50%);
outline: 1px solid red;
display: none;
background-color: white;
border-radius: 25px;
overflow: hidden;
transition: height 4s ease-in;
height: 0; // Animation works if you set initial height
}
<div class = 'popupLayer'>
...// popuplayer body
</div>
推荐阅读
- r - 使用 nls 进行指数回归
- mongodb - 如何仅打印 MongoDB 中子嵌入文档中的值?
- java - 如何在连接期间忽略空字符串?
- windows - MacOS .app 文件显示和行为类似于 Windows 资源管理器中的文件夹
- oracle - ORA-00904: : oracle 中的无效标识符错误修复
- python - 在python中获取录音的x和y数据
- flutter - ClipRRect 没有剪辑它的子小部件,我做错了什么?
- vba - 向模块添加过程会取消用户表单加载/显示,必须运行两次显示用户表单才能显示
- c++ - C ++ - 将双精度向量作为元组进行迭代
- coq - 无论如何我可以在 Coq 中输出所有最短路径吗?