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 相关文章推荐
多个$(document).ready()的执行顺序实例分析
Jul 26 Javascript
js实现ifram取父窗口URL地址的方法
Feb 09 Javascript
js实现非常简单的焦点图切换特效实例
May 07 Javascript
Eclipse编辑jsp、js文件时卡死现象的解决办法汇总
Feb 02 Javascript
关于jquery中动态增加select,事件无效的快速解决方法
Aug 29 Javascript
javascript入门之数组[新手必看]
Nov 21 Javascript
jquery利用json实现页面之间传值的实例解析
Dec 12 Javascript
jquery ajaxfileupload异步上传插件使用详解
Feb 08 Javascript
JavaScript中递归实现的方法及其区别
Sep 12 Javascript
原生JS实现前端本地文件上传
Sep 08 Javascript
在vue中使用express-mock搭建mock服务的方法
Nov 07 Javascript
JS中getElementsByClassName与classList兼容性问题解决方案分析
Aug 07 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实现采集抓取淘宝网单个商品信息
2015/01/08 PHP
php json相关函数用法示例
2017/03/28 PHP
如何修改yii2.0自带的user表为其它的表
2017/08/01 PHP
通过源码解析Laravel的依赖注入
2018/01/22 PHP
模仿jQuery each函数的链式调用
2009/07/22 Javascript
浏览器打开层自动缓慢展开收缩实例代码
2013/07/04 Javascript
node.js中的fs.chmod方法使用说明
2014/12/18 Javascript
JS动态插入并立即执行回调函数的方法
2016/04/21 Javascript
使用Javascript监控前端相关数据的代码
2016/10/27 Javascript
基于SpringMVC+Bootstrap+DataTables实现表格服务端分页、模糊查询
2016/10/30 Javascript
JavaScript数组去重的6个方法
2017/01/21 Javascript
详解vue-cli快速构建vue应用并实现webpack打包
2017/12/13 Javascript
使用webpack打包koa2 框架app
2018/02/02 Javascript
JS异步错误捕获的一些事小结
2019/04/26 Javascript
从0搭建vue-cli4脚手架
2020/06/17 Javascript
[09:43]DOTA2每周TOP10 精彩击杀集锦vol.5
2014/06/25 DOTA
python3.0 字典key排序
2008/12/24 Python
python time模块用法实例详解
2014/09/11 Python
python快速查找算法应用实例
2014/09/26 Python
Python Socket编程之多线程聊天室
2018/07/28 Python
Mac下Anaconda的安装和使用教程
2018/11/29 Python
解决python3中的requests解析中文页面出现乱码问题
2019/04/19 Python
深入了解Python枚举类型的相关知识
2019/07/09 Python
查看已安装tensorflow版本的方法示例
2020/04/19 Python
使用Python将图片转正方形的两种方法实例代码详解
2020/04/29 Python
用python监控服务器的cpu,磁盘空间,内存,超过邮件报警
2021/01/29 Python
Html5 canvas实现粒子时钟的示例代码
2018/09/06 HTML / CSS
卡西欧G-SHOCK英国官网: 防水防震手表
2018/01/08 全球购物
美国温暖商店:The Warming Store
2018/12/15 全球购物
德国大型箱包和皮具商店:Koffer
2019/10/01 全球购物
Weblogic的布署方式
2013/08/23 面试题
J2EE中的容器都包括哪些
2013/08/21 面试题
保险公司年会主持词
2014/03/22 职场文书
城南旧事读书笔记
2015/06/29 职场文书
《一面五星红旗》教学反思
2016/02/23 职场文书
Python源码解析之List
2021/05/21 Python