简体   繁体   English

无法获得内部div覆盖外部div

[英]Cannot get an inner div to overlay an outer div

I have a situation as shown in screen shot below where I would like an inner div to overlay an outer div when outer div has a CSS of position:relative . 我有一种情况,如下面的屏幕截图所示,当外部div具有position:relative的CSS时,我希望内部div覆盖外部div。 I have tried to do this, which you can see in following online demo: Demo of my code 我尝试这样做,您可以在下面的在线演示中看到: 我的代码的演示

覆盖Div

  • I have two JavaScript methods called overlay and undoOverlay . 我有两个JavaScript方法,称为overlayundoOverlay
  • When I call the overlay method, the id of div to overlay and id of div to cover are passed, but it does not result in the yellow div to cover/overlay the outer orange div. 当我调用overlay方法时,将传递要覆盖的div的ID和要覆盖的div的ID,但不会导致黄色div覆盖/覆盖外部的橙色div。
  • The undoOverlay method returns the overlayed div to it's original state. undoOverlay方法将覆盖的div返回其原始状态。

Question : How can I make the method overlay work as expected ie it should cause the yellow div to exactly overlay/cover the orange div? 问题 :如何使方法overlay按预期工作,即应使黄色div完全覆盖/覆盖橙色div?

JavaScript code I have tried for this situation 我为这种情况尝试过的JavaScript代码

<button type="button" onclick="overlay('div2','div1');">Overlay Yellow Div on Orange Div</button>&nbsp;&nbsp;
<button type="button" onclick="undoOverlay('div2');">UNDO Overlay</button>
<br><br>
<div id="div1" style="position:relative;background-color:orange;border:2px solid red;height:500px;width:300px;">This is OUTER div
<div id="div2" style="background-color:lightyellow;border:2px solid green;height:400px;width:270px;">This is INNER div</div>
</div>

<script>
//original dimensions of div to show when overlaying
var originalWidth = null;
var originalHeight = null;

//makes first div to exactly cover the second div
function overlay(divIdToShow, divIdToCover) {
   originalWidth = document.getElementById(divIdToShow).style.width;
   originalHeight = document.getElementById(divIdToShow).style.height;
   document.getElementById(divIdToShow).style.width =  document.getElementById(divIdToCover).style.width;
   document.getElementById(divIdToShow).style.height =  document.getElementById(divIdToCover).style.height;
}

//returns an overlayed div to it's original state
function undoOverlay(divId) {
   document.getElementById(divId).style.width =  originalWidth;;
   document.getElementById(divId).style.height =  originalHeight;
}
</script>

 #div1 { position:relative; background-color:orange; /*border:2px solid red;*/ outline:2px solid red; height:500px; width:300px; } #div2 { background-color:lightyellow; /*border:2px solid green;*/ outline:2px solid green; /*height:400px;*/ /*width:270px;*/ /*display: none;*/ } .overlay { position: absolute; top: 0; right: 0; bottom: 0; left: 0; } .normal { height: 400px; width: 270px; } 
 <button type="button" onclick="toggleOverlay('div2',true);">Overlay Yellow Div on Orange Div</button>&nbsp;&nbsp; <button type="button" onclick="toggleOverlay('div2',false);">UNDO Overlay</button> <br><br> <div id="div1" style="">This is Orange BASE div <div id="div2" class="normal" style="">This is Yellow OVERLAY div</div> </div> <script> //var originalWidth = document.getElementById(divIdToShow).style.width + "px"; //var originalHeight = document.getElementById(divIdToShow).style.height + "px"; function toggleOverlay(id, show) { if (show) { //makes first div to exactly cover the second div document.getElementById(id).removeAttribute('class'); document.getElementById(id).className = "overlay"; } else { //returns an overlayed div to it's original height and width document.getElementById(id).removeAttribute('class'); document.getElementById(id).className = "normal"; } } </script> 

You absolutely do not need JS for this. 您绝对不需要JS。 Just position the inner div with position: absolute . 只需将内部div的position: absolute

Add this to your inner div: 将此添加到您的内部div中:

position: absolute; 
top: 0; 
left: 0;
width: 100%;
height: 100%;
<button type="button" onclick="overlay('div2','div1');">Overlay Yellow Div on Orange Div</button>&nbsp;&nbsp;
<button type="button" onclick="undoOverlay('div2');">UNDO Overlay</button>
<br><br>
<div id="div1" style="position:relative;background-color:orange;border:2px solid red;height:500px;width:300px;">This is OUTER div
<div id="div2" style="background-color:lightyellow;border:2px solid green;min-height:400px;min-width:270px;">This is INNER div</div>
</div>

<script>
//original dimensions of div to show when overlaying
var originalWidth = null;
var originalHeight = null;

function overlay(divIdToShow, divIdToCover) {
   originalWidth = document.getElementById(divIdToShow).style.width;
   originalHeight = document.getElementById(divIdToShow).style.height;
   document.getElementById(divIdToShow).style.position = "absolute";  
   document.getElementById(divIdToShow).style.top = '0';
   document.getElementById(divIdToShow).style.bottom = '0';
   document.getElementById(divIdToShow).style.left = '0';
   document.getElementById(divIdToShow).style.right = '0';
}
function undoOverlay(divId) {
   document.getElementById(divId).style.width =  originalWidth;;
   document.getElementById(divId).style.height =  originalHeight;
}
</script>

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

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