0

0

基于jquery实现下拉框美化特效_jquery

php中文网

php中文网

发布时间:2016-05-16 15:16:43

|

1337人浏览过

|

来源于php中文网

原创

平常我们用的原生select下拉框,大部分样式没办法修改,导致在不同的浏览器里面会跟设计图的风格大相径庭。所以为了能让它美化起来,就用jq模拟了一个下拉框,可以随意定义样式。原生的下拉框也保留在div里面隐藏着,方便后台开发人员对其进行操作。

效果图如下:

HTML代码如下:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
 <meta charset="UTF-8">
 <title>下拉框美化</title>
 <link href="css/style.css" rel="stylesheet"/>
 <script src="js/jquery-1.9.1.min.js"></script>
 <script src="js/simSelect.js"></script>
 <script>
  $(function(){
  //下面是调用初始化语句,class名可通用,也可以ID单独定义
  $(".select-box").simSelect();    //什么参数都不带,默认样式。建议用这个,参数都写在div上面好了,比较直观。

  $(".slt-box01").simSelect({    //所有参数如下:
   maxNum: 4,           //最大下拉个数(超过则显示滚动条),默认为5 
   width: 250,           //下拉框盒子宽度,默认为200px。为避免过多的设置宽度,尽量依照项目中最常见的宽度设定css样式。
   direction: "down",       //下拉方向,默认down,另一个是up
   disabled: false         //是否禁用,默认不禁,禁的话是true
  });

  $(".slt-box02").simSelect({    //举例:这里写参数,div上面也写参数的情况。结果是:会以div上面的为准
   maxNum: 4,           
   width: 250,           
   direction: "down"
  });

  $(".slt-box03").simSelect({    //禁用下拉框的话,有三种写法,任选。建议第二种:
   disabled: true,         //一:这里的参数写disabled:true 二:给div加class="disabled" 三:给原生select加disabled="true"
   width: 250           
  });

  $("#slt-box04").simSelect();   //ID单独定义。单个option可以禁用
 });
</script>
</head>
<body>
 <!-- wrap和table非必需,用于布局而已 --> 
 <div class="wrap">
  <table width="600">
   <tbody>
    <tr>
     <th>不带参数:</th>
     <td><div class="select-box">
      <select>
       <option>第一个选项</option>
       <option>第二个选项</option>
       <option>第三个选项</option>
       <option>第四个选项</option>
       <option>第五个选项</option>
       <option>第六个选项</option>
      </select>
     </div></td>
    </tr>
    <tr>
     <th>初始化语句写了参数:</th>
     <td><div class="slt-box01">
      <select>
       <option>第一个选项</option>
       <option>第二个选项</option>
       <option>第三个选项</option>
       <option>第四个选项</option>
       <option>第五个选项</option>
       <option>第六个选项</option>
      </select>
     </div></td>
    </tr>
    <tr>
     <th>在div上面写参数:</th>
     <td><div class="slt-box02 up" max-num="6" width="300">
      <select>
       <option>第一个选项</option>
       <option>第二个选项</option>
       <option>第三个选项</option>
       <option>第四个选项</option>
       <option>第五个选项</option>
       <option>第六个选项</option>
      </select>
     </div></td>
    </tr>
    <tr>
     <th>禁用的样式:</th>
     <td><div class="slt-box03">
      <select>
       <option>第一个选项</option>
       <option>第二个选项</option>
       <option>第三个选项</option>
       <option>第四个选项</option>
       <option>第五个选项</option>
       <option>第六个选项</option>
      </select>
     </div></td>
    </tr>
    <tr>
     <th>其中一个选项禁用:</th>
     <td><div id="slt-box04" class="up" max-num="4" width="200">
      <select>
       <option>第一个选项</option>
       <option>第二个选项超长超长超长超长长啊</option>
       <option disabled="true">第三个选项</option>
       <option>第四个选项</option>
       <option>第五个选项</option>
       <option>第六个选项</option>
      </select>
     </div></td>
    </tr>
   </tbody>
  </table>
 </div>
</body>
</html>

CSS样式如下:

@charset "utf-8";
/* 简单reset */
body, ul, li {
  margin: 0;
  padding: 0;
}
body {
  font: 14px/24px Microsoft YaHei;
  color: #333;
}
ul { list-style: none; }
a {
  color: #333;
  outline: none;
  text-decoration: none;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
  text-align: left;
}
/* 布局样式,非必须 */
.wrap {
  width: 600px;
  margin: 100px auto 0;
  padding: 20px;
  background-color: #d3f3dd;
}
.wrap table th, .wrap table td { padding: 8px 2px; }
.wrap table th {
  font-weight: normal;
  text-align: right;
}
/* 下拉框样式 必须 */
.select-style ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
.select-style select { display: none; }
.select-style {
  position: relative;
  display: inline-block;
  font-family: Microsoft YaHei;
  color: #666;
  font-size: 14px;
  text-align: left;
  vertical-align: middle;
  z-index: 50;
}
.select-style.focus { z-index: 51; }
.select-style .slt-wrap {
  display: inline-block;
  width: 200px;
  border: solid 1px #d6d6d6;
  vertical-align: middle;
}
.select-style.focus .slt-wrap { border: solid 1px #53a8df; }
.select-style .slt-title {
  position: relative;
  display: block;
  padding: 0 36px 0 5px;
  line-height: 30px;
  height: 30px;
  text-decoration: none;
  background-color: #fff;
  word-break: break-all;
  color: #666;
  overflow: hidden;
}
.select-style .slt-title .slt-text {
  display: inline-block;
  height: 30px;
 *cursor: pointer;
}
.select-style .slt-title i {
  position: absolute;
  right: 0;
  top: 0;
  display: inline-block;
  width: 30px;
  height: 30px;
  background: url(../images/ico-select.png) 0 0 no-repeat;
  *cursor: pointer;
}
.select-style.focus .slt-title i { background-position: 0 -30px; }
.select-style.disabled .slt-title i { 
  background-position: 0 -60px;
  *cursor: default; 
}
.select-style .opn-box {
  display: none;
  position: absolute;
  left: 0;
  top: 31px;
  width: 100%;
}
.select-style.up .opn-box {
  top: auto;
  bottom: 31px;
}
.select-style .opn-box .opn-list {
  position: relative;
  _width: 100%;
  max-height: 130px;
  border: 1px solid #d6d6d6;
  background: #fff;
  overflow-y: auto;
  overflow-x: hidden;
}
.select-style.focus .opn-box .opn-list { border-color: #53a8df; }
.select-style .opn-box .opn-list li {
  display: block;
  _width: 100%;
  padding-left: 5px;
  line-height: 26px;
  height: 26px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: pointer;
}
.select-style .opn-box .opn-list .selected { background: #d4edfe; }
.select-style .opn-box .opn-list li:hover {
  color: #fff;
  background: #65abda;
}
.select-style .opn-box .opn-list li.disabled {
  color: #cacaca;
  background: #f0f0f0;
  cursor: default;
}
.select-style.disabled .slt-wrap { border: 1px solid #d6d6d6; }
.select-style.disabled .slt-title {
  color: #cacaca;
  background-color: #f0f0f0;
  cursor: default;
}
.select-style.disabled .slt-title .slt-text { *cursor: default; }
/* 下拉框样式 结束 */

Jquery代码如下:

jQuery插件实现的下拉框美化特效
jQuery插件实现的下拉框美化特效

jQuery插件实现的下拉框美化特效

下载
/** 
 *  Name  : 美化下拉框 
 **/
 (function(jQuery){
   $.fn.simSelect = function (o) {
    o = $.extend({                  //设置默认参数 
      maxNum: 5,                  //最大显示5个
      width: 200,                  //默认宽200px。为避免过多的设置宽度,尽量依照项目中最常见的宽度设定css样式。
      direction: "down",              //向下拉,另一个是up  
      disabled: false                //不可用时为true
    },o || {});
    return this.each(function(){          //构造开始
      if($(this).children(".slt-wrap")){      //去重复  
        $(this).children(".slt-wrap").remove();
      };
      var $ts = $(this),
        $select = $ts.find("select").eq(0),
        wid = parseFloat($ts.attr("width")),
        num = parseFloat($ts.attr("max-num")),
        $sltWrap = $("<div class='slt-wrap'></div>").prependTo($ts),
        $sltTit = $("<a class='slt-title' hidefocus='true' href='javascript:void(0);'><span class='slt-text'></span><i></i></a>").prependTo($sltWrap),
        $sltText = $(".slt-text", $sltTit),
        $opnBox = $("<div class='opn-box'><ul class='opn-list'></ul></div>").appendTo($sltWrap),
        $opnList = $(".opn-list", $opnBox);
      $ts.addClass("select-style");                //增加一个class专门作为写css样式用
      $select.find("option").each(function(i){          //循环生成li标签  
        var text = $(this).text(),
          $li = $("<li title='"+text+"'>"+text+"</li>").appendTo($opnList);
        if(this.selected){
          $li.addClass("selected");
          $sltText.text(text).attr("title",text);  
        };
        if(this.disabled){
          $li.addClass("disabled");
          return;
        };
      });
      var $li = $("li",$opnList),
        hei = $li.height();
      if(wid){                          //设置宽度
        $ts.css("width",wid+"px");              //兼容IE6、7
        $sltWrap.css("width",wid-2+"px");                
      }else{
        $ts.css("width",o.width+"px");            //兼容IE6、7
        $sltWrap.css("width",o.width-2+"px");
      };  
      if(num){                          //设置高度
        $opnList.css("max-height", hei*num+"px");
      }
      else{
        $opnList.css("max-height", hei*o.maxNum+"px");
      };
      if(o.direction == "up"){                  //设置上、下拉方向
        $ts.addClass("up");
      };
      $li.on("click",function(){                  //li标签的点击事件,传给原生select
        var index = $opnList.find("li").index(this),
          text = $(this).text();
        if($(this).hasClass("disabled")){
          return false;
        };
        $(this).addClass("selected").siblings().removeClass("selected");
        $select.find("option").prop("selected",false).eq(index).prop("selected",true);
        $sltText.text(text).attr("title",text);
        $opnBox.hide();
        $ts.removeClass("focus");
      });
      $sltTit.on("click",function(e){               //a标签的点击下拉事件
        e.stopPropagation();                  //阻止a标签的点击冒泡    
        if($opnBox.is(":hidden")){
          $(".select-style .opn-box").hide();         
          $(".select-style").removeClass("focus");
          $opnBox.show();
          $ts.addClass("focus");
        }
        else{
          $opnBox.hide();
          $ts.removeClass("focus");
        }
      });
      $select.on("change",function(){                //原生select的点击事件,传给ul
        var index = $(this).find("option:selected").index(),
          text = $li.eq(index).text();
        $li.eq(index).addClass("selected").siblings().removeClass("selected");
        $sltText.text(text).attr("title",text);
      });
      $(document).on("click",function(e){              //点击其他地方收起下拉框
        if($opnBox.is(":visible")){
          $opnBox.hide();
          $ts.removeClass("focus");
        }  
      });
      if($select.prop("disabled") == true || o.disabled == true || $ts.hasClass("disabled")){
        $sltTit.off("click");                  //设置禁用状态
        $select.prop("disabled",true);
        $ts.addClass("disabled");
      };
    });
  };
})(jQuery);

兼容到IE7+(IE6其实也行,只是选项多于5个下面不会出现滚动条)。

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

热门AI工具

更多
DeepSeek
DeepSeek

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

豆包大模型
豆包大模型

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

通义千问
通义千问

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

腾讯元宝
腾讯元宝

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

文心一言
文心一言

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

讯飞写作
讯飞写作

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

即梦AI
即梦AI

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

ChatGPT
ChatGPT

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

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

616

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

194

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

91

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

20

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

54

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

29

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

15

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

598

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

56

2026.02.12

热门下载

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

精品课程

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

共42课时 | 6.3万人学习

HTML+CSS基础与实战
HTML+CSS基础与实战

共132课时 | 11.3万人学习

tp6+adminlte搭建通用后台
tp6+adminlte搭建通用后台

共39课时 | 5.8万人学习

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

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