簡體   English   中英

顯示隱藏的子div而不更改父div的大小

[英]show hidden child div without changing the size of parent div

我有以下代碼:

 function show() { document.getElementById("child").style.display = "block"; } 
 #parent { background-color: orange; padding: 2em; } #child { background-color: pink; padding: 3em; display: none; } 
 <div id="parent" onmouseover="show()"> <div id="child"> Child div </div> </div> 

當我將鼠標懸停在父級div上時,父級的大小會更改。 有沒有辦法在不改變父div大小的情況下將子div顯示在父div之上?

您可以使用

visibility:hidden

代替

display:none

顯示/隱藏內容並保持大小不變​​。 需要相應地更改腳本。

您可以在父元素上使用相對位置,在子元素上使用絕對位置:

 function show() { document.getElementById("child").style.display="block"; } 
 #parent { background-color: orange; padding:2em; position:relative; } #child { background-color: pink; padding: 3em; display: none; position: absolute; } 
 <html> <head> <title></title> </head> <body> <div id="parent" onmouseover="show()"> <div id="child"> Child div </div> </div> </body> </html> 

這是您需要的嗎http://jsfiddle.net/u74m4x9o/

<div id="parent" onmouseover="show()">
  <div id="child">
    Child div
  </div>
</div>


   function show() {
  document.getElementById("child").style.display = "block";
} 



#parent {
  background-color: orange;
  padding: 2em;
}
#child {
  background-color: pink;
  padding: 1em;
  display: none;
  position:absolute;
  top : 1em
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM