【发布时间】:2018-04-12 15:28:50
【问题描述】:
我有一个高度和宽度为空的叠加层,一旦使用按钮单击它就会覆盖整个页面,这个叠加层从 top 0 left 0 一直向下过渡到右下角。关闭此覆盖层后,我使用 java 脚本编辑了 css 属性,以便它在右下角完成转换。
第一次打开和关闭此叠加层时,它工作正常,但是我第二次打开它时,它不是从左上角开始,而是从右下角开始并一直移动到左上角。我似乎无法解决这个问题。我尝试将左侧重置为 0 并将顶部重置为 0,但它不起作用。
document.querySelectorAll(".overlay_background")[0].addEventListener("click", parent);
function parent(event) {
this.style.left = this.clientWidth + "px";
this.style.top = this.clientHeight + "px";
this.style.height = 0;
this.style.width = 0;
}
function child(event) {
event.stopPropagation();
}
function http_request_availability() {
document.getElementById("overlay_background_id").style.left = "0";
document.getElementById("overlay_background_id").style.top = "0";
console.log(document.getElementById("overlay_background_id"));
document.getElementById("overlay_background_id").style.height = "100vh";
document.getElementById("overlay_background_id").style.width = "100vw";
document.getElementById("overlay_background_id").style.backgroundColor = "rgba(0,0,0, 0.5)";
document.body.style.overflow = "hidden";
}
.overlay_background {
position: fixed;
overflow: hidden;
height: 0;
width: 0;
left: 0;
top: 0;
transition: 0.7s ease;
z-index: 1;
}
<div class="overlay_background" id="overlay_background_id">
Hi this is my overlay
</div>
<button class='btn_ws' onclick='http_request_availability();'>Book Work-Shop</button></div>
【问题讨论】:
-
Uncaught TypeError: Cannot read property 'addEventListener' of undefined。缺少.overlay_ws_container -
我删除了那段代码。再试一次:)
-
这是因为在父级内部关闭时,您从未将 left + top 属性设置回 0,您不能这样做,因为它会干扰当前的自顶向下动画。
-
感谢您花时间解释,我有点想过,但我不知道如何解决这个问题
标签: javascript css