gpt4 book ai didi

javascript - 在 jQuery 中重新排列列表元素

转载 作者:太空宇宙 更新时间:2023-11-03 20:32:11 25 4
gpt4 key购买 nike

我正在制作一个 ul li 元素函数,类似于 select 输入,用户可以从中为框选择颜色。

代码如下:

$(document).ready(function() {

$('.color-picker ul.color-list li.active .text').after('<span class="pull-right glyphicon glyphicon-menu-down"></span>');

$('.color-picker ul.color-list li').click(function() {


if ($(this).hasClass('active')) {

console.log(2);
$('.color-picker ul.color-list li').show();
} else {
console.log(1);

var color = $(this).find('.box').css('backgroundColor');
$('.color-picker .color-box').css({
'background-color': color
});

$('.color-picker ul.color-list li.active').removeClass('active');
$('.color-picker ul.color-list li').hide();

$(this).addClass('active');
$('.color-picker ul.color-list li.active').show();
$('.color-picker ul.color-list li .glyphicon').remove();
$('.color-picker ul.color-list li.active .text').after('<span class="pull-right glyphicon glyphicon-menu-down"></span>');

}

});

});
.color-picker {
display: flex;
margin: 20px;
border: 1px solid #d9d9d9;
padding: 10px;
width: 350px;
}
.color-picker .color-box {
display: inline-block;
background-color: black;
height: 50px;
width: 50px;
}
.color-picker ul.color-list {
display: inline-block;
margin: 0;
position: absolute;
}
.color-picker ul.color-list li {
list-style: none;
border: 1px solid #d9d9d9;
padding: 10px;
cursor: pointer;
width: 200px;
background-color: white;
}
.color-picker ul.color-list li.active {
display: block;
}
.color-picker ul.color-list li {
display: none;
}
.color-picker ul.color-list li .box {
content: "";
display: inline-block;
height: 10px;
width: 30px;
margin-right: 10px;
}
.color-picker ul.color-list li.red .box {
background-color: #c71212;
}
.color-picker ul.color-list li.blue .box {
background-color: #0d89d1;
}
.color-picker ul.color-list li.green .box {
background-color: #18c771;
}
.color-picker .as-select .select-default {
border: 1px solid #d9d9d9;
padding: 10px 20px;
width: 200px;
margin-left: 40px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="color-picker">

<div class="color-box">
</div>

<div class="as-select">
<ul class="color-list">
<li class="red active">
<span class="box"></span>
<span class="text">Red</span>
</li>
<li class="blue">
<span class="box"></span>
<span class="text">Blue</span>
</li>
<li class="green">
<span class="box"></span>
<span class="text">Green</span>
</li>
</ul>
</div>
</div>

jsFiddle

如您所见,事件元素保留在同一位置。

(选择蓝色并再次单击下拉菜单)

我可以通过它们使所选元素成为列表的第一个子元素吗?

最佳答案

利用 Flexbox 那么您根本不需要打乱元素。 CSS 可以直观地为您“移动”它们。

 .color-picker ul.color-list {
display: inline-flex;
margin: 0;
flex-direction: column;
}

.color-picker ul.color-list li {
list-style: none;
border: 1px solid #d9d9d9;
padding: 10px;
cursor: pointer;
order:2;
width: 200px;
}

.color-picker ul.color-list li.active {
display: block;
order:1;
}

JSfiddle Demo

关于javascript - 在 jQuery 中重新排列列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39615743/

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