如何使用 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>
广告