html 手机端自定义下拉框
时间: 2023-10-17 07:14:13 浏览: 190
自定义下拉框
在手机端,可以使用HTML和CSS来自定义下拉框。以下是一个简单的示例:
HTML部分:
```html
<div class="custom-select">
<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>
</select>
</div>
```
CSS部分:
```css
/* 隐藏原生下拉框 */
.custom-select select {
display: none;
}
/* 自定义下拉框样式 */
.custom-select {
position: relative;
display: inline-block;
}
.custom-select:after {
content: "\25BC"; /* 下箭头 */
position: absolute;
top: 0;
right: 0;
padding: 10px;
background-color: #f1f1f1;
color: #555;
font-size: 20px;
}
.custom-select select:hover {
cursor: pointer;
}
/* 显示下拉框 */
.show {
display: block;
}
/* 下拉框样式 */
.custom-options {
position: absolute;
top: 100%;
left: 0;
right: 0;
z-index: 999;
max-height: 200px;
overflow-y: auto;
background-color: #f1f1f1;
}
.custom-options li {
padding: 10px;
border-bottom: 1px solid #ccc;
}
.custom-options li:last-child {
border-bottom: none;
}
.custom-options li:hover {
background-color: #ccc;
cursor: pointer;
}
```
JavaScript部分:
```js
var x, i, j, selElmnt, a, b, c;
/* 查找所有自定义下拉框 */
x = document.getElementsByClassName("custom-select");
for (i = 0; i < x.length; i++) {
selElmnt = x[i].getElementsByTagName("select")[0];
/* 创建DIV元素,用于替换原生下拉框 */
a = document.createElement("DIV");
a.setAttribute("class", "select-selected");
a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
x[i].appendChild(a);
/* 创建下拉框选项列表 */
b = document.createElement("DIV");
b.setAttribute("class", "select-options");
for (j = 0; j < selElmnt.length; j++) {
c = document.createElement("DIV");
c.innerHTML = selElmnt.options[j].innerHTML;
c.addEventListener("click", function(e) {
/* 更新选择的值 */
var y, i, k, s, h;
s = this.parentNode.parentNode.getElementsByTagName("select")[0];
h = this.parentNode.previousSibling;
for (i = 0; i < s.length; i++) {
if (s.options[i].innerHTML == this.innerHTML) {
s.selectedIndex = i;
h.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName("same-as-selected");
for (k = 0; k < y.length; k++) {
y[k].removeAttribute("class");
}
this.setAttribute("class", "same-as-selected");
break;
}
}
h.click();
});
b.appendChild(c);
}
x[i].appendChild(b);
a.addEventListener("click", function(e) {
/* 当下拉框被点击时,显示/隐藏下拉框选项列表 */
e.stopPropagation();
closeAllSelect(this);
this.nextSibling.classList.toggle("select-options-show");
this.classList.toggle("select-arrow-active");
});
}
function closeAllSelect(elmnt) {
/* 关闭所有下拉框选项列表 */
var x, y, i, arrNo = [];
x = document.getElementsByClassName("select-options");
y = document.getElementsByClassName("select-selected");
for (i = 0; i < y.length; i++) {
if (elmnt == y[i]) {
arrNo.push(i)
} else {
y[i].classList.remove("select-arrow-active");
}
}
for (i = 0; i < x.length; i++) {
if (arrNo.indexOf(i)) {
x[i].classList.remove("select-options-show");
}
}
}
document.addEventListener("click", closeAllSelect);
```
该代码将创建一个自定义下拉框,其中可以自定义下拉框的样式和选项。当用户单击下拉箭头时,将显示选项列表。当用户单击某个选项时,将更新选择的值。
阅读全文