Home > Article > Web Front-end > How to Make HTML Select Box Options Appear on Hover?
How to Display HTML Select Box Options on Hover
The challenge you presented involves creating a select box where the options are hidden until the user hovers over it. Here's a detailed solution:
Implementation:
The code provided utilizes jQuery's hover() event to toggle the visibility of the list elements. The unselected CSS class is used to style list items that are not currently displayed.
<code class="html"><select name="size"> <option value="small">Small</option> <option value="medium">Medium</option> <option value="large">Large</option> </select></code>
<code class="css">select { opacity: 0.5; } ul { width: 8em; line-height: 2em; } li { cursor: pointer; display: list-item; width: 100%; height: 2em; border: 1px solid #ccc; border-top-width: 0; text-indent: 1em; background-color: #f90; } li:first-child { border-top-width: 1px; } li.unselected { display: none; background-color: #fff; } ul#selectUl:hover li.unselected { background-color: #fff; } ul#selectUl:hover li, ul#selectUl:hover li.unselected { display: list-item; } ul#selectUl:hover li { background-color: #fc0; } ul#selectUl li:hover, ul#selectUl li.unselected:hover { background-color: #f90; }</code>
<code class="js">$('#selectUl li:not(":first")').addClass('unselected'); $('#selectUl').hover( function () { $(this).find('li').click(function () { $('.unselected').removeClass('unselected'); $(this).siblings('li').addClass('unselected'); var index = $(this).index(); $('select option:selected').removeAttr('selected'); $('select[name=size]') .find('option:eq(' + index + ')') .attr('selected', true); }); }, function () {} );</code>
How It Works:
The above is the detailed content of How to Make HTML Select Box Options Appear on Hover?. For more information, please follow other related articles on the PHP Chinese website!