2016-08-12 68 views
1

我有一個div在我的下拉菜單後面,當我使div透明(不透明屬性)時,它出現在懸停下拉菜單的前面,這會導致下拉菜單消失時鼠標進入div的區域。但我想保持div透明。我試過設置z-index屬性,但它沒有幫助。trasparent div隱藏透明下拉菜單懸停

下面是HTML代碼(簡體)

<div id="div1"> 
    <ul> 

    <li><a href="#">PROUCT</a> 
     <ul> 
     <li><a href="#">Product 1 </a></li> 
     <li><a href="#">Product 2</a></li> 
     <li><a href="#">Product 3</a></li> 
     <li><a href="#">Product 4</a></li> 
     <li><a href="#">Product 5</a></li> 
     <li><a href="#">Product 6</a></li> 
     <li><a href="#">Product 7</a></li> 
     </ul> 
    </li> 

    </ul> 

    <div id="buying_form"> 
    <h2> please fill your buying form</h2></div> 

</div> 

和css:

ul { 
margin: 0px; 
    padding: 0px; 
} 

ul li { 
    background-color: black; 
    border: 1px solid white; 
    width: 330px; 
    height: 30px; 
    line-height: 30px; 
    float: left; 
    text-align: center; 
    list-style: none; 
    opacity: .8; 
    z-index: 1px; 
} 

ul li a { 
    color: white; 
    text-decoration: none; 
    display: block; 
} 

ul li a:hover { 
    background-color: ORANGE; 
} 

ul li ul li { 
    display: none; 
} 

ul li:hover ul li { 
    display: block; 
    cursor: default; 
} 

#div1 { 
    width: 200px; 
    height: 650px; 
    background: url(bgi2.jpg); 
    text-align: center; 
} 

#buying_form { 
    float: left; 
    margin-left: 4px; 
    margin-top: 100px; 
    width: 326px; 
    height: 442px; 
    color: MEDIUMBLUE; 
    border: 1px solid gray; 
    background-color: #708090; 
    opacity: .5; 
} 

你可以看到這種行爲在這裏:

https://jsfiddle.net/xsmael/mdthqdh4/4/

回答

1

首先,不要使用opacity ...它會使內容也不透明。使用帶有alpha值的背景顏色(rgba)。 See this question

然後你需要定位子菜單絕對(與父母li)有position:relative;

ul { 
 
    margin: 0px; 
 
    padding: 0px; 
 
} 
 
ul li { 
 
    background-color: rgba(0, 0, 0, 0.8); 
 
    border: 1px solid white; 
 
    width: 330px; 
 
    height: 30px; 
 
    line-height: 30px; 
 
    float: left; 
 
    text-align: center; 
 
    list-style: none; 
 
    position: relative; 
 
} 
 
ul li a { 
 
    color: white; 
 
    text-decoration: none; 
 
    display: block; 
 
} 
 
ul li a:hover { 
 
    background-color: ORANGE; 
 
} 
 
ul li ul { 
 
    position: absolute; 
 
    display: none; 
 
} 
 
ul li:hover ul { 
 
    display: block; 
 
    cursor: default; 
 
} 
 
#div1 { 
 
    width: 200px; 
 
    height: 650px; 
 
    background: url(bgi2.jpg); 
 
    text-align: center; 
 
} 
 
#buying_form { 
 
    float: left; 
 
    margin-left: 4px; 
 
    margin-top: 100px; 
 
    width: 326px; 
 
    height: 442px; 
 
    color: MEDIUMBLUE; 
 
    border: 1px solid gray; 
 
    background-color: rgba(0, 0, 255, 0.25); 
 
} 
 
body { 
 
    background: lightgreen; 
 
}
<div id="div1"> 
 
    <ul> 
 
    <li><a href="#">PROUCT</a> 
 
     <ul> 
 
     <li><a href="#">Product 1 </a> 
 
     </li> 
 
     <li><a href="#">Product 2</a> 
 
     </li> 
 
     <li><a href="#">Product 3</a> 
 
     </li> 
 
     <li><a href="#">Product 4</a> 
 
     </li> 
 
     <li><a href="#">Product 5</a> 
 
     </li> 
 
     <li><a href="#">Product 6</a> 
 
     </li> 
 
     <li><a href="#">Product 7</a> 
 
     </li> 
 
     </ul> 
 
    </li> 
 
    </ul> 
 

 
    <div id="buying_form"> 
 
    <h2> please fill your buying form</h2> 
 
    </div> 
 

 
</div>