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 insertAfter() 实现函数代码
Oct 12 Javascript
基于jquery的拖动布局插件
Nov 25 Javascript
基于jquery实现发送文章到手机的代码
Dec 26 Javascript
原生JavaScript制作微博发布面板效果
Mar 11 Javascript
js jquery 获取某一元素到浏览器顶端的距离实现方法
Sep 05 jQuery
原生JS实现的自动轮播图功能详解
Dec 28 Javascript
JS使用new操作符创建对象的方法分析
May 30 Javascript
解决三元运算符 报错“SyntaxError: can''t assign to conditional expression”
Feb 12 Javascript
通过vue刷新左侧菜单栏操作
Aug 06 Javascript
详解webpack的文件监听实现(热更新)
Sep 11 Javascript
如何在vue中使用HTML 5 拖放API
Jan 14 Vue.js
JavaScript实例 ODO List分析
Jan 22 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
老机欣赏|中国60年代精品收音机
2021/03/02 无线电
索尼SONY SRF-S83/84电路分析和打磨
2021/03/02 无线电
PHP 七大优势分析
2009/06/23 PHP
php 数组使用详解 推荐
2011/06/02 PHP
PHP return语句另类用法不止是在函数中
2014/09/17 PHP
php格式文件打开的四种方法
2018/02/24 PHP
js获取客户端操作系统类型的方法【测试可用】
2016/05/27 Javascript
jQuery简单设置文本框回车事件的方法
2016/08/01 Javascript
浅析如何利用JavaScript进行语音识别
2016/10/27 Javascript
BootStrap Datepicker 插件修改为默认中文的实现方法
2017/02/10 Javascript
Vue获取DOM元素样式和样式更改示例
2017/03/07 Javascript
分享十三个最佳JavaScript数据网格库
2017/04/07 Javascript
ionic 3.0+ 项目搭建运行环境的教程
2017/08/09 Javascript
Angular项目如何升级至Angular6步骤全纪录
2018/09/03 Javascript
node 标准输入流和输出流代码实例
2019/09/19 Javascript
js实现点击生成随机div
2020/01/16 Javascript
python cookielib 登录人人网的实现代码
2012/12/19 Python
如何解决django配置settings时遇到Could not import settings 'conf.local'
2014/11/18 Python
Python数据分析之双色球中蓝红球分析统计示例
2018/02/03 Python
python解压TAR文件至指定文件夹的实例
2019/06/10 Python
解决import tensorflow as tf 出错的原因
2020/04/16 Python
如何快速一次性卸载所有python包(第三方库)呢
2020/10/20 Python
Python爬虫回测股票的实例讲解
2021/01/22 Python
CSS3实现莲花绽放的动画效果
2020/11/06 HTML / CSS
英国羊绒服装购物网站:Pure Collection
2018/10/22 全球购物
Java面试题:说出如下代码的执行结果
2015/10/30 面试题
介绍一下Java中标识符的命名规则
2014/02/03 面试题
公司员工的自我评价范例
2013/11/01 职场文书
“三支一扶”支教教师思想汇报
2014/09/13 职场文书
公司副总经理岗位职责
2014/10/01 职场文书
党的群众路线教育实践活动个人整改措施范文
2014/11/04 职场文书
2015年中学元旦晚会活动方案
2014/12/09 职场文书
《怀念母亲》教学反思
2016/02/19 职场文书
高考升学宴主持词
2019/06/21 职场文书
Python 文本滚动播放器的实现代码
2021/04/25 Python
浅谈Python协程asyncio
2021/06/20 Python