JS自定义select下拉框

 之前写项目一直只改变了select框的样式,option一直都是用的原生的,在网上查了一下发现option的样式除了颜色跟字体之外基本上都不可以修改,所以我用div ul li 模拟了select的功能

效果如图:

 HTML:

<div class="select-box">
    <div class="select-head" onclick="selectblock(this)">
        <span>请选择喜欢的食物</span>
        <span class="select-icon"></span>
    </div>
    <ul class="select-body">
        <label><input type="radio" name="food" hidden><li>汉堡</li></label>
        <label><input type="radio" name="food" hidden><li>奶茶</li></label>
        <label><input type="radio" name="food" hidden><li>火锅</li></label>
    </ul>
</div>

CSS:

.select-head{
  user-select:none;
  padding: 12px 12px;;
  width: 150px;
  border-radius: 3px;
  border: 1px solid #2260ff;
  position: relative;
  cursor: pointer;
}

.select-icon{
  display: block;
  height: 15px;
  width: 20px;
  background: url("./向下箭头.png") no-repeat center;
  background-size: cover;
  position: absolute;
  top: 35%;
  right: 0.5rem;
}

.icon-Y{
  transform: scaleY(-1);
}

.select-body{
  display: none;
  margin-top: 8px;
  background-color: #fff;
  border: 2px solid #b5bfd9;
  border-radius: 8px;
  flex-direction: column;
  padding: 0.5rem;
  box-shadow: 0 3px 7px 0 #c8cfe1;
  width: 157px;
}

.select-body li{
  user-select:none;
  cursor: pointer;
  color: #4f5d7e;
  display: block;
  padding: 0.625rem;
  border-radius: 8px;
  margin-top: 0.25rem;
  width: 100%;
}

.select-body label{
  display: flex;
}

.select-body input:checked + li{
  background-color: #e9efff;
  color: #2260ff;
}

.select-body li:hover{
  background-color: #e9efff;
}

JS:

function selectblock(event) {
  let selectBody = event.parentNode.querySelectorAll(".select-body")[0];
  let selectIcon = event.getElementsByClassName("select-icon")[0];
  let selectHead = event.parentNode.querySelectorAll(".select-head")[0];
  if (selectBody.style.display == "block") {
    selectBody.style.display = "none";
    selectIcon.classList.remove("icon-Y");
  } else {
    selectBody.style.display = "block";
    selectIcon.classList.add("icon-Y");
    //点击空白区域下拉框消失
    document.addEventListener("click", (event) => {
      let tDom = event.target;
      if(selectBody !== tDom && selectHead !== tDom && !selectHead.contains(tDom)){
        selectBody.style.display = "none";
        selectIcon.classList.remove("icon-Y");
      }
    });
  }
}

(function () {
  cks = document.querySelectorAll(".select-body>label>li");
  for (let i = 0; i < cks.length; i++) {
    cks[i].onclick = function () {
      cks[i].parentNode.parentNode.style.display = "none";
      cks[i].parentNode.parentNode.parentNode.getElementsByClassName("select-head")[0].getElementsByTagName("span")[0].innerHTML = cks[i].innerHTML;
      cks[i].parentNode.parentNode.parentNode.getElementsByClassName("select-head")[0].getElementsByTagName("span")[1].classList.remove("icon-Y");
    };
    let ckd = cks[i].parentNode.getElementsByTagName("input")[0];
    if (ckd.checked == true) {
      cks[i].parentNode.parentNode.parentNode.getElementsByClassName("select-head")[0].getElementsByTagName("span")[0].innerHTML = cks[i].innerHTML;
    }
  }
})();

使用的时候引入一下css,js然后改一下name就可以了

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值