gpt4 book ai didi

悬停时的Html选择框选项?

转载 作者:技术小花猫 更新时间:2023-10-29 12:56:26 26 4
gpt4 key购买 nike

我在 youtube 上看到过这个非常简单的选择框,其中选择选项在悬停时可见(而不是单击),如下面的屏幕截图所示。

alt text

我试图在下面的简单选择框中创建类似的效果。有人可以帮忙如何做到这一点吗?谢谢。

<select name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>

最佳答案

这是一种实现方法,但我更喜欢一种更加插件化的方法(而不是对 html ul 进行硬编码):

$('#selectUl li:not(":first")').addClass('unselected');
// Used to hide the 'unselected' elements in the ul.

$('#selectUl').hover(
function(){
// mouse-over
$(this).find('li').click(
function(){
$('.unselected').removeClass('unselected');
// removes the 'unselected' style

$(this).siblings('li').addClass('unselected');
// adds 'unselected' style to all other li elements

var index = $(this).index();
$('select option:selected').removeAttr('selected');
// deselects the previously-chosen option in the select

$('select[name=size]')
.find('option:eq(' + index + ')')
.attr('selected',true);
// assumes a 1:1 relationship between the li and option elements
});
},
function(){
// mouseout (or mouseleave, if they're different, I can't remember).
});

JS Fiddle demo .


编辑以包含一个警报,演示select 元素值的变化:JS Fiddle demo .


编辑以包含演示中使用的 css 和 html:

html

<select name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>

<ul id="selectUl">
<li>small</li>
<li>medium</li>
<li>large</li>
</ul>

CSS:

select {
opacity: 0.5;
}
ul {
width: 8em;
line-height: 2em;
}

li {
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;
}

这会导致菜单隐藏未选择 元素,直到ul 悬停,然后显示所有选项,并使用click()unselected 类名分配给未单击元素的事件;这允许被点击的元素在鼠标移出时仍然可见。

上面的 CSS 反射(reflect)了最近的编辑,使当前选择的和 :hover-ed,li 更加可见。

Latest JS Fiddle demo .


编辑 因为,嗯,我有点无聊。没有更好的事情可做,所以我做了一个简单的插件:

(function($) {
$.fn.selectUl = function(){
var $origSelect = $(this);
var newId = $(this).attr('name') + '-ul';
var numOptions = $(this).children().length;

$('<ul id="' + newId + '" class="plainSelect" />')
.insertAfter($(this));

for (var i = 0; i < numOptions; i++) {
var text = $(this).find('option').eq(i).text();
$('<li />').text(text).appendTo('#' + newId);
}

if ($(this).find('option:selected')) {
var selected = $(this).find('option:selected').index();
$('#' + newId)
.find('li')
.not(':eq(' + selected + ')')
.addClass('unselected');
}

$('#' + newId + ' li')
.hover(
function(){
$(this).click(
function(){
var newSelect = $(this).index();
$(this)
.parent()
.find('.unselected')
.removeClass('unselected');
$(this)
.parent()
.find('li')
.not(this)
.addClass('unselected');
$($origSelect)
.find('option:selected')
.removeAttr('selected');
$($origSelect)
.find('option:eq(' + newSelect + ')')
.attr('selected',true);
});
},
function(){
});
// assuming that you don't want the 'select' visible:
$(this).hide();

return $(this);
}
})(jQuery);


$('#sizes').selectUl();

JS Fiddle demo, including the 'plug-in' .

注意事项:

  • 这不是绝对定位的,所以如果您有多个 select(当然可以,并且工作正常),它确实会破坏页面-流动一点(或很多),但这可以在 CSS 中修改(我想,虽然我还没有尝试过)。
  • 它可能需要一些选项,可能允许使用 dl 元素代替当前的 ul,这将允许对可用选项进行解释.
  • 我怀疑它可以比目前优化得更多,但我不确定如何优化。可能会睡一觉,很晚了,浏览 jQuery API可能会提供一些见解(尽管如果有人看到任何明显的东西,请发表评论!或者使用 JS Fiddle 并且,为了更好的术语,将其 fork (尽管我显然很感激指向任何后续 fork /更新的链接)。
  • 我不完全确定我可以使用哪些许可选项(因为根据 Creative Commons cc-by-sa 和本网站的 Jeff Atwood,SO 上的所有内容都是 footer无论如何) ,但不管它的值(value)如何,我都非常高兴任何人都能接受以上内容,并且,好吧,用它做任何你喜欢的事情(如果它有任何用处,祝你玩得开心!)。

编辑,因为我觉得应该有一个可见的“指导”元素,并且还试图解释它是如何运作的。

给定 html:

<select name="sizes" id="sizes" class="makePlain" title="Select a size:">
<option value="xxs">XX-Small</option>
<option value="xs">X-Small</option>
<option value="s">Small</option>
<option value="m">Medium</option>
<option value="l">Large</option>
<option value="xl">X-Large</option>
<option value="xxl">XX-Large</option>
</select>

使用jQuery调用插件:

$('#sizes').selectUl();

这采用上面的选择,并创建一个带有以下标记的 ul:

<ul id="sizes-ul" class="plainSelect">
<li class="guidance">Select a size:</li>
<li>XX-Small</li>
<li class="unselected">X-Small</li>
<li class="unselected">Small</li>
<li class="unselected">Medium</li>
<li class="unselected">Large</li>
<li class="unselected">X-Large</li>
<li class="unselected">XX-Large</li>
</ul>

使用的 CSS(在演示中):

ul.plainSelect {
/* targets those 'ul' elements created by the plug-in */
border: 1px solid #000;
}
ul.plainSelect li {
/* this styles the 'selected' li 'option' element */
background-color: #f90;
display: list-item;
}
ul.plainSelect li.guidance {
/* this is the 'Select a Reason' guidance/explanation from the image in the question */
border-bottom: 1px solid #000;
padding: 0.3em;
font-weight: bold;
font-size: 1.2em;
background-color: #fff;
}

ul.plainSelect li.unselected {
/* hides the 'unselected' options, and removes the background-color for contrast */
background-color: #fff;
display: none;
}

ul.plainSelect:hover li,
ul.plainSelect:hover li.unselected {
/* ensures that the 'li' elements pop-up when the list is hovered over, */
/* and act/display like 'li' elements */
display: list-item;
}

JS Fiddle demo .

关于悬停时的Html选择框选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4599975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com