2015-11-07 12 views
0

我已經將內部div的背景顏色設置爲白色,並將其父div的不透明度設置爲0.6。看來內部div的背景顏色並不完全是白色的。如果我將父div的不透明度更改爲1.0,問題消失了,爲什麼? http://jsbin.com/zekacunefi/edit?html,output我已將背景顏色設置爲白色,但父元素的不透明度如何影響它?

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
\t <meta charset="UTF-8"> 
 
\t <title>Document</title> 
 
\t <script src="js/jquery.js"></script> 
 
\t <style> 
 
\t \t #msg_container { 
 
\t \t \t position: fixed; 
 
\t \t \t z-index: 3800; 
 
\t \t \t background-color: #000000; 
 
\t \t \t opacity: 0.6; 
 
\t \t \t left: 0; 
 
\t \t \t top: 0; 
 
\t \t \t right: 0; 
 
\t \t \t bottom: 0; 
 
\t \t } 
 

 
\t \t #modalDialog { 
 
\t \t \t display: inline-block; 
 
\t \t \t z-index: 3801; 
 
\t \t \t background-color: white; 
 
\t \t \t position: absolute; 
 
\t \t \t border: 1px solid rgb(103, 103, 103); 
 
\t \t \t box-shadow: 0 4px 17px 0px rgba(0,0,0,0.4); 
 
\t \t \t top: 50%; 
 
\t \t \t left: 50%; 
 
\t \t } 
 

 
\t \t body { 
 
\t \t \t background-color: blue; 
 
\t \t } 
 
\t </style> 
 
</head> 
 
<body> 
 
\t 
 
\t <script> 
 
\t \t MessageBox("abc\ndef\ng\nhdasfasdfsdsdfsd"); 
 
\t \t function MessageBox(str) { 
 
\t \t \t var boxHtml = "<div id='msg_container'><div id='modalDialog'></div></div>"; 
 
\t \t \t $("body").append(boxHtml); 
 
\t \t \t var md = $("#modalDialog"); 
 
\t \t \t var contentArray = str.split("\n"); 
 
\t \t \t var newArray = contentArray.map(function(ele, idx) { 
 
\t \t \t \t return ele + "<br>"; 
 
\t \t \t }); 
 
\t \t \t md.html("<p>" + newArray.join("") + "</p>"); 
 
\t \t \t var w = md.width(), 
 
\t \t \t  h = md.height(); 
 
\t \t \t md.css({ 
 
\t \t \t \t marginTop: -1 * h/2 + "px", 
 
\t \t \t \t marginLeft: -1 * w/2 + "px" 
 
\t \t \t }); 
 
\t \t \t $("#msg_container").appendTo($("body")); 
 
\t \t } 
 
\t </script> 
 
</body> 
 
</html>

回答

2

當您使用「不透明度」時,您不僅設置了div背景的不透明度,還設置了整個內容的不透明度。 (請參閱此W3C Wiki例如:http://www.w3.org/wiki/CSS/Properties/opacity

恕我直言,您應該從#msg_container和#modalDialog中刪除不透明度設置,然後使用替代rgba()來定義#msg_container的背景顏色。

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
\t <meta charset="UTF-8"> 
 
\t <title>Document</title> 
 
\t <script src="//code.jquery.com/jquery-1.11.3.min.js"></script> 
 
\t <style> 
 
\t \t #msg_container { 
 
\t \t \t position: fixed; 
 
\t \t \t z-index: 3800; 
 
\t \t \t background-color: rgba(0,0,0,0.6); 
 
\t \t \t left: 0; 
 
\t \t \t top: 0; 
 
\t \t \t right: 0; 
 
\t \t \t bottom: 0; 
 
\t \t } 
 

 
\t \t #modalDialog { 
 
\t \t \t display: inline-block; 
 
\t \t \t z-index: 3801; 
 
\t \t \t background-color: white; 
 
\t \t \t position: absolute; 
 
\t \t \t border: 1px solid rgb(103, 103, 103); 
 
\t \t \t box-shadow: 0 4px 17px 0px rgba(0,0,0,0.4); 
 
\t \t \t top: 50%; 
 
\t \t \t left: 50%; 
 
\t \t } 
 

 
\t \t body { 
 
\t \t \t background-color: blue; 
 
\t \t } 
 
\t </style> 
 
</head> 
 
<body> 
 
\t 
 
\t <script> 
 
\t \t MessageBox("abc\ndef\ng\nhdasfasdfsdsdfsd"); 
 
\t \t function MessageBox(str) { 
 
\t \t \t var boxHtml = "<div id='msg_container'><div id='modalDialog'></div></div>"; 
 
\t \t \t $("body").append(boxHtml); 
 
\t \t \t var md = $("#modalDialog"); 
 
\t \t \t var contentArray = str.split("\n"); 
 
\t \t \t var newArray = contentArray.map(function(ele, idx) { 
 
\t \t \t \t return ele + "<br>"; 
 
\t \t \t }); 
 
\t \t \t md.html("<p>" + newArray.join("") + "</p>"); 
 
\t \t \t var w = md.width(), 
 
\t \t \t  h = md.height(); 
 
\t \t \t md.css({ 
 
\t \t \t \t marginTop: -1 * h/2 + "px", 
 
\t \t \t \t marginLeft: -1 * w/2 + "px" 
 
\t \t \t }); 
 
\t \t \t $("#msg_container").appendTo($("body")); 
 
\t \t } 
 
\t </script> 
 
</body> 
 
</html>

1

它因爲父母的不透明度是影響內格太藍三色是身體的背景。相反,你可以做的是建立rgba()父DIV

試試這個

#msg_container { 
     position: fixed; 
     background-color: rgba(0, 0, 0, 0.6); 
     left: 0; 
     top: 0; 
     right: 0; 
     bottom: 0; 
    } 
1

您更改父的不透明度和它所有的孩子們。這意味着您將通過父級div和子級看到藍色背景。

相關問題