jquery類比實現仿select效果,jquery類比仿select
本著服務為人民的遠大理想,最近寫了個jquery類比select效果的小東東,挺好用,分享下。
可以直接在我上傳的資源裡下載壓縮包,:jquery類比實現仿select效果。
首先,上html頁面代碼
<!doctype html><html><head><meta charset="utf-8"><title>js類比select</title><link rel="stylesheet" type="text/css" href="style.css"></head><body><div class="container"> <div class="select-box select-city"> <div class="select-header"> 省份 </div> <ul class="select-content"> <li>北京</li> <li>河北省</li> <li>山東省</li> </ul> </div> <div class="select-box select-city"> <div class="select-header"> 城市 </div> <ul class="select-content"> <li>北京</li> </ul> </div> <div class="select-box select-city"> <div class="select-header"> 區/縣 </div> <ul class="select-content"> <li>海澱區</li> <li>朝陽區</li> <li>東城區</li> </ul> </div></div></body><script src="js/jquery.min.js"></script><script src="js/select.js"></script></html>
然後,上css代碼
@charset "utf-8";body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td,p{margin:0;padding:0; font-family:"微軟雅黑", Microsoft Yahei, Arial, Helvetica, sans-serif;}ol,ul{list-style:none;}.container{ padding-top:100px; margin-left:100px;}/*** 類比select**/.select-box { position: relative; float: left; margin-right: 8px; }.select-city .select-header { width: 88px; height: 36px; border: 1px solid #c1c1c1; padding-right: 26px; background: url(img/arrow.png) no-repeat #f9f9f9; background-position: 95px 15px; line-height: 36px; text-indent: 14px; font-size: 16px; color: #a9a9a9; cursor: pointer; overflow: hidden; }.select-city .select-arrow { background-position: 95px -9px; }.select-city .select-content { border-left: 1px solid #c1c1c1; border-right: 1px solid #c1c1c1; position: absolute; top: 38px; left: 0; z-index: 99; display: none; background-color: #f9f9f9; }.select-city .select-content li { width: 114px; height: 36px; margin: 0; border-bottom: 1px solid #c1c1c1; line-height: 36px; text-indent: 14px; background-color: #f9f9f9; font-size: 16px; color: #a9a9a9; cursor: pointer; }.select-bank .select-header { width: 272px; height: 36px; border: 1px solid #c1c1c1; padding-right: 26px; background: url(img/arrow.png) no-repeat #f9f9f9; background-position: 279px 15px; line-height: 36px; text-indent: 14px; font-size: 16px; color: #a9a9a9; cursor: pointer; overflow: hidden; }.select-bank .select-arrow { background-position: 279px -9px; }.select-bank .select-content { border-left: 1px solid #c1c1c1; border-right: 1px solid #c1c1c1; position: absolute; top: 38px; left: 0; z-index: 99; display: none; background-color: #f9f9f9; }.select-bank .select-content li { width: 298px; height: 36px; margin: 0; border-bottom: 1px solid #c1c1c1; line-height: 36px; text-indent: 14px; background-color: #f9f9f9; font-size: 16px; color: #a9a9a9; cursor: pointer; }
最後,上js代碼:
$(function(){/** * 類比select **/ $(".select-header").click(function(){ $(this).parent().siblings(".select-box").find(".select-content").slideUp("fast"); if($(this).siblings(".select-content").is(":hidden")){ $(this).addClass("select-arrow"); $(this).siblings(".select-content").slideDown("fast"); var evt = new Object; if ( typeof(window.event) == "undefined" ){//如果是Firefox瀏覽器 evt = arguments.callee.caller.arguments[0]; }else{ evt = event || window.event; } evt.cancelBubble = true; }else{ $(this).removeClass("select-arrow"); $(this).siblings(".select-content").slideUp("fast");//去除事件冒泡 var evt = new Object; if ( typeof(window.event) == "undefined" ){//如果是Firefox瀏覽器 evt = arguments.callee.caller.arguments[0]; }else{ evt = event || window.event; } evt.cancelBubble = true; } });$(document).click(function(){ $(".select-header").removeClass("select-arrow"); $(".select-content").slideUp("fast"); }); $(".select-content li").on("click",function(){ $(this).parent().siblings(".select-header").removeClass("select-arrow"); $(this).parent().siblings(".select-header").text($(this).text()).end().slideUp("fast"); }); $(".select-content li").hover(function(){ $(this).css("background-color","#cfcfcf"); },function(){ $(this).css("background-color","#fff"); });});
效果如下:
有任何交流和指教,可加我QQ:1740437
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。