HTML中select控件美化的示例分析-创新互联

这篇文章主要为大家展示了“HTML中select控件美化的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“HTML中select控件美化的示例分析”这篇文章吧。

网站制作、成都网站建设介绍好的网站是理念、设计和技术的结合。创新互联建站拥有的网站设计理念、多方位的设计风格、经验丰富的设计团队。提供PC端+手机端网站建设,用营销思维进行网站设计、采用先进技术开源代码、注重用户体验与SEO基础,将技术与创意整合到网站之中,以契合客户的方式做到创意性的视觉化效果。

CSS:

.div-select
{
  border: solid 1px #999;
  height: 40px;
  line-height: 40px;
  cursor: default;
}
.div-select-text
{
  float: left;
  background-color: #fff;
  height: 100%;
  word-break: keep-all;
  overflow: hidden;
  cursor: default;
}
  .div-select-text > div
  {
    padding: 3px;
    line-height: 34px;
  }
.div-select-arrow
{
  background-color: #fff;
  float: right;
  width: 40px;
  height: 100%;
  color: #999;
  cursor: default;
}
  .div-select-arrow > div
  {
    border: solid 1px #999;
    margin: 2px;
    height: 34px;
    background-color: #f2f2f2;
    text-align: center;
    line-height: 34px;
    font-size: 22px;
  }
.div-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;
}
  .div-select-list .div-select-item:nth-child(2n+1)
  {
    background-color: #fff;
  }
.div-select-item
{
  height: 50px;
  line-height: 50px;
  padding-left: 3px;
  padding-right: 3px;
  background-color: #f2f2f2;
  word-break: keep-all;
  overflow: hidden;
  cursor: default;
}
.div-select-item-hover
{
  background-color: #3399ff!important;
}
.div-select-selected
{
  background-color: #3399ff !important;
}

JS:

