用HTML+css+js写个select选择器
时间: 2023-09-22 12:05:07 浏览: 174
下面是一个简单的 HTML+CSS+JS 实现的 select 选择器示例:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Custom Select</title>
<style>
/* 隐藏原生 select */
select {
display: none;
}
/* 外层容器 */
.custom-select {
position: relative;
display: inline-block;
}
/* 下拉箭头 */
.custom-select::after {
content: "\25BC";
font-size: 12px;
color: gray;
position: absolute;
top: 50%;
right: 12px;
transform: translateY(-50%);
}
/* 显示框 */
.custom-select-selected {
display: inline-block;
padding: 6px 12px;
border: 1px solid gray;
border-radius: 4px;
background-color: #fff;
cursor: pointer;
}
/* 下拉选项容器 */
.custom-select-items {
position: absolute;
z-index: 1;
top: 100%;
left: 0;
right: 0;
background-color: #fff;
border: 1px solid gray;
border-top: none;
border-radius: 0 0 4px 4px;
overflow-y: auto;
max-height: 200px;
}
/* 下拉选项 */
.custom-select-item {
display: block;
padding: 6px 12px;
cursor: pointer;
}
/* 鼠标悬停样式 */
.custom-select-item:hover {
background-color: #f1f1f1;
}
/* 选中样式 */
.custom-select-item.selected {
background-color: #ddd;
}
</style>
</head>
<body>
<div class="custom-select">
<select id="my-select">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
<option value="option4">Option 4</option>
<option value="option5">Option 5</option>
</select>
<div class="custom-select-selected">Option 1</div>
<div class="custom-select-items">
<div class="custom-select-item" data-value="option1">Option 1</div>
<div class="custom-select-item" data-value="option2">Option 2</div>
<div class="custom-select-item" data-value="option3">Option 3</div>
<div class="custom-select-item" data-value="option4">Option 4</div>
<div class="custom-select-item" data-value="option5">Option 5</div>
</div>
</div>
<script>
var select = document.getElementById("my-select");
var selectContainer = document.querySelector(".custom-select");
var selectSelected = selectContainer.querySelector(".custom-select-selected");
var selectItemsContainer = selectContainer.querySelector(".custom-select-items");
var selectItems = selectItemsContainer.querySelectorAll(".custom-select-item");
// 初始化选中值
selectSelected.textContent = select.options[select.selectedIndex].textContent;
// 点击显示下拉选项
selectSelected.addEventListener("click", function () {
selectItemsContainer.classList.toggle("show");
});
// 点击选项
for (var i = 0; i < selectItems.length; i++) {
selectItems[i].addEventListener("click", function () {
selectSelected.textContent = this.textContent;
select.value = this.dataset.value;
for (var j = 0; j < selectItems.length; j++) {
selectItems[j].classList.remove("selected");
}
this.classList.add("selected");
selectItemsContainer.classList.remove("show");
});
}
// 点击其他地方隐藏下拉选项
window.addEventListener("click", function (e) {
if (!selectContainer.contains(e.target)) {
selectItemsContainer.classList.remove("show");
}
});
</script>
</body>
</html>
```
这个示例使用了 CSS 伪元素和 JS 事件处理来实现一个自定义的 select 选择器。在 HTML 中,我们隐藏了原生的 select 元素,使用 div 和 ul/li 元素代替。当点击显示框时,下拉选项会显示出来,并且选中的选项会更新到显示框中。当点击其他地方时,下拉选项会隐藏。你可以根据自己的需要修改样式和功能。
阅读全文