Dropdown select with images
I wanted to create a dropdown select which has images instead of text as the options. I've done some Googling and searching here on Stack Overflow, and the answer generally given is to use the jQuery combobox.
The problem with this solution, it seems to me, is that you have to provide text. It looks like the images are just icons that accompany that text on the left. Correct me if I'm wrong, but this solution wouldn't cover what I'm trying to do-- which is completely replace the text with images.
Some background on what I'm trying to do-- I'm trying to create a dropdown for users to select line thickness on an online painting/doodling app. The images would be lines of different thickness, kind of like mspaint.
Solution 1:
You don't even need javascript to do this!
I hope this got you intrigued so here it goes. First, the html structure:
<div id="image-dropdown">
<input type="radio" id="line1" name="line-style" value="1" checked="checked" />
<label for="line1"></label>
<input type="radio" id="line2" name="line-style" value="2" />
<label for="line2"></label>
...
</div>
Whaaat? Radio buttons? Correct. We'll style them to look like a dropdown list with images, because that's what you're after! The trick is in knowing that when labels are correctly linked to inputs (that "for" attribute and target element id), the input will implicitly become active; click on a label = click on a radio button. Here comes comes slightly abbreviated css with comments inline:
#image-dropdown {
/*style the "box" in its minimzed state*/
border:1px solid black; width:200px; height:50px; overflow:hidden;
/*animate the dropdown collapsing*/
transition: height 0.1s;
}
#image-dropdown:hover {
/*when expanded, the dropdown will get native means of scrolling*/
height:200px; overflow-y:scroll;
/*animate the dropdown expanding*/
transition: height 0.5s;
}
#image-dropdown input {
/*hide the nasty default radio buttons!*/
position:absolute;top:0;left:0;opacity:0;
}
#image-dropdown label {
/*style the labels to look like dropdown options*/
display:none; margin:2px; height:46px; opacity:0.2;
background:url("http://www.google.com/images/srpr/logo3w.png") 50% 50%;}
#image-dropdown:hover label{
/*this is how labels render in the "expanded" state.
we want to see only the selected radio button in the collapsed menu,
and all of them when expanded*/
display:block;
}
#image-dropdown input:checked + label {
/*tricky! labels immediately following a checked radio button
(with our markup they are semantically related) should be fully opaque
and visible even in the collapsed menu*/
opacity:1 !important; display:block;
}
Full example here: http://jsfiddle.net/NDCSR/1/
NB1: you'll probably need to use it with position:absolute inside a container with position:relative +high z-index.
NB2: when adding more backgrounds for individual line styles, consider having the selectors based on the "for" attribute of the label like so:
label[for=linestyle2] {background-image:url(...);}
Solution 2:
Check this example .. everything has been done easily http://jsfiddle.net/GHzfD/
EDIT: Updated/working as of 2013, July 02: jsfiddle.net/GHzfD/357
#webmenu{
width:340px;
}
<select name="webmenu" id="webmenu">
<option value="calendar" title="http://www.abe.co.nz/edit/image_cache/Hamach_300x60c0.JPG"></option>
<option value="shopping_cart" title="http://www.nationaldirectory.com.au/sites/itchnomore/thumbs/screenshot2013-01-23at12.05.50pm_300_60.png"></option>
<option value="cd" title="http://www.mitenterpriseforum.co.uk/wp-content/uploads/2013/01/MIT_EF_logo_300x60.jpg"></option>
<option value="email" selected="selected" title="http://annualreport.tacomaartmuseum.org/sites/default/files/L_AnnualReport_300x60.png"></option>
<option value="faq" title="http://fleetfootmarketing.com/wp-content/uploads/2013/01/Wichita-Apartment-Video-Tours-CTA60-300x50.png"></option>
<option value="games" title="http://krishnapatrika.com/images/300x50/pellipandiri300-50.gif"></option>
</select>
$("body select").msDropDown();