我創建一個CSS下拉菜單,而無需使用JavaScript和研究之後,我發現這個代碼 http://jsfiddle.net/zsp7t/1/關閉時,用戶點擊菜單外
<div class="dropdown" id="dropdown">
<input type="checkbox" id="drop1" />
<label for="drop1" class="dropdown_button">
<img src="http://ichef.bbci.co.uk/wwfeatures/43_43/images/live/p0/17/tx/p017txf6.jpg" height="43" width="43" /><span id="arrowSpan" class="arrow"></span>
</label>
<ul class="dropdown_content">
<li><a href="#">Privacy settings</a>
</li>
<li><a href="#">Account settings</a>
</li>
<li><a href="#">Logout</a>
</li>
</ul>
</div>
這裏一個彈出式菜單的CSS
body {
background-color: white;
font: normal 11px Tahoma, Verdana, Arial, Sans-Serif;
color: #222;
height: 380px;
}
#arrowSpan{
display:block;
margin-left:17px;
margin-top:2px;
margin-bottom:5px;
}
.dropdown {
display: block;
display: inline-block;
margin: 0px 3px;
position: relative;
}
/* ===[ For demonstration ]=== */
.dropdown {
margin-top: 25px
}
/* ===[ End demonstration ]=== */
.dropdown .dropdown_button {
cursor: pointer;
width: auto;
display: inline-block;
padding: 0px 0px;
border: 1px solid silver;
-webkit-border-radius: 0px;
-moz-border-radius: 0px;
border-radius: 2px;
font-weight: bold;
color: #717780;
line-height: 16px;
text-decoration: none !important;
background: white;
}
.dropdown input[type="checkbox"]:checked + .dropdown_button {
border: 1px solid #3B5998;
color: white;
background: silver;
-moz-border-radius-topleft: 2px;
-moz-border-radius-topright: 2px;
-moz-border-radius-bottomright: 0px;
-moz-border-radius-bottomleft: 0px;
-webkit-border-radius: 2px 2px 0px 0px;
border-radius: 2px 2px 0px 0px;
border-bottom-color: silver;
}
.dropdown input[type="checkbox"] + .dropdown_button .arrow {
display: inline-block;
width: 1px;
height: 1px;
border-top: 5px solid silver;
border-right: 5px solid transparent;
border-left: 5px solid transparent;
}
.dropdown input[type="checkbox"]:checked + .dropdown_button .arrow {
border-color: white transparent transparent transparent
}
.dropdown .dropdown_content {
position: absolute;
border: 1px solid #777;
padding: 0px;
background: white;
margin: 0;
display: none;
right:0;
}
.dropdown .dropdown_content li {
list-style: none;
margin-left: 0px;
line-height: 16px;
border-top: 1px solid #FFF;
border-bottom: 1px solid #FFF;
margin-top: 2px;
margin-bottom: 2px;
}
.dropdown .dropdown_content li:hover {
background: silver;
}
.dropdown .dropdown_content li a {
display: block;
padding: 2px 7px;
padding-right: 15px;
color: black;
text-decoration: none !important;
white-space: nowrap;
}
.dropdown .dropdown_content li:hover a {
color: white;
text-decoration: none !important;
}
.dropdown input[type="checkbox"]:checked ~ .dropdown_content {
display: block
}
.dropdown input[type="checkbox"] {
display: none
}
這對我來說是完美的......唯一的問題是,我希望用戶在空白區域外單擊時消失(取消選中框) 所以有什麼幫助嗎?
注:我不想用任何JavaScript
你不能,因爲toggler綁定到同一個源.. –
你也可以使用一個''標籤與目的地' #',並使用其':焦點'選擇器,也許? – Kroltan
雖然這是一種創建下拉菜單的有趣方式,但它完全打破了頁面的語義邏輯。 – Nit