HTML 선택 컨트롤 미화 기능

怪我咯
풀어 주다: 2017-04-30 10:53:56
원래의
3113명이 탐색했습니다.

이 글에서는 주로 HTML 선택 컨트롤의 미화와 js에서 선택 기능을 구현하는 단계를 소개합니다. 매우 좋은 참조 값을 가지고 있습니다. 아래 에디터를 살펴보겠습니다

CSS:

.p-select
{
  border: solid 1px #999;
  height: 40px;
  line-height: 40px;
  cursor: default;
}
.p-select-text
{
  float: left;
  background-color: #fff;
  height: 100%;
  word-break: keep-all;
  overflow: hidden;
  cursor: default;
}
  .p-select-text > p
  {
    padding: 3px;
    line-height: 34px;
  }
.p-select-arrow
{
  background-color: #fff;
  float: right;
  width: 40px;
  height: 100%;
  color: #999;
  cursor: default;
}
  .p-select-arrow > p
  {
    border: solid 1px #999;
    margin: 2px;
    height: 34px;
    background-color: #f2f2f2;
    text-align: center;
    line-height: 34px;
    font-size: 22px;
  }
.p-select-list
{
  position: absolute;
  float: left;
  top: 100px;
  left: 100px;
  border: solid 1px #999;
  max-height: 300px;
  overflow: auto;
  background-color: #9f9;
  display: none;
  z-index: 9100;
}
  .p-select-list .p-select-item:nth-child(2n+1)
  {
    background-color: #fff;
  }
.p-select-item
{
  height: 50px;
  line-height: 50px;
  padding-left: 3px;
  padding-right: 3px;
  background-color: #f2f2f2;
  word-break: keep-all;
  overflow: hidden;
  cursor: default;
}
.p-select-item-hover
{
  background-color: #3399ff!important;
}
.p-select-selected
{
  background-color: #3399ff !important;
}
로그인 후 복사

JS:

