[英]Javascript doesn't seem to correctly handle hidden elements that are later displayed
I am having difficulty understanding the following behaviour of Javascript / CSS. 我很难理解Javascript / CSS的以下行为。
I am attempting to create a modal div which will serve as an enlarged photo from a gallery. 我正在尝试创建一个模态div,将其用作画廊的放大照片。 I want to display this dead centre on screen.
我想在屏幕上显示这个死点。 If I define the div as display: none and then change this via Javascript, it doesn't seem to work as I would expect.
如果我将div定义为display:none,然后通过Javascript进行更改,则它似乎无法正常运行。
I appreciate it would be simpler to just use Example 1 below and it isn't necessary to hide the div first. 我理解只使用下面的示例1会更简单,并且不必先隐藏div。 What I would like to do, however, is define the div in HTML with display: none and then display it later.
但是,我想做的是使用display:none在HTML中定义div,然后再显示它。 I don't want to have to create the div with Javascript.
我不想用Javascript创建div。 The below is simply the easiest way to illustrate the issue.
以下是说明问题的最简单方法。
Example 1 below works as you might expect. 下面的示例1可以正常工作。 Example 2 works only if you comment out the last line which sets the margins.
示例2仅在注释掉设置边距的最后一行时才有效。 If you do, then the div vanishes.
如果这样做,那么div消失了。
Example 1 - Works as expected 示例1-按预期工作
CSS CSS
#photoviewdiv{
position: fixed;
/*display: none;*/
width: 70%;
top: 50%;
left: 50%;
background: white;
z-index:1000;
}
JS JS
var photoviewdiv = document.createElement("div");
photoviewdiv.setAttribute("id", "photoviewdiv");
document.body.appendChild(photoviewdiv);
//photoviewdiv.style.display = "inline";
photoviewdiv.innerHTML = "Hello";
var viewerwidth = photoviewdiv.offsetWidth;
var viewerheight = photoviewdiv.offsetHeight;
photoviewdiv.setAttribute("style",
"margin-left: -" + viewerwidth/2 +"px; margin-top: -" + viewerheight/2 +"px");
Example 2 - Only works up until set margins - then photoviewdiv disappears 示例2-仅工作到设置页边距-然后photoviewdiv消失
CSS CSS
#photoviewdiv{
position: fixed;
display: none;
width: 70%;
top: 50%;
left: 50%;
background: white;
z-index:1000;
}
JS JS
var photoviewdiv = document.createElement("div");
photoviewdiv.setAttribute("id", "photoviewdiv");
document.body.appendChild(photoviewdiv);
photoviewdiv.style.display = "inline";
photoviewdiv.innerHTML = "Hello";
var viewerwidth = photoviewdiv.offsetWidth;
var viewerheight = photoviewdiv.offsetHeight;
photoviewdiv.setAttribute("style",
"margin-left: -" + viewerwidth/2 +"px; margin-top: -" + viewerheight/2 +"px");
You're setting style.display = inline
, but later removing it when you reduce style
to just margin-left
and margin-top
. 您正在设置
style.display = inline
,但是稍后在将style
减小为margin-left
和margin-top
时将其删除。 The original display: none
from the CSS is all that's left. 原始
display: none
仅剩CSS了。
Include a visible display
property alongside the margins: 在边距旁边包括一个可见的
display
属性:
photoviewdiv.setAttribute("style",
"margin-left: -" + viewerwidth/2 + "px; " +
"margin-top: -" + viewerheight/2 +"px; " +
"display: block");
Alternatively: 或者:
photoviewdiv.style.display = "block";
var viewerwidth = photoviewdiv.offsetWidth;
var viewerheight = photoviewdiv.offsetHeight;
photoviewdiv.style.marginLeft = "-" + viewerwidth / 2 + "px";
photoviewdiv.style.marginTop = "-" + viewerheight / 2 + "px";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.