原生js实现下拉框功能(支持键盘事件)


Posted in Javascript onJanuary 13, 2017

话不多说,请看代码:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>倒计时-多种格式调用-原生js封装</title>
 <link rel="shortcut icon" href="../public/image/favicon.ico" type="images/x-icon"/>
 <link rel="icon" href="../public/image/favicon.png" type="images/png"/>
 <link rel="stylesheet" type="text/css" href="../public/style/cssreset-min.css">
 <link rel="stylesheet" type="text/css" href="../public/style/common.css">
 <style type="text/css">
 /*公共*/
 html{
 height:100%;
 }
 body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, textarea, p, blockquote, th, td {
 margin: 0;
 padding: 0
 }
 ol, ul {
 list-style: none
 }
 body{
 position: relative;
 min-height:100%;
 font-size:14px;
 font-family: Tahoma, Verdana,"Microsoft Yahei";
 color:#333;
 }
 a{
 text-decoration: none;
 color:#333;
 }
 .header{
 width: 960px;
 padding-top: 15px;
 margin: 0 auto;
 line-height: 30px;
 text-align: right;
 }
 .header a{
 margin: 0 5px;
 }
 .main{
 width:960px;
 margin: 50px auto 0;
 }
 .code{
 border:1px dashed #e2e2e2;
 padding:10px 5px;
 margin-bottom:25px;
 }
 pre {
 font-family: "Microsoft Yahei",Arial,Helvetica;
 white-space: pre-wrap; /*css-3*/ 
 white-space: -moz-pre-wrap; /*Mozilla,since1999*/ 
 white-space: -pre-wrap; /*Opera4-6*/ 
 white-space: -o-pre-wrap; /*Opera7*/ 
 word-wrap: break-word; /*InternetExplorer5.5+*/
 }
 .example{
 padding-top:40px;
 margin-bottom:90px;
 }
 .example .call{
 padding:18px 5px;
 background:#f0f5f8;
 }
 .example h2{
 padding-top:20px;
 margin-bottom:7px;
 }
 .example table {
 width:100%;
 table-layout:fixed;
 border-collapse: collapse;
 border-spacing: 0;
 border: 1px solid #cee1ee;
 border-left: 0;
 }
 .example thead {
 border-bottom: 1px solid #cee1ee;
 background-color: #e3eef8;
 }
 .example tr {
 line-height: 24px;
 font-size: 13px;
 }
 .example tr:nth-child(2n) {
 background-color: #f0f5f8;
 }
 .example tr th,.example tr td {
 border-left: 1px solid #cee1ee;
 word-break: break-all;
 word-wrap: break-word;
 padding:0 10px;
 font-weight: normal;
 }
 .example tr th {
 color: #555;
 padding-top: 2px;
 padding-bottom: 2px;
 text-align: left;
 }
 /*公共*/
 .select-container {
 width: 186px;
 margin: 80px 0;
 position: relative;
 z-index: 10000;
 }
 .select-container .select-pose {
 width: 150px;
 height: 24px;
 line-height: 24px;
 display: block;
 color: #807a62;
 cursor: pointer;
 font-style: normal;
 padding-left: 4px;
 padding-right: 30px;
 border: 1px solid #333333; /* background: url(xjt.png) no-repeat right center; */
 }
 .select-pose:before {
 content: '';
 position: absolute;
 right: 7px;
 bottom: 7px;
 width: 0;
 height: 0;
 border-width: 4px;
 border-style: solid;
 border-color: #888 transparent transparent transparent;
 transition: all 0.2s;
 -webkit-transition: all 0.2s;
 -moz-transition: all 0.2s;
 -o-transition: all 0.2s;
 -ms-transition: all 0.2s;
 transform-origin: 50% 25%;
 -ms-transform-origin: 50% 25%;
 -moz-transform-origin: 50% 25%;
 -webkit-transform-origin: 50% 25%;
 -o-transform-origin: 50% 25%;
 }
 .extended.select-pose:before {
 transform: rotate(180deg);
 -webkit-transform: rotate(180deg);
 -moz-transform: rotate(180deg);
 -o-transform: rotate(180deg);
 -ms-transform: rotate(180deg);
 }
 .select-container .select-content {
 width: 184px;
 border: 1px solid #333333;
 background-color: #ffffff;
 position: absolute;
 z-index: 20000;
 margin-top: -1px;
 display: none;
 overflow: hidden;
 }
 .select-list{
 cursor: pointer;
 height: 24px;
 line-height: 24px;
 padding-left: 10px;
 }
 </style>
 <script type="text/javascript">
 /*封装代码*/
 (function() {
 var Select = function(el, opts) {
 var self = this;
 var defaults = {
 before: '#fff',
 after:'#ccc'
 }
 opts = opts || {};
 for (var w in defaults) {
 if ("undefined" == typeof opts[w]) {
 opts[w] = defaults[w];
 }
 }
 this.params = opts;
 this.container = r(el);

 if (this.container.length > 1) {
 var x = [];
 return this.container.each(function() {
 x.push(new Select(this, opts))
 }), x
 }
 this.containers=this.container[0];
 this.pose=this.container.find(".select-pose")[0];
 this.content=this.container.find(".select-content")[0];
 this.list=this.container.find(".select-list");
 this.index=-1;
 this.init();
 }
 Select.prototype = {
 //初始化
 init: function() {
 var self = this;
 this.page();
 this.event();
 this.mover();
 },
 event:function(){
 var self = this;
 self.pose.addEventListener('click', function(e) {
 e = e || window.event;
 e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
 var oSrc = e.srcElement || e.target;
 if(oSrc.className.indexOf('extended') > -1){
 self.resetM();
 }else{
 oSrc.classList.add("extended");
 self.content.style.display = "block";
 }
 }, false);
 },
 //鼠标经过list
 mover:function(){
 var self = this;
 var list=this.list;
 for (var i = 0; i < list.length; i++) {
 !function(i){
 list[i].addEventListener('mouseover', function(e) {
 self.resetA();
this.style.background = self.params.after;
 self.index=i;
 }, false);
 }(i)
 list[i].addEventListener('click', function(e) {
 self.pose.innerHTML = this.innerHTML;
 }, false);
 }
 },
 resetA:function(){
 var self = this;
 var list=this.list;
 for (var i = 0; i < list.length; i++) {
 list[i].style.background = self.params.before;
 }
 },
 resetM:function(){
 var self = this;
 self.content.style.display = "none";
 self.pose.classList.remove("extended");
 self.resetA();
 },
 // 点击页面空白处时
 page:function(){
 var self = this;
 document.addEventListener('click', function(e) {
 self.resetM();
 }, false);
 document.addEventListener('keydown', function(e) {
 e = e || window.event;
 var keyVel=e.keyCode;
 if (keyVel == 38 || keyVel == 37) {
 e.preventDefault ? e.preventDefault() : e.returnValue = false;//取消事件默认行为
 self.index--;
 if (self.index < 0) {
 self.index = self.list.length-1;
 }
 self.resetA();
 self.list[self.index].style.backgroundColor = self.params.after;
 } else if (keyVel == 39 || keyVel == 40) {
 e.preventDefault ? e.preventDefault() : e.returnValue = false;//取消事件默认行为
 self.index++;
 if (self.index > self.list.length-1) {
 self.index = 0;
 }
 self.resetA();
 self.list[self.index].style.backgroundColor = self.params.after;
 }else if(keyVel == 13 && self.index != -1){
 e.preventDefault ? e.preventDefault() : e.returnValue = false;
 self.pose.innerHTML = self.list[self.index].innerHTML;
 self.index = -1;
 self.resetA();
 self.resetM();
 }
 }, false);
 },
 }
 var r = (function() {
 var e = function(e) {
 var a = this,
 t = 0;
 for (t = 0; t < e.length; t++) {
 a[t] = e[t];
 }
 return a.length = e.length, this
 };
 e.prototype = {
 addClass: function(e) {
 if ("undefined" == typeof e) return this;
 for (var a = e.split(" "), t = 0; t < a.length; t++)
 for (var r = 0; r < this.length; r++) this[r].classList.add(a[t]);
 return this
 },
 each: function(e) {
 for (var a = 0; a < this.length; a++) e.call(this[a], a, this[a]);
 return this
 },
 html: function(e) {
 if ("undefined" == typeof e) return this[0] ? this[0].innerHTML : void 0;
 for (var a = 0; a < this.length; a++) this[a].innerHTML = e;
 return this
 },
 find: function(a) {
 for (var t = [], r = 0; r < this.length; r++)
 for (var i = this[r].querySelectorAll(a), s = 0; s < i.length; s++) t.push(i[s]);
 return new e(t)
 },
 append: function(a) {
 var t, r;
 for (t = 0; t < this.length; t++)
 if ("string" == typeof a) {
 var i = document.createElement("div");
 for (i.innerHTML = a; i.firstChild;) this[t].appendChild(i.firstChild)
 } else if (a instanceof e)
 for (r = 0; r < a.length; r++) this[t].appendChild(a[r]);
 else this[t].appendChild(a);
 return this
 },
 }
 var a = function(a, t) {
 var r = [],
 i = 0;
 if (a && !t && a instanceof e) {
 return a;
 }
 if (a) {
 if ("string" == typeof a) {
 var s, n, o = a.trim();
 if (o.indexOf("<") >= 0 && o.indexOf(">") >= 0) {
 var l = "div";
 for (0 === o.indexOf("<li") && (l = "ul"), 0 === o.indexOf("<tr") && (l = "tbody"), (0 === o.indexOf("<td") || 0 === o.indexOf("<th")) && (l = "tr"), 0 === o.indexOf("<tbody") && (l = "table"), 0 === o.indexOf("<option") && (l = "select"), n = document.createElement(l), n.innerHTML = a, i = 0; i < n.childNodes.length; i++) r.push(n.childNodes[i])
 } else
 for (s = t || "#" !== a[0] || a.match(/[ .<>:~]/) ? (t || document).querySelectorAll(a) : [document.getElementById(a.split("#")[1])], i = 0; i < s.length; i++) s[i] && r.push(s[i])
 } else if (a.nodeType || a === window || a === document) {
 r.push(a);
 } else if (a.length > 0 && a[0].nodeType) {
 for (i = 0; i < a.length; i++) {
 r.push(a[i]);
 }
 }
 }
 return new e(r)
 };
 return a;
 }())
 window.select = Select;
 })()
 /*封装代码*/
 </script>
