jquery類比實現仿select效果,jquery類比仿select

來源:互聯網
上載者:User

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

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.