热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

js+css实现select的美化效果_javascript技巧

这篇文章主要为大家详细介绍了js+css实现select的美化效果,如何针对select进行美化,感兴趣的小伙伴们可以参考一下
先给大家看一看美化之后的效果图:

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:

//select美化
var pSelectListIndex = 0;

$(function () {
  initDivSelect();
});

//初始化select美化插件
function initDivSelect() {
  $(".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('

'); $("body").append('

'); } 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('

' + text + '

'); 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.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='" + select.val() + "']"); if (matchItem.length > 0) { matchItem.addClass("p-select-selected"); updateText(matchItem); } } initSelect(); select.change(function () { initSelect(); }); }); // $(".p-select-target").each }

2、如何使用:

第1步、引用CSS和JS:






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





二、滚动条美化版:

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:

//select美化
var pSelectListIndex = 0;

$(function () {
  initDivSelect();
});

//初始化select美化插件
function initDivSelect() {
  $(".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('

'); $("body").append('

'); } 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); var scrollbar; var scrollbarPosTop; var scrollbarPos; var scrollbarScrollHeight; var scrollbarUp; var scrollbarDown; var itemHeight; var itemCount; var itemsHeight; var scrollFlag = false; 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('

' + text + '

'); 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(); if (scrollbar) scrollbar.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()); } else { pText.width(list.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); updateScrollbarPos(); 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); updateScrollbarPos(); return false; } if (e.keyCode == 13) { var selectedItem = list.find(".p-select-selected"); var value = selectedItem.attr("value"); select.val(value); list.hide(); if (scrollbar) scrollbar.hide(); select.change(); } }); itemHeight = list.find(".p-select-item:first").height(); itemCount = list.find(".p-select-item").length; itemsHeight = itemHeight * itemCount; if (itemsHeight > list.height()) { $("body").append('

'); } scrollbar = $(".p-select-list-scrollbar-" + pSelectListIndex); scrollbarPosTop = scrollbar.find(".p-select-scrollbar-pos").find("p:first"); scrollbarPos = scrollbar.find(".p-select-scrollbar-pos").find("p:last"); scrollbarScrollHeight = scrollbarPos.parent().height() - scrollbarPos.height(); scrollbarUp = scrollbar.find(".p-select-scrollbar-up"); scrollbarDown = scrollbar.find(".p-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); } } pSelect.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", pSelect.offset().top + pSelect.height() + 1); list.css("left", pSelect.offset().left); var listOffsetTop = list.offset().top; if ($(window).scrollTop() + $(window).height() list.height())) { list.width(pSelect.width()); } else { list.width(pSelect.width() - scrollbar.width()); } } scrollbar.find(".p-select-scrollbar-pos").find("p:first").height(0); scrollbar.css("left", pSelect.offset().left + list.width() + 1); scrollbar.css("top", pSelect.offset().top + pSelect.height() + 1); if ($(window).scrollTop() + $(window).height() 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(".p-select-selected").removeClass("p-select-selected"); var matchItem = list.find(".p-select-item[value='" + select.val() + "']"); if (matchItem.length > 0) { matchItem.addClass("p-select-selected"); updateText(matchItem); } } initSelect(); select.change(function () { initSelect(); }); }); // $(".p-select-target").each }

效果图:

以上就是本文的全部内容,希望对大家学习Javascript程序设计有所帮助。

推荐阅读
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
  • Java实战之电影在线观看系统的实现
    本文介绍了Java实战之电影在线观看系统的实现过程。首先对项目进行了简述,然后展示了系统的效果图。接着介绍了系统的核心代码,包括后台用户管理控制器、电影管理控制器和前台电影控制器。最后对项目的环境配置和使用的技术进行了说明,包括JSP、Spring、SpringMVC、MyBatis、html、css、JavaScript、JQuery、Ajax、layui和maven等。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • HTML5网页模板怎么加百度统计?
    本文介绍了如何在HTML5网页模板中加入百度统计,并对模板文件、css样式表、js插件库等内容进行了说明。同时还解答了关于HTML5网页模板的使用方法、表单提交、域名和空间的问题,并介绍了如何使用Visual Studio 2010创建HTML5模板。此外,还提到了使用Jquery编写美好的HTML5前端框架模板的方法,以及制作企业HTML5网站模板和支持HTML5的CMS。 ... [详细]
  • Android实战——jsoup实现网络爬虫,糗事百科项目的起步
    本文介绍了Android实战中使用jsoup实现网络爬虫的方法,以糗事百科项目为例。对于初学者来说,数据源的缺乏是做项目的最大烦恼之一。本文讲述了如何使用网络爬虫获取数据,并以糗事百科作为练手项目。同时,提到了使用jsoup需要结合前端基础知识,以及如果学过JS的话可以更轻松地使用该框架。 ... [详细]
  • 从零基础到精通的前台学习路线
    随着互联网的发展,前台开发工程师成为市场上非常抢手的人才。本文介绍了从零基础到精通前台开发的学习路线,包括学习HTML、CSS、JavaScript等基础知识和常用工具的使用。通过循序渐进的学习,可以掌握前台开发的基本技能,并有能力找到一份月薪8000以上的工作。 ... [详细]
  • 本文介绍了在满足特定条件时如何在输入字段中使用默认值的方法和相应的代码。当输入字段填充100或更多的金额时,使用50作为默认值;当输入字段填充有-20或更多(负数)时,使用-10作为默认值。文章还提供了相关的JavaScript和Jquery代码,用于动态地根据条件使用默认值。 ... [详细]
  • 本文介绍了如何在Jquery中通过元素的样式值获取元素,并将其赋值给一个变量。提供了5种解决方案供参考。 ... [详细]
  • 本文介绍了使用jQuery实现图片预加载和等比例缩放的方法,同时提供了演示和相关代码。该方法可以重置图片的宽度和高度,并使图片在水平和垂直方向上居中显示。 ... [详细]
  • 本文介绍了DataTables插件的官方网站以及其基本特点和使用方法,包括分页处理、数据过滤、数据排序、数据类型检测、列宽度自动适应、CSS定制样式、隐藏列等功能。同时还介绍了其易用性、可扩展性和灵活性,以及国际化和动态创建表格的功能。此外,还提供了参数初始化和延迟加载的示例代码。 ... [详细]
  • 获取ul中第一个li元素的五种方法和多个ul中第一个li元素的四种方法
    本文介绍了获取ul中第一个li元素的五种方法和多个ul中第一个li元素的四种方法,包括使用jQuery的选择器和遍历方法。通过这些方法,可以方便地获取到所需的元素,并进行相应的操作。 ... [详细]
  • 如何压缩网站页面以减少页面加载时间
    本文介绍了影响网站打开时间的两个因素,即网页加载速度和网站页面大小。重点讲解了如何通过压缩网站页面来减少页面加载时间。具体包括图片压缩、Javascript压缩、CSS压缩和HTML压缩等方法,并推荐了相应的压缩工具。此外,还提到了一款Google Chrome插件——网页加载速度分析工具Speed Tracer。 ... [详细]
  • mui框架offcanvas侧滑超出部分隐藏无法滚动如何解决
    web前端|js教程off-canvas,部分,超出web前端-js教程mui框架中off-canvas侧滑的一个缺点就是无法出现滚动条,因为它主要用途是设置类似于qq界面的那种格 ... [详细]
  • angular.element使用方法及总结
    2019独角兽企业重金招聘Python工程师标准在线查询:http:each.sinaapp.comangularapielement.html使用方法 ... [详细]
author-avatar
00zhhl_513
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有