原生js實(shí)現(xiàn)下拉多選框組件完整代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>下拉多選框</title> <link rel="stylesheet" href="./css/index1.css" rel="external nofollow" > <link rel="stylesheet" href="./iconfont/iconfont.css" rel="external nofollow" > </head> <body> <div class="box"> <!-- 頭部輸入框 --> <div class="top"> <input type="text" name="" id="input1"> <i class="iconfont icon-arrow-up-bold size"></i> <ul> <li class="change">666</li> <li class="change">777</li> <li class="arr1">333</li> </ul> </div> <!-- 下拉列表 --> <div class="list"> <ul class="list-ul"> </ul> </div> </div> <script src="./js/index1.js"></script> </body> </html>
* { margin: 0; padding: 0; box-sizing: border-box; } li { list-style: none; } .box { width: 300px; position: relative; top: 50px; left: 50px; } .top { height: 45px; display: flex; position: relative; } .top input { width: 300px; height: 45px; font-size: 25px; padding: 0 20px; border-radius: 10px; border: 1px solid #d1ccd0; outline: none; } .top .blue { border: 2px solid #409eff;; } .top .size { width: 16px; height: 16px; position: relative; top: 50%; left: -30px; margin-top: -8px; transform: rotate(180deg); transition: all .5s; color: #d1ccd0; } .top ul { position: absolute; display:flex; font-size: 10px; top: 50%; transform: translateY(-50%); left: 15px; } .top ul li { background-color: #409eff; color: #fff; padding: 1px 10px; margin-left: 3px; border-radius: 5px; display: none; } .list { width: 300px; height: 230px; border-radius: 5px; margin-top: 5px; background-color:#fff; border: 1px solid #d1ccd0; box-shadow: 0px 0px 5px 0px #aaa; display: none; /* 超出隱藏,需要時(shí)加滾動(dòng)條*/ overflow: auto; } /* 隱藏滾動(dòng)條 */ .list::-webkit-scrollbar { display: none; } .list ul { display: flex; flex-direction: column; margin: 8px 0; } .list ul li { display: flex; height: 27px; align-items: center; padding: 0 20px; color: #65676b; cursor: pointer; } .list ul li:hover { background-color: #f5f8ff; } .list ul li label { font-size: 10px; margin-left: 15px; display: block; width: 100%; } .fontBlue { color: #0075ff; }
// 獲取整體大盒子元素 let box = document.querySelector('.box') // 獲取輸入框元素 let input = document.querySelector('#input1') // 獲取字體圖標(biāo)元素 let round = document.querySelector('.top .size') // 獲取下拉列表 let list = document.querySelector('.list') // 鼠標(biāo)進(jìn)入 box.addEventListener('mouseenter', function () { // 顯示下拉列表 list.style.display = 'block' // 外框線變色 input.classList.add('blue') // 圖標(biāo)旋轉(zhuǎn) round.style.transform = 'rotate(0deg)' }) // 鼠標(biāo)離開 box.addEventListener('mouseleave', function () { // 隱藏下拉列表 list.style.display = 'none' // 外框線顏色恢復(fù) input.classList.remove('blue') // 圖標(biāo)恢復(fù) round.style.transform = 'rotate(180deg)' }) // 獲取元素ul let ul = document.querySelector('.list-ul') // 新建數(shù)組對象 let optionArr = [ { id: 'l1', name: '全部' }, { id: 'l2', name: '魚香肉絲' }, { id: 'l3', name: '宮保雞丁' }, { id: 'l4', name: '西紅柿炒雞蛋' }, { id: 'l5', name: '五彩水餃' }, { id: 'l6', name: '清蒸鱸魚' }, { id: 'l7', name: '紅燒茄子' }, { id: 'l8', name: '土豆燒牛肉' }, { id: 'l9', name: '麻婆豆腐' }, { id: 'l10', name: '糖醋里脊' } ] // 新建小li,追加到ul中,通過循環(huán)遍歷渲染完成 for (let i = 0; i < optionArr.length; i++) { // 有幾條數(shù)據(jù)就新建幾個(gè)小li let li = document.createElement('li') // 新建復(fù)選框及l(fā)abel元素 let input1 = document.createElement('input') let label = document.createElement('label') // 修改表單屬性為復(fù)選框 input1.type = 'checkbox' // 給表單添加id屬性 input1.id = `${optionArr[i].id}` // 給第一個(gè)li的input和label設(shè)置class屬性,其他的li設(shè)置相同的input和label設(shè)置class屬性 if (i === 0) { input1.setAttribute("class", 'all'); label.setAttribute("class", 'sp'); } else { input1.setAttribute("class", 'check'); label.setAttribute("class", 'wz'); } // 給label修改文字內(nèi)容,值為對象的name屬性值 label.innerText = `${optionArr[i].name}` // 給label設(shè)置for屬性,值為對象的id屬性值 label.setAttribute("for", `${optionArr[i].id}`); //復(fù)選框及l(fā)abel元素追加給li li.appendChild(input1) li.appendChild(label) // 將li追加給ul ul.appendChild(li) } // 選中的數(shù)組 let chooseArr = [] let cks = document.querySelectorAll('.top ul .change') let arr1 = document.querySelector('.top ul .arr1') /* let lis = document.querySelectorAll('.list ul li') */ // 獲取剛才新建的所有class為check的復(fù)選框 let checkboxs = document.querySelectorAll('.list ul li .check') // 獲取剛才新建的所有class為wz的label let labels = document.querySelectorAll('.list ul li .wz') let tag1 = cks[0] let tag2 = cks[1] let k = 0 // 循環(huán)遍歷所有l(wèi)i,添加點(diǎn)擊事件 for (let i = 0; i < checkboxs.length; i++) { // +1是因?yàn)閘i的偽數(shù)組長度比checkboxs和checkboxs大1 checkboxs[i].addEventListener('click', function () { updateCheckStatus() }) } // 獲取新建的class為all的復(fù)選框 let all = document.querySelector('.list ul li .all') // 獲取新建的class為sp的label let sp = document.querySelector('.list ul li .sp') // 全部按鈕 all.addEventListener('click', function () { checkAll() updateCheckStatus() }) function updateCheckStatus() { // let count = 0 let arr = [] // 是否全選,默認(rèn)認(rèn)為全選 let isCheckAll = true for(let i=0;i<checkboxs.length;i++) { if(checkboxs[i].checked) { // count++ arr.push(labels[i].innerHTML) labels[i].classList.add('fontBlue') }else { isCheckAll = false labels[i].classList.remove('fontBlue') } } all.checked = isCheckAll isCheckAll ? sp.classList.add('fontBlue') : sp.classList.remove('fontBlue') console.log(arr) if(arr.length > 0) { arr1.style.display = 'block' tag1.style.display = "block" tag1.innerHTML = arr[0] console.log(tag1); if(arr.length>1) { tag2.style.display = "block" tag2.innerHTML = arr[1] console.log(tag2); }else { tag2.style.display = "none" } }else { arr1.style.display = 'none' tag1.style.display = "none" tag2.style.display = "none" } arr1.innerHTML = `+${arr.length}` } function checkAll() { sp.classList.add('fontBlue') // 取消復(fù)選框選中,字體顏色恢復(fù) if (all.checked === false) { sp.classList.remove('fontBlue') } for (let i = 0; i < checkboxs.length; i++) { // 全選操作 console.log(all.checked); checkboxs[i].checked = all?.checked } }
總結(jié)
到此這篇關(guān)于原生js實(shí)現(xiàn)下拉多選框組件的文章就介紹到這了,更多相關(guān)原生js下拉多選框組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript實(shí)現(xiàn)簡約的頁面右下角點(diǎn)擊彈出窗口示例【測試可用】
這篇文章主要介紹了javascript實(shí)現(xiàn)的頁面右下角點(diǎn)擊彈出窗口功能,結(jié)合實(shí)例形式詳細(xì)分析了javascript頁面右下角點(diǎn)擊彈出窗口功能的相關(guān)步驟、原理與注意事項(xiàng),需要的朋友可以參考下2023-07-07微信小程序數(shù)據(jù)劫持代理的實(shí)現(xiàn)
本文主要介紹了微信小程序?數(shù)據(jù)劫持代理的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01不使用jquery實(shí)現(xiàn)js打字效果示例分享
js打字效果示例js打字效果示例,data-period設(shè)置從打字返回刪字的時(shí)間,data-rotate可加減中英文詞語,不用jquery支持2014-01-01js實(shí)現(xiàn)前面自動(dòng)補(bǔ)全位數(shù)的方法
今天小編就為大家分享一篇js實(shí)現(xiàn)前面自動(dòng)補(bǔ)全位數(shù)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10javascript比較語義化版本號的實(shí)現(xiàn)代碼
這篇文章先是給大家簡單的介紹了下語義化版本號,而后再用實(shí)例代碼演示語義化版本號的比較方法,有需要的朋友們可以參考借鑒。2016-09-09JS中隊(duì)列和雙端隊(duì)列實(shí)現(xiàn)及應(yīng)用詳解
這篇文章主要介紹了JS中隊(duì)列和雙端隊(duì)列實(shí)現(xiàn)及應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09