以下是 jquery自定义select下拉样式js代码 的示例演示效果:
部分效果截图:
HTML代码(index.html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery自定义select下拉样式</title>
<link href="css/zzsc.css" type="text/css" rel="stylesheet" />
<script src="js/jquery.min.js"></script>
</head>
<body>
<div class="main">
<div id="cate">
<div class="cate_wrp">
<div class="cate_inp cate_tri">程序员</div>
<a href="###" class="cate_tri"></a> </div>
<div class="cate_drop" style="display:none">
<ul>
<li>焦点图特效</li>
<li>菜单导航</li>
<li>flash素材</li>
<li>jquery特效</li>
</ul>
</div>
</div>
</div>
<script type="text/javascript">
// 下拉列表js
(function($) {
var $cate = $('#cate'),
$tri = $('.cate_tri', $cate),
$drop = $('div.cate_drop', $cate),
$inp = $('div.cate_inp', $cate);
$tri.on('click', function(event) {
var $el = $(this);
if( $el.data('active') !== 'on' ) {
$drop[0].style.display = 'block';
$el.data('active', 'on');
} else {
$drop[0].style.display = 'none';
$el.data('active', 'off')
}
});
$drop.on('mouseover', 'li', function(event) {
$inp[0].innerHTML = this.innerHTML;
}).on('click', 'li', function(event) {
// do something
$drop[0].style.display = 'none';
$tri.data('active', 'off');
});
}(jQuery));
</script>
</body>
</html>
CSS代码(zzsc.css):
@charset "utf-8";*{margin:0;padding:0;list-style:none;}
body{font-size:12px;text-align:center;}
a{text-decoration:none;}
img{border:0;}
.main{width:200px;height:30px;margin:100px auto;}
#cate{float:left;height:26px;line-height:26px;text-align:left;width:200px;padding-right:20px;position:relative;}
#cate .cate_wrp{overflow:hidden;}
#cate .cate_inp{padding-left:15px;height:24px;width:155px;border-radius:3px 0 0 3px;border:1px solid #D4D4D4;border-right:none;background:#fff;float:left;}
#cate a.cate_tri:link,#cate a.cate_tri:visited{width:26px;height:26px;float:left;background:transparent url('../images/select_right.gif') no-repeat center center;}
#cate .cate_drop{background:#E2E2E2;width:195px;border:1px solid #ADADAD;border-radius:2px;}
#cate .cate_drop li{padding-left:15px;width:180px;}
#cate .cate_drop li:hover{background:#E5007F;color:#fff;cursor:pointer;}