//2015年2月8日
//select美化
var pSelectListIndex = 0;
$(function () {
  initpSelect();
});
//初始化select美化插件
function initpSelect() {
  $(".p-select-target").each(function () {
    pSelectListIndex++;
    var select = $(this);
    if (select.css("display") == "none") {
      return;
    }
    else {
      select.css("display", "none")
    }
    if (select.next("p").find(".p-select-list").length == 0) {
      select.after(&#39;<p><p class="p-select"><p class="p-select-text"><p></p></p><p class="p-select-arrow"><p>∨</p></p></p></p>&#39;);
      $("body").append(&#39;<p class="p-select-list p-select-list-&#39; + pSelectListIndex + &#39;"></p>&#39;);
    }
    var p = select.next("p");
    var pText = p.find(".p-select-text");
    var pSelect = p.find(".p-select");
    var pArrow = p.find(".p-select-arrow");
    var list = $(".p-select-list-" + pSelectListIndex);
    function updateText(item) {
      pText.find("p").html(item.html());
    }
    select.find("option").each(function () {
      var option = $(this);
      var text = option.html();
      var value = option.attr("value");
      list.append(&#39;<p class="p-select-item" value="&#39; + value + &#39;">&#39; + text + &#39;</p>&#39;);
      list.find(".p-select-item:last").click(function () {
        var item = $(this);
        var value = item.attr("value");
        select.val(value);
        select.change();
        list.find(".p-select-selected").removeClass("p-select-selected");
        item.addClass("p-select-selected");
        updateText(item);
        list.hide();
      });
      list.find(".p-select-item:last").mouseenter(function () {
        var item = $(this);
        var selectedMark = list.find(".p-select-selected");
        selectedMark.removeClass("p-select-selected");
        selectedMark.addClass("p-select-selected-mark");
        list.find(".p-select-item-hover").removeClass("p-select-item-hover");
        item.addClass("p-select-item-hover");
        updateText(item);
      });
    });
    list.mouseleave(function () {
      var selectedMark = list.find(".p-select-selected-mark");
      if (list.find(".p-select-selected").length == 0) {
        selectedMark.addClass("p-select-selected");
        updateText(selectedMark);
      }
      selectedMark.removeClass("p-select-selected-mark");
      list.find(".p-select-item-hover").removeClass("p-select-item-hover");
    });
    if (select.attr("width")) {
      pSelect.width(select.attr("width") - 2);
      pText.width(pSelect.width() - pArrow.width());
      if (select.attr("width") > list.width()) {
        list.width(pSelect.width());
      }
    }
    p.keydown(function (e) {
      list.find(".p-select-selected-mark").removeClass("p-select-selected-mark");
      list.find(".p-select-item-hover").addClass("p-select-selected");
      list.find(".p-select-item-hover").removeClass("p-select-item-hover");
      if (e.keyCode == 40) {
        var currentSelected = list.find(".p-select-selected");
        var nextSelected = currentSelected.next(".p-select-item");
        if (nextSelected.length == 0) {
          nextSelected = list.find(".p-select-item:first");
          nextSelected.addClass("p-select-selected");
          currentSelected.removeClass("p-select-selected");
          list.scrollTop(0);
        } else {
          nextSelected.addClass("p-select-selected");
          currentSelected.removeClass("p-select-selected");
          var i = 0;
          while (nextSelected.position().top < 0
            || nextSelected.position().top > list.height() - nextSelected.height()) {
            list.scrollTop(list.scrollTop() + nextSelected.height());
            if (i++ > 100) break;
          }
        }
        updateText(nextSelected);
        return false;
      }
      if (e.keyCode == 38) {
        var currentSelected = list.find(".p-select-selected");
        var nextSelected = currentSelected.prev(".p-select-item");
        if (nextSelected.length == 0) {
          nextSelected = list.find(".p-select-item:last");
          nextSelected.addClass("p-select-selected");
          currentSelected.removeClass("p-select-selected");
          list.scrollTop(list.find(".p-select-item").length * nextSelected.height());
        }
        else {
          nextSelected.addClass("p-select-selected");
          currentSelected.removeClass("p-select-selected");
          var i = 0;
          while (nextSelected.position().top < 0
            || nextSelected.position().top > list.height() - nextSelected.height()) {
            list.scrollTop(list.scrollTop() - nextSelected.height());
            if (i++ > 100) break;
          }
        }
        updateText(nextSelected);
        return false;
      }
      if (e.keyCode == 13) {
        var selectedItem = list.find(".p-select-selected");
        var value = selectedItem.attr("value");
        select.val(value);
        list.hide();
        select.change();
      }
    });
    pSelect.click(function () {
      $("a").bind("click", function () {
        $("a").unbind("click");
        list.hide();
      });
      if (list.css("display") == "none") {
        list.show();
      }
      else {
        list.hide();
      }
      list.css("top", pSelect.offset().top + pSelect.height() + 1);
      list.css("left", pSelect.offset().left);
      if ($(window).scrollTop() + $(window).height() < list.offset().top + list.height() + 2) {
        list.css("top", $(window).scrollTop() + $(window).height() - list.height() - 2);
      }
      if (list.width() < pSelect.width()) {
        list.width(pSelect.width());
      }
      var currentSelected = list.find(".p-select-selected");
      if (currentSelected.position().top > list.height() - currentSelected.height()) {
        list.scrollTop(currentSelected.position().top - currentSelected.height() * 2);
      }
      return false;
    });
    $("html,body").bind("click", function () {
      list.hide();
    });
    list.click(function () {
      return false;
    });
    function initSelect() {
      list.find(".p-select-selected").removeClass("p-select-selected");
      var matchItem = list.find(".p-select-item[value=&#39;" + select.val() + "&#39;]");
      if (matchItem.length > 0) {
        matchItem.addClass("p-select-selected");
        updateText(matchItem);
      }
    }
    initSelect();
    select.change(function () {
      initSelect();
    });
  }); // $(".p-select-target").each
}
로그인 후 복사

사용 방법:

1단계, CSS 및 JS 인용:

<link type="text/css" href="~/scripts/pSelect/pSelect.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" />
<script type="text/javascript" src="~/scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="~/scripts/pSelect/pSelect.js"></script>
로그인 후 복사

2단계, 선택 컨트롤 "200에 class="p-select-target" width=를 추가합니다. ", 여기서 class="p-select-target"은 필수이고 width="200"은 선택 사항입니다. 전체 HTML 코드는 다음과 같습니다:

<link type="text/css" href="~/scripts/pSelect/pSelect.css" rel="external nofollow" rel="external nofollow" rel="stylesheet" />
<script type="text/javascript" src="~/scripts/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="~/scripts/pSelect/pSelect.js"></script>

로그인 후 복사

렌더링:

스크롤 막대 미화 버전:

CSS:

.p-select
{
  border: solid 1px #999;
  height: 40px;
  line-height: 40px;
  cursor: default;
}

.p-select-text
{
  float: left;
  background-color: #fff;
  height: 100%;
  word-break: keep-all;
  overflow: hidden;
  cursor: default;
  font-size: 16px;
  font-family: 微软雅黑,雅黑;
}

  .p-select-text > p
  {
    padding: 3px;
    line-height: 34px;
  }

.p-select-arrow
{
  background-color: #fff;
  float: right;
  width: 40px;
  height: 100%;
  color: #999;
  cursor: default;
}

  .p-select-arrow > p
  {
    border: solid 1px #999;
    margin: 2px;
    height: 34px;
    background-color: #f2f2f2;
    text-align: center;
    line-height: 34px;
    font-size: 22px;
  }

.p-select-list
{
  position: absolute;
  float: left;
  top: 100px;
  left: 100px;
  border: solid 1px #999;
  max-height: 300px;
  overflow: hidden;
  background-color: #9f9;
  display: none;
  z-index: 9100;
  font-size: 16px;
  font-family: 微软雅黑,雅黑;
}

  .p-select-list .p-select-item:nth-child(2n+1)
  {
    background-color: #fff;
  }

.p-select-item
{
  height: 50px;
  line-height: 50px;
  padding-left: 3px;
  padding-right: 3px;
  background-color: #f2f2f2;
  word-break: keep-all;
  overflow: hidden;
  cursor: default;
}

.p-select-item-hover
{
  background-color: #3399ff!important;
}

.p-select-selected
{
  background-color: #3399ff !important;
}
.p-select-list-scrollbar
{
  position: absolute;
  float: left;
  border: solid 1px #999;
  border-left: 0;
  background-color: #e8e8ec;
  width: 40px;
  height: 300px;
  display: none;
  cursor: default;
  z-index: 9101;
}
.p-select-scrollbar-up
{
  border-bottom: solid 1px #fff;
  height: 39px;
  font-size: 22px;
  line-height: 39px;
  color: #999;
  background-color: #cdcdcd;
  text-align: center;
}
.p-select-scrollbar-pos
{
  height: 220px;
}
  .p-select-scrollbar-pos > p:last-child
  {
    width: 40px;
    height: 20px;
    background-color: #cdcdcd;
  }
.p-select-scrollbar-down
{
  border-top: solid 1px #fff;
  height: 39px;
  font-size: 22px;
  line-height: 39px;
  color: #999;
  background-color: #cdcdcd;
  text-align: center;
}
로그인 후 복사

JS:

아아앙

렌더링:

위 내용은 HTML 선택 컨트롤 미화 기능의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!