@SubstanceD 的解决方案是我找到的最好的解决方案,但它存在一些可访问性问题,因此我对其进行了重新设计以使用单选按钮字段集。
html:
<div id="colourlist">red</div>
<fieldset id="colours">
<label for="red">red<input type="radio" name="foo" id="red"/></label>
<label for="blue">blue <input type="radio" name="foo" id="blue"/> </label>
<label for="green">green<input type="radio" name="foo" id="green"/></label>
<label for="orange">orange<input type="radio" name="foo" id="orange"/></label>
</fieldset>
<div id="preview"></div>
CSS:
body{
margin: 0;
padding: 50px;
}
input {
opacity:0;
}
label {
display:block;
height:20px;
}
#colourlist{
position: absolute;
border: 1px solid #B5B0AC;
width: 200px;
height: 21px;
background: url('http://www.thermaxindia.com/images/dropdown_arrow.JPG') 180px 0 no-repeat;
}
label:hover{
background-color: #3399FF;
}
#colours{
display: none;
position: relative;
top: 22px;
left: 0;
width: 200px;
position: relative;
border: 1px solid #B5B0AC;
overflow-y: scroll;
height:60px;
}
#preview{
display: none;
position: relative;
background-color: #fff;
border: 1px solid #ccc;
padding: 10px;
width: 250px;
height: 30px;
}
js:
$("#colours label").on('mouseenter', function(){
var O = $(this).offset();
var CO = $("#colours").offset();
$("#preview").css("top", O.top-150).show();
$("#preview").css("left", CO.left+160);
var text = $(this).text();
$("#preview").css("background-color", text);
});
$("#colours input").on('click', function(){
var text = $(this).attr("id");
$("#colourlist").text(text);
$("#colours").hide();
$("#preview").hide();
});
$("#colourlist").on('click', function(e){
e.stopPropagation();
$("#colours").show();
});
$("body").on('click',function(e){
$("#colours").hide();
});
这是小提琴:http: //jsfiddle.net/MikeGodin/CJbeF/109/