首页 > 代码库 > jquery 实现下拉选择框

jquery 实现下拉选择框

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,
maximum-scale=1.0,user-scalable=no">
<title></title>
<style>
body {
-webkit-tap-highlight-color: transparent
}

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

.in .content .select {
background: #FFFFFF;
margin: 0px auto;
position: relative;
cursor: pointer;
}

.in .content .select p {
margin-top: 16px;
font-size: 16px;
font-family: "microsoft yahei";
color: #666666;
text-align: center
}

.in .content .select p.latest {
border-right: 1px solid #e5e5e5;
}

.in .content ul {
width: 100%;
display: block;
font-size: 16px;
background: #FFFFFF;
position: absolute;
top: 49px;
left: 0px;
max-height: 0px;
overflow: hidden;
transition: max-height .3s ease-in;
}

.in .content ul li {
margin-left: 30px;
list-style: none;
font-size: 16px;
color: #666666;
border-bottom: 1px solid #e5e5e5;
display: flex
}

.in .content ul li span {
flex: 1;
line-height: 48px;
}

.in .content ul li i {
flex: 1;
text-align: right;
padding-right: 30px;
height: 48px;
line-height: 48px;
color: #fff;
}

.in .content ul li.Selected {
color: #00a73e;
}

.in .content ul li.Selected i {
color: #00a73e;
}

.in .content ul li:hover {
color: #00a73e;
}

.in .content.open p {
color: #00a73e
}

.in .content.open ul {
max-height: 250px;
transform-origin: 50% 0;
-webkit-animation: slide-down .5s ease-in;
transition: max-height .2s ease-in;
}

.in {
height: 48px;
border-bottom: 1px solid #e5e5e5;
display: flex;
}

.in .content {
flex: 1;
background-color: #fff
}

.select1 {
border-right: 1px solid #e5e5e5
}
</style>

</head>

<body>
<div class="in">
<div class="content">
<div class="select">
<p class="select1" data-value="http://www.mamicode.com/所有选项">Select 1 </p>
</div>
<ul>
<li data-value="http://www.mamicode.com/Html"><span>Html</span> <i class="icon-choice">√</i></li>
<li data-value="http://www.mamicode.com/Html5"><span>Html5</span><i class="icon-choice">√</i></li>
<li data-value="http://www.mamicode.com/JavaScript"><span>JavaScript</span><i class="icon-choice">√</i></li>
<li data-value="http://www.mamicode.com/Jquery"><span>Jquery</span><i class="icon-choice">√</i></li>
</ul>
</div>
<div class="content">
<div class="select">
<p data-value="http://www.mamicode.com/所有选项">Select 2</p>
</div>
<ul>
<li data-value="http://www.mamicode.com/Html2"><span>Html2</span> <i class="icon-choice">√</i></li>
<li data-value="http://www.mamicode.com/Html52"><span>Html52</span><i class="icon-choice">√</i></li>
<li data-value="http://www.mamicode.com/JavaScript2"><span>JavaScript2</span><i class="icon-choice">√</i></li>
<li data-value="http://www.mamicode.com/Jquery2"><span>Jquery2</span><i class="icon-choice">√</i></li>
</ul>
</div>
</div>
<script src="http://www.mamicode.com/js/jquery-1.10.1.min.js"></script>
<script>
$(function() {
$(".select p").click(function(e) {
$(this).parent().toggleClass(‘open‘);
$(this).parent().parent().siblings().removeClass(‘open‘);
$(this).parent().parent().siblings().find(".select").removeClass(‘open‘);
$(this).parent().parent().toggleClass(‘open‘);
e.stopPropagation();
});

$(".content ul li").click(function(e) {
var _this = $(this);
$(this).parent().parent().find("p").text(_this.attr(‘data-value‘));
_this.addClass("Selected").siblings().removeClass("Selected");
$(this).parent().parent().removeClass("open");
$(this).parent().parent().find(".select").removeClass("open");
e.stopPropagation();
});

$(document).on(‘click‘, function() {
$(".content").removeClass("open");
$(".content .select").removeClass("open");
})
});
</script>
</body>

</html>

jquery 实现下拉选择框