Cách tạo: Menu chọn tùy chỉnh

Học cách sử dụng CSS và JavaScript để tạo hộp chọn tùy chỉnh.

Hộp chọn tùy chỉnh

Mặc định:

Tùy chỉnh:

Thử ngay

Tạo menu chọn tùy chỉnh

Bước 1 - Thêm HTML:

/* Bao bọc phần chọn bằng phần DIV "custom-select". Đừng quên đặt độ rộng: */
<div class="custom-select" style="width:200px;">
  <select>
    <option value="0">Chọn xe:</option>
    <option value="1">Audi</option>
    <option value="2">BMW</option>
    <option value="3">Citroen</option>
    <option value="4">Ford</option>
    <option value="5">Honda</option>
    <option value="6">Jaguar</option>
    <option value="7">Land Rover</option>
    <option value="8">Mercedes</option>
    <option value="9">Mini</option>
    <option value="10">Nissan</option>
    <option value="11">Toyota</option>
    <option value="12">Volvo</option>
  </select>
</div>

Bước 2 - Thêm CSS:

/* Hộp chứa phải được định vị tương đối: */
.custom-select {
  position: relative;
  font-family: Arial;
}
.custom-select select {
  display: none; /* Ẩn phần SELECT nguyên thủy: */
}
.select-selected {
  background-color: DodgerBlue;
}
/* Đặt樣式 cho mũi tên bên trong phần chọn: */
.select-selected:after {
  position: absolute;
  content: "";
  top: 14px;
  right: 10px;
  width: 0;
  height: 0;
  border: 6px solid transparent;
  border-color: #fff transparent transparent transparent;
}
/* Khi hộp chọn mở (hoạt động), để mũi tên chỉ lên trên: */
.select-selected.select-arrow-active:after {
  border-color: transparent transparent #fff transparent;
  top: 7px;
}
/* Đặt樣式 cho các mục (tùy chọn), bao gồm mục đã chọn: */
.select-items div,.select-selected {
  color: #ffffff;
  padding: 8px 16px;
  border: 1px solid transparent;
  border-color: transparent transparent rgba(0, 0, 0, 0.1) transparent;
  cursor: pointer;
}
/* Đặt樣式 cho các mục (tùy chọn): */
.select-items {
  position: absolute;
  background-color: DodgerBlue;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 99;
}
/* Ẩn các mục khi đóng hộp chọn: */
.select-hide {
  display: none;
}
.select-items div:hover, .same-as-selected {
  background-color: rgba(0, 0, 0, 0.1);
}

Bước 3 - Thêm JavaScript:

var x, i, j, l, ll, selElmnt, a, b, c;
/* Tìm kiếm bất kỳ phần tử nào có lớp "custom-select": */
x = document.getElementsByClassName("custom-select");
l = x.length;
for (i = 0; i < l; i++) {
  selElmnt = x[i].getElementsByTagName("select")[0];
  ll = selElmnt.length;
  /* Đối với mỗi phần tử, tạo một DIV mới, nó sẽ là mục đã chọn: */
  a = document.createElement("DIV");
  a.setAttribute("class", "select-selected");
  a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
  x[i].appendChild(a);
  /* Đối với mỗi phần tử, tạo một DIV mới, nó sẽ chứa danh sách mục: */
  b = document.createElement("DIV");
  b.setAttribute("class", "select-items select-hide");
  for (j = 1; j < ll; j++) {
    /* Đối với mỗi mục trong phần tử select ban đầu, tạo một DIV mới, nó sẽ là mục mục tiêu: */
    c = document.createElement("DIV");
    c.innerHTML = selElmnt.options[j].innerHTML;
    c.addEventListener("click", function(e) {
        /* Khi một mục được nhấp, cập nhật hộp chọn ban đầu,
        và mục đã chọn: */
        var y, i, k, s, h, sl, yl;
        s = this.parentNode.parentNode.getElementsByTagName("select")[0];
        sl = s.length;
        h = this.parentNode.previousSibling;
        for (i = 0; i < sl; i++) {
          if (s.options[i].innerHTML == this.innerHTML) {
            s.selectedIndex = i;
            h.innerHTML = this.innerHTML;
            y = this.parentNode.getElementsByClassName("same-as-selected");
            yl = y.length;
            for (k = 0; k < yl; 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) {
    /* Khi khung chọn được nhấp, hãy đóng bất kỳ khung chọn nào khác và mở/đóng khung chọn hiện tại: */
    e.stopPropagation();
    closeAllSelect(this);
    this.nextSibling.classList.toggle("select-hide");
    this.classList.toggle("select-arrow-active");
  });
}
function closeAllSelect(elmnt) {
  /* Đặt lại hàm đóng tất cả các khung chọn trong tài liệu, trừ khung chọn hiện tại: */
  var x, y, i, xl, yl, arrNo = [];
  x = document.getElementsByClassName("select-items");
  y = document.getElementsByClassName("select-selected");
  xl = x.length;
  yl = y.length;
  for (i = 0; i < yl; i++) {
    if (elmnt == y[i]) {
      arrNo.push(i)
    }
      y[i].classList.remove("select-arrow-active");
    }
  }
  for (i = 0; i < xl; i++) {
    if (arrNo.indexOf(i)) {
      x[i].classList.add("select-hide");
    }
  }
}
/* Nếu người dùng nhấp vào vị trí nào đó ngoài khung chọn, hãy đóng tất cả các khung chọn: */
document.addEventListener("click", closeAllSelect);

Thử ngay