//2015年2月8日
//select美化
var divSelectListIndex = 0;
$(function () {
  initDivSelect();
});
//初始化select美化插件
function initDivSelect() {
  $(".div-select-target").each(function () {
    divSelectListIndex++;
    var select = $(this);
    if (select.css("display") == "none") {
      return;
    }
    else {
      select.css("display", "none")
    }
    if (select.next("div").find(".div-select-list").length == 0) {
      select.after('
');       $("body").append('
');     }     var div = select.next("div");     var divText = div.find(".div-select-text");     var divSelect = div.find(".div-select");     var divArrow = div.find(".div-select-arrow");     var list = $(".div-select-list-" + divSelectListIndex);     function updateText(item) {       divText.find("div").html(item.html());     }     select.find("option").each(function () {       var option = $(this);       var text = option.html();       var value = option.attr("value");       list.append('' + text + '
');       list.find(".div-select-item:last").click(function () {         var item = $(this);         var value = item.attr("value");         select.val(value);         select.change();         list.find(".div-select-selected").removeClass("div-select-selected");         item.addClass("div-select-selected");         updateText(item);         list.hide();       });       list.find(".div-select-item:last").mouseenter(function () {         var item = $(this);         var selectedMark = list.find(".div-select-selected");         selectedMark.removeClass("div-select-selected");         selectedMark.addClass("div-select-selected-mark");         list.find(".div-select-item-hover").removeClass("div-select-item-hover");         item.addClass("div-select-item-hover");         updateText(item);       });     });     list.mouseleave(function () {       var selectedMark = list.find(".div-select-selected-mark");       if (list.find(".div-select-selected").length == 0) {         selectedMark.addClass("div-select-selected");         updateText(selectedMark);       }       selectedMark.removeClass("div-select-selected-mark");       list.find(".div-select-item-hover").removeClass("div-select-item-hover");     });     if (select.attr("width")) {       divSelect.width(select.attr("width") - 2);       divText.width(divSelect.width() - divArrow.width());       if (select.attr("width") > list.width()) {         list.width(divSelect.width());       }     }     div.keydown(function (e) {       list.find(".div-select-selected-mark").removeClass("div-select-selected-mark");       list.find(".div-select-item-hover").addClass("div-select-selected");       list.find(".div-select-item-hover").removeClass("div-select-item-hover");       if (e.keyCode == 40) {         var currentSelected = list.find(".div-select-selected");         var nextSelected = currentSelected.next(".div-select-item");         if (nextSelected.length == 0) {           nextSelected = list.find(".div-select-item:first");           nextSelected.addClass("div-select-selected");           currentSelected.removeClass("div-select-selected");           list.scrollTop(0);         } else {           nextSelected.addClass("div-select-selected");           currentSelected.removeClass("div-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(".div-select-selected");         var nextSelected = currentSelected.prev(".div-select-item");         if (nextSelected.length == 0) {           nextSelected = list.find(".div-select-item:last");           nextSelected.addClass("div-select-selected");           currentSelected.removeClass("div-select-selected");           list.scrollTop(list.find(".div-select-item").length * nextSelected.height());         }         else {           nextSelected.addClass("div-select-selected");           currentSelected.removeClass("div-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(".div-select-selected");         var value = selectedItem.attr("value");         select.val(value);         list.hide();         select.change();       }     });     divSelect.click(function () {       $("a").bind("click", function () {         $("a").unbind("click");         list.hide();       });       if (list.css("display") == "none") {         list.show();       }       else {         list.hide();       }       list.css("top", divSelect.offset().top + divSelect.height() + 1);       list.css("left", divSelect.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() < divSelect.width()) {         list.width(divSelect.width());       }       var currentSelected = list.find(".div-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(".div-select-selected").removeClass("div-select-selected");       var matchItem = list.find(".div-select-item[value='" + select.val() + "']");       if (matchItem.length > 0) {         matchItem.addClass("div-select-selected");         updateText(matchItem);       }     }     initSelect();     select.change(function () {       initSelect();     });   }); // $(".div-select-target").each }

如何使用:

第1步、引用CSS和JS:



第2步、给select控件加上class="div-select-target" width="200",其中class="div-select-target"是必须的,width="200"是可选的。完整HTML代码如下:






  
    中国
    美国
    法国
    英国
    俄罗斯
    德国
    韩国
    日本
    印度
    巴西
    意大利
    这个国家的名称很长很长很长很长很长很长很长很长
    瑞士
    越南
    缅甸
    泰国
    加拿大
    南非
    澳大利亚
    新西兰
    挪威
    巴勒斯坦
    以色列
    新加坡
    马来西亚
    波兰
    国家27
    国家28
    国家29
    国家30
    国家31
    国家32
    国家33
    国家34
    国家35
    国家36
    国家37
    国家38
  


  
    中国
    美国
    法国
    英国
    俄罗斯
    德国
    韩国
    日本
  

效果图:

HTML中select控件美化的示例分析

滚动条美化版:

CSS:

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

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

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

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

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

.div-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: 微软雅黑,雅黑;
}

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

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

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

.div-select-selected
{
  background-color: #3399ff !important;
}
.div-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;
}
.div-select-scrollbar-up
{
  border-bottom: solid 1px #fff;
  height: 39px;
  font-size: 22px;
  line-height: 39px;
  color: #999;
  background-color: #cdcdcd;
  text-align: center;
}
.div-select-scrollbar-pos
{
  height: 220px;
}
  .div-select-scrollbar-pos > div:last-child
  {
    width: 40px;
    height: 20px;
    background-color: #cdcdcd;
  }
.div-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:

//2015年2月8日
//select美化
var divSelectListIndex = 0;

$(function () {
  initDivSelect();
});
//初始化select美化插件
function initDivSelect() {
  $(".div-select-target").each(function () {
    divSelectListIndex++;
    var select = $(this);
    if (select.css("display") == "none") {
      return;
    }
    else {
      select.css("display", "none")
    }
    if (select.next("div").find(".div-select-list").length == 0) {
      select.after('
');       $("body").append('');     }     var div = select.next("div");     var divText = div.find(".div-select-text");     var divSelect = div.find(".div-select");     var divArrow = div.find(".div-select-arrow");     var list = $(".div-select-list-" + divSelectListIndex);     var scrollbar;     var scrollbarPosTop;     var scrollbarPos;     var scrollbarScrollHeight;     var scrollbarUp;     var scrollbarDown;     var itemHeight;     var itemCount;     var itemsHeight;     var scrollFlag = false;     function updateText(item) {       divText.find("div").html(item.html());     }     select.find("option").each(function () {       var option = $(this);       var text = option.html();       var value = option.attr("value");       list.append('' + text + '');       list.find(".div-select-item:last").click(function () {         var item = $(this);         var value = item.attr("value");         select.val(value);         select.change();         list.find(".div-select-selected").removeClass("div-select-selected");         item.addClass("div-select-selected");         updateText(item);         list.hide();         if (scrollbar) scrollbar.hide();       });       list.find(".div-select-item:last").mouseenter(function () {         var item = $(this);         var selectedMark = list.find(".div-select-selected");         selectedMark.removeClass("div-select-selected");         selectedMark.addClass("div-select-selected-mark");         list.find(".div-select-item-hover").removeClass("div-select-item-hover");         item.addClass("div-select-item-hover");         updateText(item);       });     });     list.mouseleave(function () {       var selectedMark = list.find(".div-select-selected-mark");       if (list.find(".div-select-selected").length == 0) {         selectedMark.addClass("div-select-selected");         updateText(selectedMark);       }       selectedMark.removeClass("div-select-selected-mark");       list.find(".div-select-item-hover").removeClass("div-select-item-hover");     });     if (select.attr("width")) {       divSelect.width(select.attr("width") - 2);       divText.width(divSelect.width() - divArrow.width());     }     else {       divText.width(list.width());     }     div.keydown(function (e) {       list.find(".div-select-selected-mark").removeClass("div-select-selected-mark");       list.find(".div-select-item-hover").addClass("div-select-selected");       list.find(".div-select-item-hover").removeClass("div-select-item-hover");       if (e.keyCode == 40) {         var currentSelected = list.find(".div-select-selected");         var nextSelected = currentSelected.next(".div-select-item");         if (nextSelected.length == 0) {           nextSelected = list.find(".div-select-item:first");           nextSelected.addClass("div-select-selected");           currentSelected.removeClass("div-select-selected");           list.scrollTop(0);         } else {           nextSelected.addClass("div-select-selected");           currentSelected.removeClass("div-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);         updateScrollbarPos();         return false;       }       if (e.keyCode == 38) {         var currentSelected = list.find(".div-select-selected");         var nextSelected = currentSelected.prev(".div-select-item");         if (nextSelected.length == 0) {           nextSelected = list.find(".div-select-item:last");           nextSelected.addClass("div-select-selected");           currentSelected.removeClass("div-select-selected");           list.scrollTop(list.find(".div-select-item").length * nextSelected.height());         }         else {           nextSelected.addClass("div-select-selected");           currentSelected.removeClass("div-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);         updateScrollbarPos();         return false;       }       if (e.keyCode == 13) {         var selectedItem = list.find(".div-select-selected");         var value = selectedItem.attr("value");         select.val(value);         list.hide();         if (scrollbar) scrollbar.hide();         select.change();       }     });     itemHeight = list.find(".div-select-item:first").height();     itemCount = list.find(".div-select-item").length;     itemsHeight = itemHeight * itemCount;     if (itemsHeight > list.height()) {       $("body").append('
∨');     }     scrollbar = $(".div-select-list-scrollbar-" + divSelectListIndex);     scrollbarPosTop = scrollbar.find(".div-select-scrollbar-pos").find("div:first");     scrollbarPos = scrollbar.find(".div-select-scrollbar-pos").find("div:last");     scrollbarScrollHeight = scrollbarPos.parent().height() - scrollbarPos.height();     scrollbarUp = scrollbar.find(".div-select-scrollbar-up");     scrollbarDown = scrollbar.find(".div-select-scrollbar-down");     scrollbar.click(function () {       return false;     });     scrollbarUp.click(function () {       list.scrollTop(list.scrollTop() - list.height());       updateScrollbarPos();     });     scrollbarDown.click(function () {       list.scrollTop(list.scrollTop() + list.height());       updateScrollbarPos();     });     scrollbar.mousedown(function () {       scrollFlag = true;     });     scrollbar.mouseup(function () {       scrollFlag = false;     });     scrollbar.mousemove(function (e) {       if (scrollFlag) {         var pos = e.pageY - scrollbar.offset().top - 50;         if (pos <= scrollbarScrollHeight) {           scrollbarPosTop.height(pos);           list.scrollTop(scrollbarPosTop.height() / scrollbarScrollHeight * (itemsHeight - list.height()));         }       }     });     function updateScrollbarPos() {       scrollbarPosTop.height(scrollbarScrollHeight * list.scrollTop() * 1.0 / (itemsHeight - list.height()));       if (list.scrollTop() + list.height() == itemsHeight) {         scrollbarPosTop.height(scrollbarScrollHeight);       }     }     divSelect.click(function () {       $("a").bind("click", function () {         $("a").unbind("click");         list.hide();         scrollbar.hide();       });       if (list.css("display") == "none") {         list.show();         scrollbar.show();       }       else {         list.hide();         scrollbar.hide();       }       list.css("top", divSelect.offset().top + divSelect.height() + 1);       list.css("left", divSelect.offset().left);       var listOffsetTop = list.offset().top;       if ($(window).scrollTop() + $(window).height() < list.offset().top + list.height() + 2) {         list.css("top", $(window).scrollTop() + $(window).height() - list.height() - 2);       }       if (list.width() < divSelect.width()) {         if (!(itemsHeight > list.height())) {           list.width(divSelect.width());         }         else {           list.width(divSelect.width() - scrollbar.width());         }       }       scrollbar.find(".div-select-scrollbar-pos").find("div:first").height(0);       scrollbar.css("left", divSelect.offset().left + list.width() + 1);       scrollbar.css("top", divSelect.offset().top + divSelect.height() + 1);       if ($(window).scrollTop() + $(window).height() < listOffsetTop + list.height() + 2) {         scrollbar.css("top", $(window).scrollTop() + $(window).height() - list.height() - 2);       }       var currentSelected = list.find(".div-select-selected");       if (currentSelected.position().top > list.height() - currentSelected.height()) {         list.scrollTop(currentSelected.position().top - currentSelected.height() * 2);       }       updateScrollbarPos();       return false;     });     $("html,body").bind("click", function () {       list.hide();       scrollbar.hide();     });     list.click(function () {       return false;     });     function initSelect() {       list.find(".div-select-selected").removeClass("div-select-selected");       var matchItem = list.find(".div-select-item[value='" + select.val() + "']");       if (matchItem.length > 0) {         matchItem.addClass("div-select-selected");         updateText(matchItem);       }     }     initSelect();     select.change(function () {       initSelect();     });   }); // $(".div-select-target").each }

效果图:

HTML中select控件美化的示例分析

以上是“HTML中select控件美化的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联网站建设公司行业资讯频道!

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站名称:HTML中select控件美化的示例分析-创新互联
URL分享:http://csdahua.cn/article/dpcdhp.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流

其他资讯