Hỗ trợ:
TEXT
PC window, Smart Phone
Mình không nói nhiều bây giờ share code đây:
JAVASCRIPT
//SET thisSelect={}; SelectCreate={}; optionArray={}; iAr={}; function createSelect(getID){ thisSelect[getID]=$('select.js_select#'+getID); thisSelect[getID].wrap('<div class="select ID_'+getID+'" style="width: '+thisSelect[getID].outerWidth()+'px"></div>'); SelectCreate[getID]=$('.select.ID_'+getID); if(thisSelect[getID].attr('v')=='' || !thisSelect[getID].attr('v')){ optionArray[getID]=''; for(iAr[getID]=0;iAr[getID]<thisSelect[getID].find('option').length;iAr[getID]++){ optionArray[getID]+='<div class="optionArray" title="'+thisSelect[getID].find('option:eq('+iAr[getID]+')').text()+'" value="'+thisSelect[getID].find('option:eq('+iAr[getID]+')').val()+'">'+thisSelect[getID].find('option:eq('+iAr[getID]+')').text()+'</div>'; } SelectCreate[getID].append('<input type="checkbox" id="checkbox_select['+getID+']"><label for="checkbox_select['+getID+']"><div class="selectOption"><div class="selectThis"></div><div class="selectEvent"><i class="fa fa-caret-down"></i><i class="fa fa-caret-up"></i></div><div class="selectList"></div></div></label>'); SelectCreate[getID].find('.selectOption .selectThis').text(thisSelect[getID].find('option:checked').html()).attr('title',SelectCreate[getID].find('option:checked').html()); SelectCreate[getID].find('.selectOption .selectList').html(optionArray[getID]); SelectCreate[getID].find('.selectOption .selectList .optionArray').click(function(){ SelectCreate[getID].find('.selectThis').text($(this).text()).attr('title',$(this).text()); thisSelect[getID].val($(this).attr('value')).change(); }); SelectCreate[getID].find('input[type="checkbox"]').change(function(){ SelectCreate[getID].find('.optionArray').removeClass('focused'); countArray=SelectCreate[getID].find('.optionArray').length; ThisFocus=0; if((SelectCreate[getID].find('input[type="checkbox"]:checked').val() ? true : false)==true){ $(document).unbind().on("keydown",function(){ if(event.keyCode==38/* UP */){ if(ThisFocus==0){ ThisFocus=countArray-1; }else{ ThisFocus=ThisFocus-1; } } if(event.keyCode==40/* DOWN */){ if(ThisFocus==countArray-1){ ThisFocus=0; }else{ ThisFocus=ThisFocus+1; } } if(event.keyCode==13/* ENTER */){ SelectCreate[getID].find('.selectThis').text(SelectCreate[getID].find('.optionArray:eq('+ThisFocus+')').text()).attr('title',SelectCreate[getID].find('.optionArray:eq('+ThisFocus+')').text()); thisSelect[getID].val(SelectCreate[getID].find('.optionArray:eq('+ThisFocus+')').attr('value')).change(); SelectCreate[getID].find('label').click(); } SelectCreate[getID].find('.optionArray').removeClass('focused'); SelectCreate[getID].find('.optionArray:eq('+ThisFocus+')').addClass('focused'); SelectCreate[getID].find('.selectList').scrollTop(SelectCreate[getID].find('.optionArray:eq('+ThisFocus+')').outerHeight()*ThisFocus); }) }else{ $(document).unbind(); } }); }else if(thisSelect[getID].attr('v')=='manager'){ optionArray[getID]=''; for(iAr[getID]=0;iAr[getID]<thisSelect[getID].find('option').length;iAr[getID]++){ optionArray[getID]+='<div class="optionArray_2" title="'+thisSelect[getID].find('option:eq('+iAr[getID]+')').text()+'" value="'+thisSelect[getID].find('option:eq('+iAr[getID]+')').val()+'">'+thisSelect[getID].find('option:eq('+iAr[getID]+')').text()+'</div>'; } SelectCreate[getID].append('<input type="checkbox" id="checkbox_select['+getID+']"><label for="checkbox_select['+getID+']"><div class="selectOption_2"><div class="selectThis_2"></div><div class="selectEvent_2"><i class="fa fa-caret-down"></i><i class="fa fa-caret-up"></i></div><div class="selectList_2"></div></div></label>'); SelectCreate[getID].find('.selectOption_2 .selectThis_2').text(thisSelect[getID].find('option:checked').html()).attr('title',SelectCreate[getID].find('option:checked').html()); SelectCreate[getID].find('.selectOption_2 .selectList_2').html(optionArray[getID]); LocationID[getID]=thisSelect[getID].find('option:checked').val(); SelectCreate[getID].find('.selectOption_2 .selectList_2 .optionArray_2').click(function(){ SelectCreate[getID].find('.selectThis_2').text($(this).text()).attr('title',$(this).text()); thisSelect[getID].val($(this).attr('value')).change(); }) SelectCreate[getID].find('input[type="checkbox"]').change(function(){ countArray=SelectCreate[getID].find('.optionArray_2').length; ThisFocus=0; if((SelectCreate[getID].find('input[type="checkbox"]:checked').val() ? true : false)==true){ $(document).unbind().on("keydown",function(){ if(event.keyCode==38/* UP */){ if(ThisFocus==0){ ThisFocus=countArray-1; }else{ ThisFocus=ThisFocus-1; } } if(event.keyCode==40/* DOWN */){ if(ThisFocus==countArray-1){ ThisFocus=0; }else{ ThisFocus=ThisFocus+1; } } if(event.keyCode==13/* ENTER */){ SelectCreate[getID].find('.selectThis_2').text(SelectCreate[getID].find('.optionArray_2:eq('+ThisFocus+')').text()).attr('title',SelectCreate[getID].find('.optionArray_2:eq('+ThisFocus+')').text()); thisSelect[getID].val(SelectCreate[getID].find('.optionArray_2:eq('+ThisFocus+')').attr('value')).change(); SelectCreate[getID].find('input[type="checkbox"]').change(); } SelectCreate[getID].find('.optionArray_2').removeClass('focused'); SelectCreate[getID].find('.optionArray_2:eq('+ThisFocus+')').addClass('focused'); SelectCreate[getID].find('.selectList_2').scrollTop(SelectCreate[getID].find('.optionArray_2:eq('+ThisFocus+')').outerHeight()*ThisFocus); }) }else{ $(document).unbind(); } }); } SelectCreate[getID].attr('v',thisSelect[getID].attr('v')); } $(function(){ activeSelect=$('select.js_select'); for(i=0;i<activeSelect.length;i++){ createSelect($('select.js_select:eq('+i+')').attr('id')); } })
CSS:
CSS
.select{ display: inline-block; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; -o-user-select: none; user-select: none; } .select[v]{ max-width: calc(100% - 80px); } .select:not([v]){ max-width: 100%; } .select select,.select input[type="checkbox"]{ display: none; } .select input[type="checkbox"]:checked+label .selectList{ display: block!important; } .selectOption{ position: relative; } .selectThis { display: inline-block; padding: 6px; width: calc(100% - 25px); background: #2b2b2b; box-sizing: border-box; text-shadow: 0px -1px 0px #000; box-shadow: 0px 0px 1px 0px #666 inset, 0px 1px 0px 0px #555 inset; color: #fff; border: 1px solid #000; border-right: 0px; overflow: hidden; vertical-align: top; white-space: nowrap; text-overflow: ellipsis; } .selectEvent{ display: inline-block; padding: 6px 0px; width: 25px; background-image: linear-gradient(to bottom, #2d2d2d,#101010); box-sizing: border-box; text-align: center; text-shadow: 0px -1px 0px #000; box-shadow: 0px 0px 1px 0px #666 inset, 0px 1px 0px 0px #555 inset; color: #fff; border: 1px solid #000; cursor: pointer; } .select label .selectThis:active+.selectEvent, .select label .selectEvent:active{ background-image: linear-gradient(to bottom, #161616,#101010); box-shadow: 0px 1px 1px 0px #000 inset; } .select input[type="checkbox"]:checked+label .selectEvent .fa-caret-down, .select input[type="checkbox"]+label .selectEvent .fa-caret-up{ display: none!important; } .select input[type="checkbox"]+label .selectEvent .fa-caret-down, .select input[type="checkbox"]:checked+label .selectEvent .fa-caret-up{ display: inline-block!important; } .selectList{ display: none; position: absolute; width: 100%; max-height: 124px; overflow: auto; background: #2b2b2b; border: 1px solid #000; border-top: 0px; box-sizing: border-box; z-index: 999; transition: 100ms opacity ease-out; word-wrap: break-word; white-space: nowrap; } .optionArray{ padding: 6px; border-bottom: 1px solid #262626; color: #999; cursor: pointer; transition: 100ms background ease-out, 100ms color ease-out; overflow: hidden; text-overflow: ellipsis; } .optionArray:last-child{ border-bottom: 0px; } .optionArray:hover, .optionArray.focused{ background: #2196F3; color: #fff; } .select input[type="checkbox"]:checked+label .selectList_2{ display: block!important; } .selectOption_2{ position: relative; } .selectThis_2{ display: inline-block; padding: 3px 6px; width: calc(100% - 25px); background-image: linear-gradient(to bottom, #b9b9b9,#e2e2e2); box-sizing: border-box; box-shadow: 0px 0px 1px 0px #fff inset, 0px 1px 0px 0px #c8c8c8 inset; color: #000; border: 1px solid #000; border-right: 0px; overflow: hidden; vertical-align: top; white-space: nowrap; text-overflow: ellipsis; } .selectEvent_2{ display: inline-block; padding: 3px 0px; width: 25px; background-image: linear-gradient(to bottom, #2d2d2d,#101010); box-sizing: border-box; text-align: center; text-shadow: 0px -1px 0px #000; box-shadow: 0px 0px 1px 0px #666 inset, 0px 1px 0px 0px #555 inset; color: #fff; border: 1px solid #000; cursor: pointer; } .select label .selectThis_2:active+.selectEvent_2, .select label .selectEvent_2:active{ background-image: linear-gradient(to bottom, #161616,#101010); box-shadow: 0px 1px 1px 0px #000 inset; } .select input[type="checkbox"]:checked+label .selectEvent_2 .fa-caret-down, .select input[type="checkbox"]+label .selectEvent_2 .fa-caret-up{ display: none!important; } .select input[type="checkbox"]+label .selectEvent_2 .fa-caret-down, .select input[type="checkbox"]:checked+label .selectEvent_2 .fa-caret-up{ display: inline-block!important; } .selectList_2{ display: none; position: absolute; width: 100%; max-height: 75px; overflow: auto; background: #2b2b2b; border: 1px solid #000; border-top: 0px; box-sizing: border-box; z-index: 1000; transition: 100ms opacity ease-out; word-wrap: break-word; white-space: nowrap; } .optionArray_2{ box-sizing: border-box; padding: 3px 6px; border-bottom: 1px solid #262626; color: #999; cursor: pointer; transition: 100ms background ease-out, 100ms color ease-out; overflow: hidden; text-overflow: ellipsis; } .optionArray_2:last-child{ border-bottom: 0px; } .optionArray_2:hover, .optionArray_2.focused{ background: #2196F3; color: #fff; }
Cách sử dụng:
* Style Default;
HTML5
<select id="server" class="js_select"> <option value="0">Chọn 0</option> <option value="1">Chọn 1</option> <option value="2">Chọn 2</option> </select>>
Style Black White;
HTML5
<select id="server2" class="js_select" v="manager"> <option value="0">Chọn 0</option> <option value="1">Chọn 1</option> <option value="2">Chọn 2</option> </select>>
Lưu ý mỗi select đều phải có 1 ID nhé!
Và mỗi select ID phải khác nhau.
![[OFF]](/assets/images/off.gif)
![[IMAGE]](http://i.imgur.com/9jAOFV1h.png)