0

0

HTML的select控件美化功能

怪我咯

怪我咯

发布时间:2017-04-30 10:53:56

|

3406人浏览过

|

来源于php中文网

原创

本文主要介绍了html的select控件美化以及js实现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:

//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('

立即学习前端免费学习笔记(深入)”;

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

如何使用:

第1步、引用CSS和JS:



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





效果图:

ShopWind网店系统
ShopWind网店系统

ShopWind网店系统是国内最专业的网店程序之一,采用ASP语言设计开发,速度快、性能好、安全性高。ShopWind网店购物系统提供性化的后台管理界面,标准的网上商店管理模式和强大的网店软件后台管理功能。ShopWind网店系统提供了灵活强大的模板机制,内置多套免费精美模板,同时可在后台任意更换,让您即刻快速建立不同的网店外观。同时您可以对网模板自定义设计,建立个性化网店形象。ShopWind网

下载

滚动条美化版:

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:

//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('

立即学习前端免费学习笔记(深入)”;

'); $("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.offset().top + list.height() + 2) { list.css("top", $(window).scrollTop() + $(window).height() - list.height() - 2); } if (list.width() < pSelect.width()) { if (!(itemsHeight > 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() < listOffsetTop + list.height() + 2) { scrollbar.css("top", $(window).scrollTop() + $(window).height() - list.height() - 2); } var currentSelected = list.find(".p-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(".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 }

效果图:

相关文章

HTML速学教程(入门课程)
HTML速学教程(入门课程)

HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

2

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.31

Golang人工智能合集
Golang人工智能合集

本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

1

2026.01.31

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

76

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

73

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

67

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

19

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Bootstrap 5教程
Bootstrap 5教程

共46课时 | 3.1万人学习

AngularJS教程
AngularJS教程

共24课时 | 3.2万人学习

CSS教程
CSS教程

共754课时 | 26.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号