最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

js下拉菜單生成器dropMenu使用方法詳解

來(lái)源:懂視網(wǎng) 責(zé)編:小OO 時(shí)間:2020-11-27 22:33:36
文檔

js下拉菜單生成器dropMenu使用方法詳解

本文實(shí)例為大家分享了下拉菜單生成器dropMenu的使用方法,供大家參考,具體內(nèi)容如。HTML;<;div class="input-group">;  <;span class="input-group-addon" style="width: 100px" >;職級(jí):<;/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;
推薦度:
導(dǎo)讀本文實(shí)例為大家分享了下拉菜單生成器dropMenu的使用方法,供大家參考,具體內(nèi)容如。HTML;<;div class="input-group">;  <;span class="input-group-addon" style="width: 100px" >;職級(jí):<;/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;

本文實(shí)例為大家分享了下拉菜單生成器dropMenu的使用方法,供大家參考,具體內(nèi)容如

HTML

<div class="input-group">
   <span class="input-group-addon" style="width: 100px" >職級(jí):</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", //數(shù)據(jù)是下面的這種格式的,你要的是name的值
 "ableSearch":true, //可以搜索
 "style":{ //樣式,可選
 "width":173,
 "maxHeight":200,
 "left":0, //定位到哪里
 "top":5,
 "initPos":"left" //設(shè)置在哪邊出現(xiàn)
 }
 })
 }

3.在頁(yè)面中引用一個(gè)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() // 觸發(fā) window.click 使其他dropMeun關(guān)閉

 _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點(diǎn)擊
 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.在頁(yè)面中引用一個(gè)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;
}

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

js下拉菜單生成器dropMenu使用方法詳解

本文實(shí)例為大家分享了下拉菜單生成器dropMenu的使用方法,供大家參考,具體內(nèi)容如。HTML;<;div class="input-group">;  <;span class="input-group-addon" style="width: 100px" >;職級(jí):<;/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;
推薦度:
標(biāo)簽: 使用 下拉菜單 使用方法
  • 熱門(mén)焦點(diǎn)

最新推薦

猜你喜歡

熱門(mén)推薦

專(zhuān)題
Top