Custom select
Custom select with options
HTML
<select class="posts-select view-categories" id="cat">
<option value="hide">View all categories</option>
<option value="one">Siyasət34</option>
<option value="two">İqtisadiyyat 18</option>
<option value="three">Cəmiyyət 363</option>
<option value="three">Şou-biznes 45</option>
<option value="three">İdman 23</option>
<option value="three">Mədəniyyət 67</option>
<option value="three">Dünya 21</option>
</select>
CSS
.select-hidden {
display: none;
visibility: hidden;
padding-right: 10px;
}
.select {
cursor: pointer;
display: inline-block;
font-size: 16px;
position: absolute;
left: 0px;
top: 10px;
color: #717171;
width: 220px;
height: 40px;
}
.select-styled {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: #fcfcfc;
padding: 8px 15px;
box-shadow: inset 0 0 4px 0 rgba(0,0,0, 0.4);
}
.select-styled:after {
content: "";
width: 0;
height: 0;
border: 7px solid transparent;
border-color: #474647 transparent transparent transparent;
position: absolute;
top: 16px;
right: 10px;
}
.select-styled:hover {
background-color: #fcfcfc;
}
.select-styled:active, .select-styled.active {
background-color: #656565;
color: #fff;
}
.select-styled:active:after, .select-styled.active:after {
top: 9px;
border-color: transparent transparent #fff transparent;
}
.select-options {
display: none;
top: 100%;
right: 0;
left: 0;
z-index: 999;
margin: 0;
padding: 0;
list-style: none;
background-color: #fcfcfc;
box-shadow: 0px 0px 4px rgba(0,0,0,0.6);
}
.select-options li {
margin: 0;
padding: 8px 0;
text-indent: 15px;
}
.select-options li:hover {
color: #fff;
background: #626262;
}
.select-options li[rel="hide"] {
display: none;
}
.view-categories__amount {
float: right;
}
JavaScript
$('select').each(function(){
var $this = $(this), numberOfOptions = $(this).children('option').length;
$this.addClass('select-hidden');
$this.wrap('<div class="select"></div>');
$this.after('<div class="select-styled"></div>');
var $styledSelect = $this.next('div.select-styled');
$styledSelect.text($this.children('option').eq(0).text());
var $list = $('<ul />', {
'class': 'select-options'
}).insertAfter($styledSelect);
for (var i = 0; i < numberOfOptions; i++) {
var text = $this.children('option').eq(i).text().split(" ");
$('<li />', {
html: text[0] + "<em class='view-categories__amount'>"+ text[1] +"</em>",
rel: $this.children('option').eq(i).val()
}).appendTo($list);
}
var $listItems = $list.children('li');
$styledSelect.click(function(e) {
e.stopPropagation();
$('div.select-styled.active').each(function(){
$(this).removeClass('active').next('ul.select-options').hide();
});
$(this).toggleClass('active').next('ul.select-options').toggle();
});
$listItems.click(function(e) {
e.stopPropagation();
$styledSelect.text($(this).text()).removeClass('active');
$this.val($(this).attr('rel'));
$list.hide();
//console.log($this.val());
});
$(document).click(function() {
$styledSelect.removeClass('active');
$list.hide();
});
});