js+CSS实现模拟华丽的select控件下拉菜单效果

<!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>模拟select控件</title>
<style> 
html,body{height:100%;overflow:hidden;}
body,div,form,h2,ul,li{margin:0;padding:0;}
ul{list-style-type:none;}
body{background:#23384e;font:12px/1.5 \5fae\8f6f\96c5\9ed1;}
#search,#search form,#search .box,#search .select,#search a{background:url(images/search.jpg) no-repeat;}
#search,#search .box,#search form{height:34px;}
#search{position:relative;width:350px;margin:10px auto;}
#search .box{background-position:right 0;}
#search form{background-repeat:repeat-x;background-position:0 -34px;margin:0 20px 0 40px;}
#search .select{float:left;color:#fff;width:190px;height:22px;cursor:pointer;margin-top:4px;line-height:22px;padding-left:10px;background-position:0 -68px;}
#search a{float:left;width:80px;height:24px;color:#333;letter-spacing:4px;line-height:22px;text-align:center;text-decoration:none;background-position:0 -90px;margin:4px 0 0 10px;}
#search a:hover{color:#f60;background-position:-80px -90px;}
#search .sub{position:absolute;top:26px;left:40px;color:#fff;width:198px;background:#2b2b2b;border:1px solid #fff;display:none;}
#search .sub li{height:25px;line-height:24px;cursor:pointer;padding-left:10px;margin-bottom:-1px;border-bottom:1px dotted #fff;}
#search .sub li.hover{background:#8b8b8b;}
</style>
<script type="text/javascript"> 
window.onload = function ()
{
 var oSelect = document.getElementsByTagName("span")[0];
 var oSub = document.getElementsByTagName("ul")[0];
 var aLi = oSub.getElementsByTagName("li");
 var i = 0;
 oSelect.onclick = function (event)
 {
  var style = oSub.style;
  style.display = style.display == "block" ? "none" : "block";
  //阻止事件冒泡
  (event || window.event).cancelBubble = true
 };
 for (i = 0; i < aLi.length; i++)
 {
  //鼠标划过
  aLi[i].onmouseover = function ()
  {
   this.className = "hover"
  };
  //鼠标离开
  aLi[i].onmouseout = function ()
  {
   this.className = "";
  };
  //鼠标点击
  aLi[i].onclick = function ()
  {
   oSelect.innerHTML = this.innerHTML 
  }
 }
 document.onclick = function ()
 {
  oSub.style.display = "none"; 
 };
};
</script>
</head>
<body>
<div id="search">
 <div class="box">
 <form>
  <span class="select">请选择游戏名称</span> 
  <a href="javascript:;">搜索</a>
 </form>
 </div>
 <ul class="sub">
 <li>地下城与勇士</li>
 <li>魔兽世界(国服)</li>
 <li>魔兽世界(台服)</li>
 <li>热血江湖</li>
 <li>大话西游II</li>
 <li>QQ幻想世界</li>
 </ul>
</div>
</body>
</html>

 

转载于:https://www.cnblogs.com/good10001/p/4778186.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里给出一个简单的实现,使用了jQuery和CSS模拟下拉菜单效果。 HTML代码: ``` <div class="custom-select"> <select> <option value="">请选择</option> <option value="1">选项一</option> <option value="2">选项二</option> <option value="3">选项三</option> </select> </div> ``` CSS代码: ``` .custom-select { position: relative; display: inline-block; font-size: 16px; font-weight: 500; line-height: 1.5; color: #333; } .custom-select select { display: none; } .custom-select::after { position: absolute; top: 50%; right: 10px; transform: translateY(-50%); content: "\f107"; font-family: "FontAwesome"; font-size: 14px; color: #999; pointer-events: none; } .custom-select.open::after { content: "\f106"; color: #666; } .custom-select-dropdown { position: absolute; top: 100%; left: 0; right: 0; z-index: 1000; display: none; max-height: 240px; overflow-y: auto; background-color: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2); } .custom-select.open .custom-select-dropdown { display: block; } .custom-select-dropdown-option { padding: 8px 12px; font-size: 14px; line-height: 1.5; color: #333; cursor: pointer; } .custom-select-dropdown-option:hover { background-color: #f5f5f5; } ``` JavaScript代码: ``` $(document).on("click", function(e) { var $target = $(e.target); if ($target.closest(".custom-select").length) { $target.closest(".custom-select").toggleClass("open"); } else { $(".custom-select").removeClass("open"); } }); $(".custom-select select").on("change", function() { var text = $(this).find("option:selected").text(); $(this).closest(".custom-select").find(".custom-select-text").text(text); }); $(".custom-select select").each(function() { var $this = $(this), numberOfOptions = $(this).children("option").length; $this.addClass("s-hidden"); $this.wrap('<div class="custom-select"></div>'); $this.after('<div class="custom-select-text">' + $this.children("option").eq(0).text() + '</div>'); var $dropdown = $('<div class="custom-select-dropdown"></div>'); for (var i = 0; i < numberOfOptions; i++) { $dropdown.append('<div class="custom-select-dropdown-option">' + $this.children("option").eq(i).text() + '</div>'); } $this.after($dropdown); }); ``` 这段代码的实现原理是: 1. 隐藏原生的下拉菜单,并在其外部包裹一个自定义的容器。 2. 在容器中添加一个显示当前选项的文本框,以及一个下拉菜单容器。 3. 遍历所有选项,把它们添加到下拉菜单容器中。 4. 绑定点击事件,让点击自定义下拉菜单的容器时,切换下拉菜单的显示状态。 5. 绑定下拉菜单选项的点击事件,让其选中对应的选项,并更新文本框的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值