console
let box = document.querySelector('.box')
let input = document.querySelector('#input1')
let round = document.querySelector('.top .size')
let list = document.querySelector('.list')
box.addEventListener('mouseenter', function () {
list.style.display = 'block'
input.classList.add('blue')
round.style.transform = 'rotate(0deg)'
})
box.addEventListener('mouseleave', function () {
list.style.display = 'none'
input.classList.remove('blue')
round.style.transform = 'rotate(180deg)'
})
let ul = document.querySelector('.list-ul')
let optionArr = [
{
id: 'l1',
name: '全部'
},
{
id: 'l2',
name: '鱼香肉丝'
},
{
id: 'l3',
name: '宫保鸡丁'
},
{
id: 'l4',
name: '西红柿炒鸡蛋'
},
{
id: 'l5',
name: '五彩水饺'
},
{
id: 'l6',
name: '清蒸鲈鱼'
},
{
id: 'l7',
name: '红烧茄子'
},
{
id: 'l8',
name: '土豆烧牛肉'
},
{
id: 'l9',
name: '麻婆豆腐'
},
{
id: 'l10',
name: '糖醋里脊'
}
]
for (let i = 0; i < optionArr.length; i++) {
let li = document.createElement('li')
let input1 = document.createElement('input')
let label = document.createElement('label')
input1.type = 'checkbox'
input1.id = `${optionArr[i].id}`
if (i === 0) {
input1.setAttribute("class", 'all');
label.setAttribute("class", 'sp');
} else {
input1.setAttribute("class", 'check');
label.setAttribute("class", 'wz');
}
label.innerText = `${optionArr[i].name}`
label.setAttribute("for", `${optionArr[i].id}`);
li.appendChild(input1)
li.appendChild(label)
ul.appendChild(li)
}
let chooseArr = []
let cks = document.querySelectorAll('.top ul .change')
let arr1 = document.querySelector('.top ul .arr1')
let checkboxs = document.querySelectorAll('.list ul li .check')
let labels = document.querySelectorAll('.list ul li .wz')
let tag1 = cks[0]
let tag2 = cks[1]
let k = 0
for (let i = 0; i < checkboxs.length; i++) {
checkboxs[i].addEventListener('click', function () {
updateCheckStatus()
})
}
let all = document.querySelector('.list ul li .all')
let sp = document.querySelector('.list ul li .sp')
all.addEventListener('click', function () {
checkAll()
updateCheckStatus()
})
function updateCheckStatus() {
let arr = []
let isCheckAll = true
for(let i=0;i<checkboxs.length;i++) {
if(checkboxs[i].checked) {
arr.push(labels[i].innerHTML)
labels[i].classList.add('fontBlue')
}else {
isCheckAll = false
labels[i].classList.remove('fontBlue')
}
}
all.checked = isCheckAll
isCheckAll ? sp.classList.add('fontBlue') : sp.classList.remove('fontBlue')
if(arr.length > 0) {
arr1.style.display = 'block'
tag1.style.display = "block"
tag1.innerHTML = arr[0]
if(arr.length>1) {
tag2.style.display = "block"
tag2.innerHTML = arr[1]
}else {
tag2.style.display = "none"
}
}else {
arr1.style.display = 'none'
tag1.style.display = "none"
tag2.style.display = "none"
}
arr1.innerHTML = `+${arr.length}`
}
function checkAll() {
sp.classList.add('fontBlue')
if (all.checked === false) {
sp.classList.remove('fontBlue')
}
for (let i = 0; i < checkboxs.length; i++) {
checkboxs[i].checked = all?.checked
}
}
<div class="box">
<div class="top">
<input type="text" name="" id="input1">
<i class="iconfont icon-arrow-up-bold size"></i>
<ul>
<li class="change">666</li>
<li class="change">777</li>
<li class="arr1">333</li>
</ul>
</div>
<div class="list">
<ul class="list-ul">
</ul>
</div>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
li {
list-style: none;
}
.box {
width: 300px;
position: relative;
top: 50px;
left: 50px;
}
.top {
height: 45px;
display: flex;
position: relative;
}
.top input {
width: 300px;
height: 45px;
font-size: 25px;
padding: 0 20px;
border-radius: 10px;
border: 1px solid #d1ccd0;
outline: none;
}
.top .blue {
border: 2px solid #409eff;;
}
.top .size {
width: 16px;
height: 16px;
position: relative;
top: 50%;
left: -30px;
margin-top: -8px;
transform: rotate(180deg);
transition: all .5s;
color: #d1ccd0;
}
.top ul {
position: absolute;
display:flex;
font-size: 10px;
top: 50%;
transform: translateY(-50%);
left: 15px;
}
.top ul li {
background-color: #409eff;
color: #fff;
padding: 1px 10px;
margin-left: 3px;
border-radius: 5px;
display: none;
}
.list {
width: 300px;
height: 230px;
border-radius: 5px;
margin-top: 5px;
background-color:#fff;
border: 1px solid #d1ccd0;
box-shadow: 0px 0px 5px 0px #aaa;
display: none;
overflow: auto;
}
.list::-webkit-scrollbar {
display: none;
}
.list ul {
display: flex;
flex-direction: column;
margin: 8px 0;
}
.list ul li {
display: flex;
height: 27px;
align-items: center;
padding: 0 20px;
color: #65676b;
cursor: pointer;
}
.list ul li:hover {
background-color: #f5f8ff;
}
.list ul li label {
font-size: 10px;
margin-left: 15px;
display: block;
width: 100%;
}
.fontBlue {
color: #0075ff;
}