html+css+js模拟select+option下拉菜单
以下是一个简单的html+css+js模拟select+option下拉菜单的示例:
HTML代码:
<div class="custom-select-wrapper">
<div class="custom-select">
<div class="custom-select__trigger">Select an option</div>
<div class="custom-options">
<span class="custom-option selected" data-value="1">Option 1</span>
<span class="custom-option" data-value="2">Option 2</span>
<span class="custom-option" data-value="3">Option 3</span>
<span class="custom-option" data-value="4">Option 4</span>
<span class="custom-option" data-value="5">Option 5</span>
</div>
</div>
</div>
CSS代码:
.custom-select-wrapper {
position: relative;
user-select: none;
/* Prevents the select options from being selected */
}
.custom-select {
position: relative;
display: inline-block;
cursor: pointer;
}
.custom-select__trigger {
position: relative;
display: block;
width: 100%;
padding: 0.5rem;
font-size: 1rem;
font-weight: 300;
color: #fff;
line-height: 1;
background-color: #7b7b7b;
border-radius: 0;
transition: all 0.25s ease;
}
.custom-select__trigger:hover {
background-color: #666;
}
.custom-select__trigger:after {
position: absolute;
display: block;
content: "";
top: 50%;
right: 1rem;
width: 0.5rem;
height: 0.5rem;
margin-top: -0.15rem;
border-bottom: solid 0.2rem #fff;
border-right: solid 0.2rem #fff;
transform: rotate(45deg);
transition: all 0.25s ease;
transform-origin: 50% 50%;
}
.custom-select.open .custom-select__trigger:after {
margin-top: -0.35rem;
transform: rotate(-135deg);
}
.custom-options {
position: absolute;
display: none;
top: 100%;
left: 0;
right: 0;
z-index: 999;
margin: 0;
padding: 0;
list-style: none;
background-color: #fff;
border: 1px solid #7b7b7b;
border-top: none;
border-radius: 0 0 4px 4px;
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.1);
}
.custom-options:before {
position: absolute;
display: block;
content: "";
top: -0.5rem;
left: 50%;
width: 1rem;
height: 1rem;
margin-left: -0.5rem;
background-color: #fff;
border-top: 1px solid #7b7b7b;
border-left: 1px solid #7b7b7b;
transform: rotate(45deg);
z-index: -1;
}
.custom-option {
position: relative;
display: block;
padding: 0.5rem;
font-size: 1rem;
font-weight: 300;
color: #7b7b7b;
line-height: 1;
transition: all 0.25s ease;
cursor: pointer;
}
.custom-option:hover,
.custom-option:focus {
background-color: #f9f9f9;
}
.custom-option.selected {
color: #fff;
background-color: #7b7b7b;
}
.custom-select.open .custom-options {
display: block;
}
JS代码:
const customSelects = document.querySelectorAll(".custom-select");
customSelects.forEach((customSelect) => {
const customSelectTrigger = customSelect.querySelector(".custom-select__trigger");
const customOptions = customSelect.querySelector(".custom-options");
const customOptionList = customSelect.querySelectorAll(".custom-option");
customSelectTrigger.addEventListener("click", () => {
customSelect.classList.toggle("open");
});
customOptionList.forEach((customOption) => {
customOption.addEventListener("click", () => {
if (!customOption.classList.contains("selected")) {
const selectedOption = customSelect.querySelector(".custom-option.selected");
selectedOption.classList.remove("selected");
customOption.classList.add("selected");
customSelectTrigger.textContent = customOption.textContent;
}
customSelect.classList.remove("open");
});
});
window.addEventListener("click", (e) => {
if (!customSelect.contains(e.target)) {
customSelect.classList.remove("open");
}
});
});
注意:此示例只是一个简单的模拟,实际使用时需要根据具体的需求进行相应的修改和优化。
原文地址: https://www.cveoy.top/t/topic/bJC9 著作权归作者所有。请勿转载和采集!