如何使用 CSS 和 JavaScript 建立自定義選擇框?
選擇框允許您建立一個下拉選單,供使用者從列表中選擇特定值。例如,當您希望使用者從學位列表中選擇一個學位,或從熱門運動列表中選擇喜歡的運動等時,通常會出現這些選擇框。讓我們看看如何使用 CSS 和 JavaScript 建立自定義選擇框。選擇框的顏色、外觀等可以輕鬆更改。讓我們看看如何操作 -
為選擇框建立一個容器
首先,為選擇框建立一個容器 div。選項值 0 是選擇框值從頂部開始的位置 -
<div class="customSelect" style="width:200px;">
<select>
<option value="0">Select Animal:</option>
<option value="Giraffe">Giraffe</option>
<option value="Lion">Lion</option>
<option value="Cow">Cow</option>
<option value="Dog">Dog</option>
<option value="Tiger">Tiger</option>
</select>
</div>
定位選擇框
選擇框使用 position 屬性和 relative 值進行定位
.customSelect {
position: relative;
font-family: Arial, Helvetica, sans-serif;
}
最初隱藏選擇值
網頁載入時,選擇選項處於隱藏狀態。要隱藏它們,將 display 屬性設定為 none -
.customSelect select {
display: none;
}
選擇框箭頭
在選擇框上,一個向下指向的箭頭可以讓使用者瞭解這是一個下拉選單。要定位該箭頭,將 position 屬性設定為 absolute 值。使用 content 屬性設定形狀。為了將其定位到精確的位置,需要考慮 top 和 right 屬性 -
.select-selected:after {
position: absolute;
content: "";
top: 14px;
right: 10px;
width: 0;
height: 0;
border: 6px solid transparent;
border-color: #fff transparent transparent transparent;
}
所選值的背景顏色
background-color 屬性用於設定所選值的背景顏色 -
.select-selected {
background-color: rgb(78, 11, 122);
}
示例
要使用 CSS 和 JavaScript 建立自定義選擇框,程式碼如下 -
<!DOCTYPE html>
<html>
<head>
<style>
.customSelect {
position: relative;
font-family: Arial, Helvetica, sans-serif;
}
.customSelect select {
display: none;
}
.select-selected {
background-color: rgb(78, 11, 122);
}
.select-selected:after {
position: absolute;
content: "";
top: 14px;
right: 10px;
width: 0;
height: 0;
border: 6px solid transparent;
border-color: #fff transparent transparent transparent;
}
.select-selected.select-arrow-active:after {
border-color: transparent transparent #fff transparent;
top: 7px;
}
.select-items div, .select-selected {
color: #ffffff;
padding: 8px 16px;
border: 1px solid transparent;
cursor: pointer;
user-select: none;
}
.select-items {
position: absolute;
background-color: rgb(138, 29, 148);
top: 100%;
left: 0;
right: 0;
z-index: 99;
}
.select-hide {
display: none;
}
.select-items div:hover, .sameSelected {
background-color: rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<h1>Custom Select Example</h1>
<div class="customSelect" style="width:200px;">
<select>
<option value="0">Select Animal:</option>
<option value="Giraffe">Giraffe</option>
<option value="Lion">Lion</option>
<option value="Cow">Cow</option>
<option value="Dog">Dog</option>
<option value="Tiger">Tiger</option>
</select>
</div>
<script>
var customSelectEle, i, j, selElmnt, divEle, divEleSelected, c;
customSelectEle = document.querySelector(".customSelect");
selElmnt = customSelectEle.getElementsByTagName("select")[0];
divEle = document.createElement("DIV");
divEle.setAttribute("class", "select-selected");
divEle.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
customSelectEle.appendChild(divEle);
divEleSelected = document.createElement("DIV");
divEleSelected.setAttribute("class", "select-items select-hide");
Array.from(selElmnt).forEach((item, index) => {
c = document.createElement("DIV");
c.innerHTML = selElmnt.options[index].innerHTML;
c.addEventListener("click", function(e) {
var y, i, k, selEleParent, selEleSibling;
selEleParent = this.parentNode.parentNode.getElementsByTagName( "select" )[0];
selEleSibling = this.parentNode.previousSibling;
for (i = 0; i < selEleParent.length; i++) {
if (selEleParent.options[i].innerHTML == this.innerHTML) {
selEleParent.selectedIndex = i;
selEleSibling.innerHTML = this.innerHTML;
y = this.parentNode.getElementsByClassName("sameSelected");
for (k = 0; k < y.length; k++) {
y[k].removeAttribute("class");
}
this.setAttribute("class", "sameSelected");
break;
}
}
selEleSibling.click();
});
divEleSelected.appendChild(c);
});
customSelectEle.appendChild(divEleSelected);
divEle.addEventListener("click", function(e) {
e.stopPropagation();
closeSelect(this);
this.nextSibling.classList.toggle("select-hide");
this.classList.toggle("select-arrow-active");
});
function closeSelect(elmnt) {
var customSelectEle,
y,
i,
arrNo = [];
customSelectEle = document.getElementsByClassName("select-items");
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 < customSelectEle.length; i++) {
if (arrNo.indexOf(i)) {
customSelectEle[i].classList.add("select-hide");
}
}
}
document.addEventListener("click", closeSelect);
</script>
</body>
</html>
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP