Skip to content

Instantly share code, notes, and snippets.

@cole007
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cole007/98c8dca1f7b2449f20c6 to your computer and use it in GitHub Desktop.
Save cole007/98c8dca1f7b2449f20c6 to your computer and use it in GitHub Desktop.
Custom select
// fork of library at http://adam.co/lab/jquery/customselect/
// removes width calculations
/*!
* jquery.customSelect() - v0.5.1
* http://adam.co/lab/jquery/customselect/
* 2014-03-19
*
* Copyright 2013 Adam Coulombe
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @license http://www.gnu.org/licenses/gpl.html GPL2 License
*/
(function ($) {
'use strict';
$.fn.extend({
customSelect: function (options) {
// filter out <= IE6
if (typeof document.body.style.maxHeight === 'undefined') {
return this;
}
var defaults = {
customClass: 'customSelect',
mapClass: true,
mapStyle: true
},
options = $.extend(defaults, options),
prefix = options.customClass,
changed = function ($select,customSelectSpan) {
var currentSelected = $select.find(':selected'),
customSelectSpanInner = customSelectSpan.children(':first'),
html = currentSelected.html() || '&nbsp;';
customSelectSpanInner.html(html);
if (currentSelected.attr('disabled')) {
customSelectSpan.addClass(getClass('DisabledOption'));
} else {
customSelectSpan.removeClass(getClass('DisabledOption'));
}
setTimeout(function () {
customSelectSpan.removeClass(getClass('Open'));
$(document).off('mouseup.customSelect');
}, 60);
},
getClass = function(suffix){
return prefix + suffix;
};
return this.each(function () {
var $select = $(this),
// customSelectId = $select.attr('id'),
customSelectInnerSpan = $('<span />').addClass(getClass('Inner')),
customSelectSpan = $('<span />'),
customSelectWrapper = $('<span></span>').addClass(getClass('Wrapper'));
$select.wrap(customSelectWrapper).after(customSelectSpan.append(customSelectInnerSpan));
customSelectSpan.addClass(prefix);
if (options.mapClass) {
customSelectSpan.addClass($select.attr('class'));
}
if (options.mapStyle) {
customSelectSpan.attr('style', $select.attr('style'));
}
// need to tie inputs to label clicks
/*
if (customSelectId && customSelectId.length > 0) {
$('label[for='+customSelectId+']').on('click',function() {
customSelectSpan.removeClass(getClass('Changed'));
// $('#'+customSelectId).css('border','10px solid green');
});
}
*/
$select
.addClass('hasCustomSelect')
.on('render.customSelect', function () {
changed($select,customSelectSpan);
if ($select.attr('disabled')) {
customSelectSpan.addClass(getClass('Disabled'));
} else {
customSelectSpan.removeClass(getClass('Disabled'));
}
$select.css({
'-webkit-appearance': 'menulist-button',
position: 'absolute',
opacity: 0,
fontSize: customSelectSpan.css('font-size')
});
})
.on('change.customSelect', function () {
customSelectSpan.addClass(getClass('Changed'));
changed($select,customSelectSpan);
})
.on('keyup.customSelect', function (e) {
if(!customSelectSpan.hasClass(getClass('Open'))){
$select.trigger('blur.customSelect');
$select.trigger('focus.customSelect');
}else{
if(e.which==13||e.which==27){
changed($select,customSelectSpan);
}
}
})
.on('mousedown.customSelect', function () {
customSelectSpan.removeClass(getClass('Changed'));
})
.on('mouseup.customSelect', function (e) {
if( !customSelectSpan.hasClass(getClass('Open'))){
// if FF and there are other selects open, just apply focus
if($('.'+getClass('Open')).not(customSelectSpan).length>0 && typeof InstallTrigger !== 'undefined'){
$select.trigger('focus.customSelect');
}else{
customSelectSpan.addClass(getClass('Open'));
e.stopPropagation();
$(document).one('mouseup.customSelect', function (e) {
if( e.target != $select.get(0) && $.inArray(e.target,$select.find('*').get()) < 0 ){
$select.trigger('blur.customSelect');
}else{
changed($select,customSelectSpan);
}
});
}
}
})
.on('focus.customSelect', function () {
customSelectSpan.removeClass(getClass('Changed')).addClass(getClass('Focus'));
})
.on('blur.customSelect', function () {
customSelectSpan.removeClass(getClass('Focus')+' '+getClass('Open'));
})
.on('mouseenter.customSelect', function () {
customSelectSpan.addClass(getClass('Hover'));
})
.on('mouseleave.customSelect', function () {
customSelectSpan.removeClass(getClass('Hover'));
})
.trigger('render.customSelect');
});
}
});
})(jQuery);
.customSelect {
display: block;
font-size: 11px;
border: 1px solid #b8b8b8;
background: white;
&Inner {
display: block;
padding: 5px 8px;
}
}
.hasCustomSelect {
width: 100% !important;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment