我已经完成了以下工作:
HTML部分:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<div id="dd" class="select-wrapper">--- Select ---
<div class="categories">
<div class="category">1</div>
<div class="category">2</div>
<div class="category">3</div>
</div>
CSS部分:
.select-wrapper {
/* Size & position */
position: relative;
width: 200px;
margin-top: 25px;
margin-left: 25px;
padding: 12px 15px;
/* Styles */
background: #fff;
border-radius: 5px;
box-shadow: 0 1px 0 rgba(0,0,0,0.2);
cursor: pointer;
outline: none;
-webkit-transition: all 0.3s ease-out;
-moz-transition: all 0.3s ease-out;
-ms-transition: all 0.3s ease-out;
-o-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
}
.select-wrapper:after { /* Little arrow */
content: "";
width: 0;
height: 0;
position: absolute;
top: 50%;
right: 15px;
margin-top: -3px;
border-width: 6px 6px 0 6px;
border-style: solid;
border-color: #4cbeff transparent;
}
.select-wrapper .categories {
/* Size & position */
position: absolute;
top: 100%;
left: 0;
right: 0;
/* Styles */
background: #fff;
border-radius: 0 0 5px 5px;
border: 1px solid rgba(0,0,0,0.2);
border-top: none;
border-bottom: none;
list-style: none;
-webkit-transition: all 0.3s ease-out;
-moz-transition: all 0.3s ease-out;
-ms-transition: all 0.3s ease-out;
-o-transition: all 0.3s ease-out;
transition: all 0.3s ease-out;
/* Hiding */
max-height: 0;
overflow: hidden;
}
.select-wrapper .categories div {
display: block;
text-decoration: none;
color: #333;
padding: 10px;
transition: all 0.3s ease-out;
border-bottom: 1px solid #e6e8ea;
}
/* Hover state */
.select-wrapper .categories div:hover {
color: #57a9d9;
}
/* Active state */
.select-wrapper.active {
border-radius: 5px 5px 0 0;
background: #4cbeff;
box-shadow: none;
border-bottom: none;
color: white;
}
.select-wrapper.active:after {
border-color: #82d1ff transparent;
}
.select-wrapper.active .categories {
border-bottom: 1px solid rgba(0,0,0,0.2);
max-height: 400px;
}
.select-wrapper:focus {
border-radius: 5px 5px 0 0;
background: #4cbeff;
box-shadow: none;
border-bottom: none;
color: white;
}
.select-wrapper:focus:after {
border-color: #82d1ff transparent;
}
.select-wrapper:focus .categories {
border-bottom: 1px solid rgba(0,0,0,0.2);
max-height: 400px;
}
jQuery部分:
function DropDown(el) {
this.dd = el;
this.initEvents();
}
DropDown.prototype = {
initEvents : function() {
var obj = this;
obj.dd.on('click', function(event){
$(this).toggleClass('active');
event.stopPropagation();
});
}
}
$(function() {
var dd = new DropDown( $('#dd') );
$(document).click(function() {
// all dropdowns
$('.select-wrapper').removeClass('active');
});
});
要查看它的实际效果:http: //jsfiddle.net/john_rock/LhUsc/
我认为这可以帮助您解决问题。