[英]My overlay pushes the rest of the page down rather than appearing on top
I want a <div>
to appear over the current screen when the user clicks on an image whilst darkening the background out behind the overlay.当用户单击图像同时使叠加层后面的背景变暗时,我希望
<div>
出现在当前屏幕上。 However rather than the <div>
appearing just over the top of the screen in the current position, it pushes everything in the background beneath it.然而,在当前的 position 中,
<div>
并没有出现在屏幕顶部,而是将背景中的所有内容都推到了它的下方。
Screenshot before Overlay: Overlay前的截图:
Screen after Overlay has been applied (you can see it pushes the other elements beneath):应用 Overlay 后的屏幕(您可以看到它将其他元素推到下方):
On a side note, is there a way in which just by clicking in the darkened out background (out of the overlay) it closes the overlay down?在旁注中,有没有一种方法可以通过单击变暗的背景(在覆盖层之外)来关闭覆盖层?
HTML: HTML:
<section id="content">
<div class="container">
<section id="grid" class="clearfix">
<div class="cf show-grid">
<div class="row">
<!-- Start Overlay -->
<div id="overlay"></div>
<!-- End Overlay -->
<!-- Start Special Centered Box -->
<div id="specialBox">
<button onmousedown="toggleOverlay()">Close Overlay</button>
</div>
<!-- End Special Centered Box -->
<!-- Start Normal Page Content -->
<div id="wrapper">
<button onmousedown="toggleOverlay()">Apply Overlay</button>
</div>
<!-- End Normal Page Content -->
<div class="grid-2 dashboardIcons">
<h3 class="fontAmaticH1">Stress & Anxiety</h3>
<a href="stess-anxiety.php"><img src="images/stress.jpg"></a>
</div>
<div class="grid-2 dashboardIcons">
<h3 class="fontAmaticH1">Mindfulness</h3>
<a class="cursor" onmousedown="toggleOverlay()"><img src="images/mindfulness.jpg"></a>
</div>
<div class="grid-2 dashboardIcons">
<h3 class="fontAmaticH1">Exam Preperation</h3>
<a href="exam-prep.php"><img src="images/examPrep.jpg"></a>
</div>
</div>
</div>
</section>
</div>
</section>
CSS: CSS:
/*Practising the overlays for modules */
div#overlay {
display: none;
z-index: 2;
background: #000;
position: fixed;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
text-align: center;
}
div#specialBox {
display: none;
position: relative;
z-index: 3;
margin: 150px auto 0px auto;
width: 500px;
height: 300px;
background: #FFF;
color: #000;
}
div#wrapper {
position:absolute;
top: 0px;
left: 0px;
padding-left:24px;
}
JS:记者:
/* This is for the page overloays within the module pages */
function toggleOverlay(){
var overlay = document.getElementById('overlay');
var specialBox = document.getElementById('specialBox');
overlay.style.opacity = .8;
if(overlay.style.display == "block"){
overlay.style.display = "none";
specialBox.style.display = "none";
} else {
overlay.style.display = "block";
specialBox.style.display = "block";
}
}
For info: I am using Foldy Grids to ensure my divs are all responsive etc.有关信息:我正在使用Foldy Grids来确保我的 div 都是响应式的等等。
For div#specialBox
try this:对于
div#specialBox
试试这个:
position: fixed;
top: 150px;
left: 50%;
transform: translateX(-50%); /* this will place it in the center */
And delete this (also for div#specialBox
) :并删除它(也适用于
div#specialBox
) :
position: relative;
margin: 150px auto 0px auto;
While div#specialBox
hasposition: relative
then the block will be in "main flow" and push the rest page down.当
div#specialBox
有position: relative
时,该块将处于“主流”并将 rest 页面向下推。 Position fixed
and absolute
"remove" the block from this flow. Position
fixed
且absolute
地“移除”此流中的块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.