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 25 Javascript
JS在textarea光标处插入文本的小例子
Mar 22 Javascript
jQuery 1.9使用$.support替代$.browser的使用方法
May 27 Javascript
jQuery处理XML文件的几种方法
Jun 14 Javascript
AngularJS入门教程之Helloworld示例
Dec 25 Javascript
AngularJS实现自定义指令与控制器数据交互的方法示例
Jun 19 Javascript
浅谈Angular路由复用策略
Oct 04 Javascript
vue中如何创建多个ueditor实例教程
Nov 14 Javascript
JavaScript函数、闭包、原型、面向对象学习笔记
Sep 06 Javascript
Vue组件教程之Toast(Vue.extend 方式)详解
Jan 27 Javascript
JS实现普通轮播图特效
Jan 01 Javascript
JS校验与最终登陆界面功能完整示例
Jan 13 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简单实现MVC
2015/02/05 PHP
Laravel学习教程之本地化模块
2017/08/18 PHP
PHP实现验证码校验功能
2017/11/16 PHP
offsetParent 算法分析
2010/04/05 Javascript
dotopAlert 提示用户需安装播放器的代码
2012/09/17 Javascript
在AngularJS中使用jQuery的zTree插件的方法
2016/04/21 Javascript
JavaScript使用键盘输入控制实现数字验证功能
2016/08/19 Javascript
JavaScript表单验证开发
2016/11/23 Javascript
JS简单获取当前日期和农历日期的方法
2017/04/17 Javascript
layui选项卡效果实现代码
2017/05/19 Javascript
Vue2.0子同级组件之间数据交互方法
2018/02/28 Javascript
你应该了解的JavaScript Array.map()五种用途小结
2018/11/14 Javascript
vue图片上传本地预览组件使用详解
2019/02/20 Javascript
js中script的上下放置区别,Dom的增删改创建操作实例分析
2019/12/16 Javascript
Vue-router编程式导航的两种实现代码
2021/03/04 Vue.js
Python转码问题的解决方法
2008/10/07 Python
python处理圆角图片、圆形图片的例子
2014/04/25 Python
让 python 命令行也可以自动补全
2014/11/30 Python
详解Python里使用正则表达式的ASCII模式
2017/11/02 Python
浅谈用Python实现一个大数据搜索引擎
2017/11/28 Python
tensorflow构建BP神经网络的方法
2018/03/12 Python
Python实现的简单排列组合算法示例
2018/07/04 Python
Python面向对象之类的定义与继承用法示例
2019/01/14 Python
Python检查ping终端的方法
2019/01/26 Python
django的聚合函数和aggregate、annotate方法使用详解
2019/07/23 Python
对python中的os.getpid()和os.fork()函数详解
2019/08/08 Python
python数据处理——对pandas进行数据变频或插值实例
2020/04/22 Python
解决安装新版PyQt5、PyQT5-tool后打不开并Designer.exe提示no Qt platform plugin的问题
2020/04/24 Python
浅谈css3中的前缀
2016/07/20 HTML / CSS
使用CSS3制作一个简单的进度条(demo)
2017/05/23 HTML / CSS
使用html5新特性轻松监听任何App自带返回键的示例
2018/03/13 HTML / CSS
美国在线精品家居网站:Burke Decor
2017/04/12 全球购物
Feelunique美国:欧洲大型的在线美妆零售电商
2018/11/04 全球购物
Nordgreen台湾官网:极简北欧设计手表
2019/08/21 全球购物
解释i节点在文件系统中的作用
2013/11/26 面试题
工程质量月活动方案
2014/02/19 职场文书