</head>
<body>
 <div class="header">
 <a href="https://github.com/huanghanzhilian/widget" target="_blank">项目地址</a>
 <a href="/widget/">返回首页</a>
 </div>
 <div class="main">
 <div class="select-container" id="select1">
 <cite class="select-pose">请选择分类</cite>
 <ul class="select-content">
 <li class="select-list">选项一</li>
 <li class="select-list">选项二</li>
 <li class="select-list">选项三</li>
 <li class="select-list">选项四</li>
 </ul>
 </div>
 <script type="text/javascript">
 new select("#select1");
 </script>
 <div class="code">
 <p>
 支持键盘事件,执行默认参数,鼠标点击分类显示,鼠标经过list执行默认参数
 </p>
 <p>new select("#select1");</p>
 </div>
 <div class="example">
 <div class="call">
 <h1>调用方法:</h1>
 <p>new select(selector,{options});</p>
 </div>
 <h2>options参数</h2>
 <table>
 <thead>
 <tr>
 <th width="150">参数</th>
 <th width="100">默认值</th>
 <th>说明</th>
 </tr>
 </thead>
 <tbody>
 <tr>
 <td>before</td>
 <td>'#fff'</td>
 <td>鼠标经过list初始化颜色色值</td>
 </tr>
 <tr>
 <td>after</td>
 <td>'#ccc'</td>
 <td>鼠标经过list颜色色值</td>
 </tr>
 </tbody>
 </table>
 </div>
 </div>
</body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

Javascript 相关文章推荐
namespace.js Javascript的命名空间库
Oct 11 Javascript
Javascript insertAfter() 实现函数代码
Oct 12 Javascript
javascript object array方法使用详解
Dec 03 Javascript
前后台交互过程中json格式如何解析以及如何生成
Dec 26 Javascript
用js实现trim()的解决办法
Apr 16 Javascript
JS实现随机乱撞彩色圆球特效的方法
May 05 Javascript
js实现将选中值累加到文本框的方法
Aug 12 Javascript
JavaScript文本框脚本编写的注意事项
Jan 25 Javascript
两种JavaScript的AES加密方式(可与Java相互加解密)
Aug 02 Javascript
Vue 进入/离开动画效果
Dec 26 Javascript
关于react-router/react-router-dom v4 history不能访问问题的解决
Jan 08 Javascript
使用typescript开发angular模块并发布npm包
Apr 19 Javascript
js实现自动轮换选项卡
Jan 13 #Javascript
Javascript 使用ajax与C#获取文件大小实例详解
Jan 13 #Javascript
详解angularJs中自定义directive的数据交互
Jan 13 #Javascript
微信小程序 MD5加密登录密码详解及实例代码
Jan 12 #Javascript
很棒的一组js图片轮播特效
Jan 12 #Javascript
微信小程序 页面跳转传递值几种方法详解
Jan 12 #Javascript
微信小程序 详解Page中data数据操作和函数调用
Jan 12 #Javascript
You might like
php随机输出名人名言的代码
2012/10/07 PHP
PHP使用curl函数发送Post请求的注意事项
2016/11/26 PHP
在IE模态窗口中自由查看HTML源码的方法
2007/03/08 Javascript
什么是JavaScript
2009/08/13 Javascript
JQuery页面图片切换和新闻列表滚动效果的具体实现
2013/09/26 Javascript
浅析Cookie中的Path与domain
2013/12/18 Javascript
jquery复选框checkbox实现删除前判断
2014/04/20 Javascript
JS使用ajax从xml文件动态获取数据显示的方法
2015/03/24 Javascript
详细解读AngularJS中的表单验证编程
2015/06/19 Javascript
在for循环中length值是否需要缓存
2015/07/27 Javascript
原生JavaScript实现瀑布流布局
2020/06/28 Javascript
jquery 全选、全不选、反选效果的实现代码【推荐】
2016/05/05 Javascript
JavaScript学习笔记整理_用于模式匹配的String方法
2016/09/19 Javascript
node+experss实现爬取电影天堂爬虫
2016/11/20 Javascript
jQuery UI插件实现百度提词器效果
2016/11/21 Javascript
BootStrap 模态框实现刷新网页并关闭功能
2017/01/04 Javascript
浅谈ES6新增的数组方法和对象
2017/08/08 Javascript
javascript 作用于作用域链的详解
2017/09/27 Javascript
JavaScript常见事件对象与操作实例总结
2019/01/05 Javascript
javascript数组去重方法总结(推荐)
2019/03/20 Javascript
javascript设计模式 ? 单例模式原理与应用实例分析
2020/04/09 Javascript
JS原型对象操作实例分析
2020/06/06 Javascript
Vue记住滚动条和实现下拉加载的完美方法
2020/07/31 Javascript
详谈python http长连接客户端
2017/06/12 Python
详解python3中zipfile模块用法
2018/06/18 Python
浅谈spring boot 集成 log4j 解决与logback冲突的问题
2020/02/20 Python
python math模块的基本使用教程
2021/01/16 Python
ECCO爱步美国官网:来自丹麦的鞋履品牌
2016/11/23 全球购物
国贸类专业毕业生的求职信分享
2013/12/08 职场文书
商务经理岗位职责
2014/07/30 职场文书
售后客服个人自我评价
2014/09/14 职场文书
升学宴学生答谢词
2015/01/05 职场文书
2015年幼儿园中班工作总结
2015/04/25 职场文书
电影圆明园观后感
2015/06/03 职场文书
创业方案:赚钱的烧烤店该怎样做?
2019/07/05 职场文书
Win11 Build 22000.829更新补丁KB5015882发布(附更新修复内容汇总)
2022/07/15 数码科技