項(xiàng)目中需要自定義一個(gè)下拉框多選插件,業(yè)務(wù)問題還是自己實(shí)現(xiàn)比較好
10余年的海港網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)整合營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整海港建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。成都創(chuàng)新互聯(lián)從事“海港網(wǎng)站設(shè)計(jì)”,“海港網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
通過$.fn 向jQuery添加新的方法
下拉數(shù)據(jù)通過參數(shù)傳遞進(jìn)去,通過調(diào)用該插件時(shí)接收,選擇后的確定與取消事件采用事件傳遞方式
代碼如下:
1.效果圖
2.代碼
.dropdown-container{ display: block; width: 200px; height: 30px; padding: 0px; position: relative; margin: 0px auto; } .dropdown-display{ display: block; height: 30px; position: absolute; top: 0; width:100%; margin: 0px; border:1px solid steelblue; } .dropdown-display span{ background: url(../static/choose_down_icon_01.png) no-repeat left 8px; display: block; height: 25px; width: 10px; position: absolute; right: 5px; top: 3px; } .dropdown-display input[class='iptdiplay']{ border: none; border-color: transparent; background: transparent; border-radius: 0px; box-shadow: none; height: 30px; width: 100%; margin: 0px; box-sizing: border-box; box-shadow: none; padding-left: 10px; padding-right: 18px; outline: none; cursor: pointer; text-overflow: ellipsis; } .dropdown-panel { position: absolute; top: 32px; width:100%; padding: 0px; display: none; border-left: 1px solid steelblue; border-bottom: 1px solid steelblue; border-right: 1px solid steelblue; } .dropdown-panel .dropdown-search{ display: block; width: 100%; height: 30px; } .dropdown-search span{ background: url(../static/chosen-sprite.png) no-repeat 100% -20px,linear-gradient(#eee 1%,#fff 15%); display: block; height: 25px; width: 20px; position: absolute; right: 0px; top: 3px; } .dropdown-search input[class='iptsearch']{ border: none; border-color: transparent; background: transparent; border-radius: 0px; box-shadow: none; height: 30px; width: 100%; margin: 0px; box-sizing: border-box; box-shadow: none; padding-left: 10px; outline: none; } .dropdown-panel .dd-select{ display: block; width:100%; position: relative; height: auto; margin: 0px; padding:0px !important; box-sizing: border-box; list-style-type: none; text-align: left; max-height: 300px; overflow-y: scroll; overflow-x: hidden; } .dd-item{ display: block; height: 30px; line-height: 30px; padding-left: 5px; border-bottom: 1px solid steelblue; font-size: 13px; z-index: 8; overflow: hidden; } .dd-item .dd-v{ width: 0px; height: 0px; display: none; } .dd-item .dd-k{ z-index: 8; padding-left: 15px; } .dd-item:first-child{ border-top: 1px solid steelblue; } .dd-item:last-child{ border-bottom: none; } .dd-select .on{ background-color: steelblue\9; } .dd-item:hover::before,.dd-item:hover before{ content:'\2714'; position: absolute; left: 0px; color: #79a979 z-index: 9; font-size: 16px; padding-right: 3px; padding-left: 2px; background-color: #fff; } .dd-select .on::before,.dd-select .on before{ content:'\2714'; position: absolute; left: 0px; color: green; z-index: 9; font-size: 16px; padding-right: 3px; padding-left: 2px; background-color: #fff; } .dd-item:hover{ cursor: pointer; background-color: #ccc; } .dropdown-container .dropdown-opt{ width:100%; text-align: center; position: absolute; left: -1px; bottom: -30px; padding: 0px; border-left: 1px solid steelblue; border-bottom: 1px solid steelblue; border-right: 1px solid steelblue; box-sizing: content-box; } .dropdown-container .dd-btn{ color: #333; height: 28px !important; display: inline-block; background-color: #e6e6e6; border-color: #adadad; user-select: none; background-image: none; border: 1px solid transparent; border-radius: 4px; margin: 0px 15px; } .dropdown-container .dd-btn:hover{ color:#fff; background-color: steelblue; } /* scrollbar */ .dropdown-container ::-webkit-scrollbar { width: 5px; height: 10px; } .dropdown-container ::-webkit-scrollbar-track, .dropdown-container ::-webkit-scrollbar-thumb { border-radius: 999px; border: 5px solid transparent; } .dropdown-container ::-webkit-scrollbar-track { box-shadow: 1px 1px 5px rgba(0,0,0,.2) inset; } .dropdown-container ::-webkit-scrollbar-thumb { min-height: 20px; background-clip: content-box; box-shadow: 0 0 0 5px rgba(0,0,0,.2) inset; } .dropdown-container ::-webkit-scrollbar-corner { background: transparent; }
3.Jquery插件
;(function ($,window,document,undefined) { var _pluginName="jqDropdown"; var defaults = { items:[] }; var parm=[]; //es5 filter hack if (!Array.prototype.filter){ Array.prototype.filter = function(fun){ var len = this.length; if (typeof fun != "function") throw new TypeError(); var res = new Array(); var _arg = arguments[1]; for (var i = 0; i < len; i++){ if (i in this){ var val = this[i]; if (fun.call(_arg, val, i, this)) res.push(val); } } return res; }; } if(!Array.prototype.indexOf) { Array.prototype.indexOf = function (elem, startFrom) { var startFrom = startFrom || 0; if (startFrom > this.length) return -1; for (var i = 0; i < this.length; i++) { if (this[i] == elem && startFrom <= i) { return i; } else if (this[i] == elem && startFrom > i) { return -1; } } return -1; } } var init = function (o,opts,okFn,cancelFn) { var _panel=$(o); var _ul=_panel.find(".dd-select:eq(0)"); function createDropItems(items,p){ p=p||[]; _ul.empty(); var _curretVal=$(".iptdiplay").val(); for(var i=0,len=items.length;i-1?'dd-item on':'dd-item')); } _panel.find('.dd-item').click(function(e){ var $this=$(this); var _k_= $this.find('.dd-k:eq(0)').text() var _v_= $this.find('.dd-v:eq(0)').text(); if($this.hasClass('on')){ parm=parm.filter(function(t){ return t!=_v_; }); $this.removeClass('on'); }else{ parm.push(_v_); $this.addClass('on'); } var disArr=[]; for(var i=0,len=items.length;i -1) disArr.push(_d.name); } $(".iptdiplay").val(disArr.join('|')); }); }; //init to build dropdown items createDropItems(opts.items||[]); function toggleDrop(){ $(".dropdown-panel").slideToggle(); }; //search $(".iptsearch").bind("input propertychange",function(e){ if(!e) return; var _sk=e.currentTarget.value; var _items=opts.items||[]; createDropItems(_items.filter(function(d){ return d.name.indexOf(_sk)>-1; }),parm); }); //Toggle dropdown $(".dropdown-display").click(function(){ toggleDrop(); }); //OK button event $(".dropdown-opt button.ok").click(function(){ toggleDrop(); okFn&&okFn.apply(this,[parm]); }); //Cancel button event $(".dropdown-opt button.cancel").click(function(){ toggleDrop(); cancelFn&&cancelFn.call(this); }); }; $.fn[_pluginName] = function (options,okFn,cancelFn) { var options = $.extend(defaults, options); return this.each(function () { init(this,options,okFn,cancelFn); }); } })(jQuery,window,document);
4.頁(yè)面調(diào)用示例
5.輸出
6.不足
頁(yè)面樣式在不支持css3瀏覽器中顯示有問題 后期需要改進(jìn)
下拉數(shù)據(jù)為一次性渲染 如有需要 可設(shè)置滾動(dòng)加載
下拉框滾動(dòng)條的美化未兼容所有瀏覽器
搜索時(shí)檢索數(shù)據(jù)直接源自頁(yè)面數(shù)據(jù) 所有需要添加延遲處理 獲取服務(wù)端數(shù)據(jù)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。