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

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

HTML

职级:

js

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 文件

/-- 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 += '

  • <input class="dropMeun-searchInput" type="text">
  • '
    }

    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 +''
    })

    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文件

    .dropMeun {
    position: absolute;
    border: 1px solid #ccc;
    overflow: auto;
    padding: 8px 12px;
    box-shadow: 0 6px 12px rgba(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,0.8);
    }

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

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

    版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

    相关推荐


    kindeditor4.x代码高亮功能默认使用的是prettify插件,prettify是Google提供的一款源代码语法高亮着色器,它提供一种简单的形式来着色HTML页面上的程序代码,实现方式如下: 首先在编辑器里面插入javascript代码: 确定后会在编辑器插入这样的代码: <pre
    这一篇我将介绍如何让kindeditor4.x整合SyntaxHighlighter代码高亮,因为SyntaxHighlighter的应用非常广泛,所以将kindeditor默认的prettify替换为SyntaxHighlighter代码高亮插件 上一篇“让kindeditor显示高亮代码”中已经
    js如何实现弹出form提交表单?(图文+视频)
    js怎么获取复选框选中的值
    js如何实现倒计时跳转页面
    如何用js控制图片放大缩小
    JS怎么获取当前时间戳
    JS如何判断对象是否为数组
    JS怎么获取图片当前宽高
    JS对象如何转为json格式字符串
    JS怎么获取图片原始宽高
    怎么在click事件中调用多个js函数
    js如何往数组中添加新元素
    js如何拆分字符串
    JS怎么对数组内元素进行求和
    JS如何判断屏幕大小
    js怎么解析json数据
    js如何实时获取浏览器窗口大小
    原生JS实现别踩白块小游戏(五)
    原生JS实现别踩白块小游戏(一)