Skip to content

Instantly share code, notes, and snippets.

@amaxwell01
Created November 26, 2012 18:48
Show Gist options
  • Select an option

  • Save amaxwell01/4149875 to your computer and use it in GitHub Desktop.

Select an option

Save amaxwell01/4149875 to your computer and use it in GitHub Desktop.
A CodePen by Andrew Maxwell.
<div class="fake_background">
<!-- Editable Dropdown -->
<div id="kind">
<div id="current_selection" class="kind_selection">:nth-child</div>
<div id="kind_arrow" class="selection_arrows">
<span class="top_arrow">▲</span>
<span class="bottom_arrow">▼</span>
</div>
<ul id="kind_options" class="selection_options">
<li data-value=":nth-child" class="selected">:nth-child</li>
<li data-value=":nth-of-type">:nth-of-type</li>
<li data-value=":nth-last-child">:nth-last-child</li>
<li data-value=":nth-last-of-type">:nth-last-of-type</li>
</ul>
</div>
<select id="old">
<option value=":nth-child" selected="">:nth-child</option>
<option value=":nth-of-type">:nth-of-type</option>
<option value=":nth-last-child">:nth-last-child</option>
<option value=":nth-last-of-type">:nth-last-of-type</option>
</select>
</div>
var menu = {
init : function() {
menu.toggleDropdown();
menu.optionSelection();
$('.fake_background').on('click', function() {
menu.removeSelection();
});
},
toggleDropdown : function() {
$('#current_selection').on('click', function(event) {
event.stopPropagation();
var selectionContainer = $('#kind_options');
selectionContainer.toggleClass('visible');
});
$('#kind_arrow').on('click', function(event) {
event.stopPropagation();
var selectionContainer = $('#kind_options');
selectionContainer.toggleClass('visible');
});
},
removeSelection : function() {
var selectionContainer = $('#kind_options');
selectionContainer.removeClass('visible');
},
optionSelection : function() {
var kindOption = $('#kind_options').find('li');
kindOption.on('click', function(event) {
event.stopPropagation();
// Remove selection from all items
kindOption.removeClass('selected');
$(this).addClass('selected');
var alertMe = function( data ) {
//alert( data );
};
menu.changeSelection( alertMe );
menu.removeSelection();
});
},
changeSelection : function( callback ) {
// Get the selected value and display that and the current selection
var selectedItem = $('#kind_options').find('li.selected');
var selectedItemText = selectedItem.text();
var selectionContainer = $('#current_selection');
selectionContainer.text(selectedItemText);
if( callback ) {
callback(selectedItem.attr('data-value'));
}
}
};
menu.init();
@import "compass";
.fake_background {
background: url(http://css-tricks.com/wp-content/themes/CSS-Tricks-10/images/bg.png);
box-shadow: inset 0 0 15px rgba(0,0,0,.7);
float: left;
height: 400px;
margin: 40px;
width: 400px;
}
#kind {
background: #FFF;
border: 0;
border-radius: 5px;
float: left;
font: inherit;
font-size: 15px;
height: 20px;
margin: 60px 0 0 90px;
padding: 4px;
position: relative;
width: 200px;
}
.kind_selection {
cursor: pointer;
padding: 2px 0 0 5px;
}
.selection_arrows {
border-left: solid 1px #CCC;
border-radius: 0 5px 5px 0;
cursor: pointer;
height: 28px;
position: absolute;
right: 0;
top: 0;
width: 20px;
}
.top_arrow {
color: #333;
display: block;
float: left;
font-size: 7px;
margin: 5px 0 0 6px;
}
.bottom_arrow {
color: #333;
display: block;
float: left;
font-size: 7px;
margin: 1px 0 0 6px;
}
.selection_options {
background: #FFF;
border-radius: 5px;
box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .5);
display: none;
left: 0;
list-style-type: none;
margin: 0;
padding: 0;
position: absolute;
top: 28px;
width: 207px;
}
.selection_options li {
cursor: pointer;
margin: 0;
padding: 5px 30px;
position: relative;
&:hover {
background: blue;
color: #FFF;
}
&:first-child {
border-radius: 4px 4px 0 0;
}
&:last-child {
border-radius: 0 0 4px 4px;
}
&.selected {
background: #BADA55;
&:before {
content: '✓';
left: 10px;
position: absolute;
}
}
}
.visible {
display: block;
}
#old {
float: left;
font-size: 17px;
height: 40px;
margin: 60px 0 0 90px;
width: 210px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment