js下拉菜单生成器dropMenu使用方法详解


Posted in Javascript onAugust 01, 2017

本文实例为大家分享了下拉菜单生成器dropMenu的使用方法,供大家参考,具体内容如

HTML

<div class="input-group">
 <span class="input-group-addon" style="width: 100px" >职级:</span>
 <input type="text" class="units form-control" id="jobTitle" value="其他" style="border-radius:0 4px 4px 0;"></input>
 <span class="caret beside"></span>
</div>

js

$(function(){
 var title,
 populationType,
 titleInParty;
 $.ajax({
 url:'/api/v1/user/getUserTypeInfo',
 type:'GET',
 dataType:'json',
 success:function (data) {
  title=data.data.title;
  titleInParty=data.data.titleInParty;
  populationType=data.data.populationType;
  partyLabel('jobTitle',title);
  partyLabel('populationType',populationType);
  partyLabel('titleInParty',titleInParty);
 }
 });

function partyLabel(menuID,data){
 new DropMeun({
  'id':menuID,
  "data":data,
  "dataSrc":"name", //数据是下面的这种格式的,你要的是name的值
  "ableSearch":true, //可以搜索
  "style":{ //样式,可选
  "width":173,
  "maxHeight":200,
  "left":0, //定位到哪里
  "top":5,
  "initPos":"left" //设置在哪边出现
  }
 })
 }

3.在页面中引用一个js 文件

(function(vq0599) {
 window.DropMeun = vq0599
})(function() {

 /*-- tools --*/

 function getRealTop(node) {
 return node.offsetParent.tagName.toUpperCase() === 'BODY' ?
 node.offsetTop :
 node.offsetTop + arguments.callee(node.offsetParent)
 }

 function getRealLeft(node) {
 return node.offsetParent.tagName.toUpperCase() === 'BODY' ?
 node.offsetLeft :
 node.offsetLeft + arguments.callee(node.offsetParent)
 }

 /*-- tools end--*/


 function DropMeun(option) {
  this.picker = null
  this.self = null
  this.option = option
  this.item = option.item || []
  this.style = option.style || {}
  this.dataList = option.data || []

  this.init()
 return this;
 }

 DropMeun.prototype.init = function () {
  var html = '',
    _this = this

  this.self = document.createElement('ul')
  this.picker = document.getElementById(this.option.id)

  if (! this.picker) {
   throw 'picker is null, making sure that picker\'s ID \''+ this.option.id +'\' is correct'
   return
  }


  if (this.option.ableSearch) {
   html += '<li><input class="dropMeun-searchInput" type="text"></li>'
  }

  this.dataList.forEach(function(data, index) {
   var item   = _this.option.dataSrc ? data[_this.option.dataSrc] : data,
     content = _this.item.render ? _this.item.render(item, data) : item

   html += '<li class="dropMeun-item '+ (_this.item.className || '') +'" data-index="'+ index +'">'+ content +'</li>'
  })

  this.self.classList.add('dropMeun')
  this.self.innerHTML = html
  document.body.appendChild(this.self)

  this.setStyle()
  this.bindEvent()
 }

 DropMeun.prototype.setStyle = function() {

  this.self.style.width =
  this.style.width ?
  (parseInt(this.style.width) - 26) + 'px' :
  '150px'

  this.self.style.maxHeight =
  this.style.maxHeight ?
  (parseInt(this.style.maxHeight) - 26) + 'px' :
  '300px'

  var w = getRealLeft(this.picker) + (parseInt(this.style.left) || 0)
  var h = getRealTop(this.picker) + this.picker.offsetHeight + (parseInt(this.style.top) || 0)

  var realWidth = parseInt(this.self.style.width) + 26  // 26 = dobule(padding + border)

  if (this.style.initPos === 'right') {
   w = w - realWidth + this.picker.offsetWidth
  }

  this.self.style.top = h + 'px'
  this.self.style.left = w + 'px'

 }

 DropMeun.prototype.bindEvent = function() {
  var

  _this = this,
  iEvent = this.picker.nodeName.toUpperCase() !== 'INPUT' ?
       'click' :
       this.picker.type.toUpperCase() === 'TEXT' ?
       'focus' : 'click'

  this.picker.addEventListener('click', function(ev) {
   var ev = ev || window.ev
   ev.stopPropagation()
  })

  //
  this.picker.addEventListener(iEvent, function(ev) {

   document.body.click()  // 触发 window.click 使其他dropMeun关闭

   _this.self.style.display = 'block'
  })

  //
  window.addEventListener('click', function() {
   _this.self.style.display = 'none'
  })

  //
  this.self.addEventListener('click', function(ev) {
   var ev = ev || window.ev
   ev.stopPropagation()

   // 事件委托 item点击
   if (ev.target.classList.contains('dropMeun-item')) {
    var index = parseInt(ev.target.getAttribute('data-index'))
      data = _this.option.dataSrc ?
          _this.dataList[index][_this.option.dataSrc] :
          _this.dataList[index]


    if (iEvent === 'focus') {
     _this.picker.value = ev.target.innerText
    }

  if (_this.item.callbakc) {
   _this.item.callbakc(data, _this.picker, _this.dataList[index], _this.dataList)
  }

    _this.self.style.display = 'none'
   }
  })
  //
  if (_this.option.ableSearch) {

   _this.searchInput = _this.self.getElementsByClassName('dropMeun-searchInput')[0]

   _this.searchInput.addEventListener('keyup', function() {
    var target = this.value.trim(),
      items = _this.self.getElementsByClassName('dropMeun-item');

    [].slice.call(items).forEach(function(item, index) {
     item.style.display =
     item.innerText.indexOf(target) === -1 ?
     'none' : ''
    })

   })
  }
 }

 return DropMeun
}())

4.在页面中引用一个css文件

ul,
li {
 list-style: none;
 margin: 0;
 padding: 0;
}

.dropMeun {
 position: absolute;
 border: 1px solid #ccc;
 overflow: auto;
 padding: 8px 12px;
 box-shadow: 0 6px 12px rgba(0, 0, 0, .175);
 background-color: #fff;
 border-bottom-left-radius: 4px;
 border-bottom-right-radius: 4px;
 box-sizing: content-box;
 display: none;
}

.dropMeun li.dropMeun-item {
 min-width: 150px;
 padding: 2px 2px;
 white-space: nowrap;
 overflow: hidden;
 text-overflow: ellipsis;
}

.dropMeun li.dropMeun-item:hover {
 cursor: pointer;
 background-color: rgba(238, 238, 238, 0.8);
}

.dropMeun-searchInput {
 outline: none;
 width: 100%;
 box-sizing: border-box;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
Javascript继承(上)——对象构建介绍
Nov 08 Javascript
url参数中有+、空格、=、%、&amp;、#等特殊符号的问题解决
May 15 Javascript
$.getJSON在IE下失效的原因分析及解决方法
Jun 16 Javascript
js简单实现HTML标签Select联动带跳转
Oct 23 Javascript
深入探究AngularJs之$scope对象(作用域)
Jul 20 Javascript
ionic3 懒加载
Aug 16 Javascript
JS实现table表格固定表头且表头随横向滚动而滚动
Oct 26 Javascript
mongoose设置unique不生效问题的解决及如何移除unique的限制
Nov 07 Javascript
Gulp实现静态网页模块化的方法详解
Jan 09 Javascript
AJAX在JQuery中的应用详解
Jan 30 jQuery
vue 移动端记录页面浏览位置的方法
Mar 11 Javascript
JS实现4位随机验证码
Oct 19 Javascript
简述jQuery Easyui一些用法
Aug 01 #jQuery
js图片上传的封装代码
Aug 01 #Javascript
使用重写url机制实现验证码换一张功能
Aug 01 #Javascript
js实现拖拽上传图片功能
Aug 01 #Javascript
Jquery中.bind()、.live()、.delegate()和.on()之间的区别详解
Aug 01 #jQuery
angularjs2 ng2 密码隐藏显示的实例代码
Aug 01 #Javascript
使用JavaScript进行表单校验功能
Aug 01 #Javascript
You might like
PHP 批量更新网页内容实现代码
2010/01/05 PHP
php数据结构之顺序链表与链式线性表示例
2018/01/22 PHP
laravel 框架配置404等异常页面
2019/01/07 PHP
疯掉了,尽然有js写的操作系统
2007/04/23 Javascript
用JavaScript编写COM组件的步骤
2009/03/17 Javascript
浅析JavaScript中的delete运算符
2013/11/30 Javascript
JavaScript 作用域链解析
2014/11/13 Javascript
jQuery密码强度检测插件passwordStrength用法实例分析
2015/10/30 Javascript
Node.js的npm包管理器基础使用教程
2016/05/26 Javascript
angularjs 表单密码验证自定义指令实现代码
2016/10/27 Javascript
用JS动态设置CSS样式常见方法小结(推荐)
2016/11/10 Javascript
javascript实现获取图片大小及图片等比缩放的方法
2016/11/24 Javascript
vue.js实例对象+组件树的详细介绍
2017/10/20 Javascript
基于jQuery中ajax的相关方法汇总(必看篇)
2017/11/08 jQuery
京东优选小程序的实现代码示例
2020/02/25 Javascript
JavaScript直接调用函数与call调用的区别实例分析
2020/05/22 Javascript
Echarts在Taro微信小程序开发中的踩坑记录
2020/11/09 Javascript
Js利用正则表达式去除字符串的中括号
2020/11/23 Javascript
JS addEventListener()和attachEvent()方法实现注册事件
2021/01/11 Javascript
[01:23:45]DOTA2-DPC中国联赛 正赛 CDEC vs Dragon BO3 第一场 1月22日
2021/03/11 DOTA
python在Windows下安装setuptools(easy_install工具)步骤详解
2016/07/01 Python
Python中字典的setdefault()方法教程
2017/02/07 Python
基于Python的文件类型和字符串详解
2017/12/21 Python
Python实现查找字符串数组最长公共前缀示例
2019/03/27 Python
Python中py文件转换成exe可执行文件的方法
2019/06/14 Python
Django 自定义权限管理系统详解(通过中间件认证)
2020/03/11 Python
python使用scapy模块实现ping扫描的过程详解
2021/01/21 Python
中国领先的专业演出票务网:永乐票务
2016/08/29 全球购物
俄罗斯商务邀请函
2014/01/26 职场文书
交通事故协议书范文
2014/10/23 职场文书
2014幼儿园中班工作总结
2014/11/10 职场文书
幼师辞职信范文
2015/02/27 职场文书
前端学习——JavaScript原生实现购物车案例
2021/03/31 Javascript
JavaScript canvas实现流星特效
2021/05/20 Javascript
详细聊聊MySQL中慢SQL优化的方向
2021/08/30 MySQL
Python Pandas数据分析之iloc和loc的用法详解
2021/11/11 Python