javascript實現(xiàn)下拉菜單效果
用Javascript實現(xiàn)下拉菜單,供大家參考,具體內(nèi)容如下
正在學(xué)習(xí)大前端中,有代碼和思路不規(guī)范不正確的地方往多多包涵,感謝指教
下拉菜單,或者側(cè)拉菜單在實際開發(fā)當(dāng)中非常的實用
代碼:
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title> <style> *{ padding: 0; margin: 0; border: 0; } .menu{ width: 100%; height: 50px; border: 1px solid lightyellow; box-shadow: 0 2px 5px black; } .menu div{ /*margin-top: 10px;*/ float: left; width: 19.82%; height: 50px; /* border: 1px solid red;*/ text-align: center; } button{ margin-top: 15px; cursor: pointer; width: 25px; height: 15px; background-color: pink; } .show1{ display: none; width: 19.82%; height: 250px; /*border: 1px solid black;*/ } .show1 div{ border: 1px solid pink; width: 247px; height: 48px; text-align: center; } a{ text-decoration: none; display: block; margin-top: 10px; } a:hover{ color: #ff242d; font-size: 25px; } </style></head><body> <div class='menu'> <div>下拉1 <button>^</button> </div> <div>下拉2 <button>^</button> </div> <div>下拉3 <button>^</button> </div> <div>下拉4 <button>^</button> </div> <div>下拉5 <button>^</button> </div> </div> <div class='show1'> <div><a href='http://www.aoyou183.cn/bcjs/14180.html#' >4654tyyut</a></div> <div><a href='http://www.aoyou183.cn/bcjs/14180.html#' >4654</a></div> <div><a href='http://www.aoyou183.cn/bcjs/14180.html#' >sdf</a></div> <div><a href='http://www.aoyou183.cn/bcjs/14180.html#' >sdf</a></div> <div><a href='http://www.aoyou183.cn/bcjs/14180.html#' >tert</a></div> </div> <script> var btn=document.querySelector(’button’) var show1=document.querySelector(’.show1’) var flag=0 btn.onclick=function () { if (flag === 0) { show1.style.display = ’block’ flag=1 }else { show1.style.display=’none’ flag=0 } } </script></body></html>
代碼解釋
這里主要就是用script的onclick來進(jìn)行實現(xiàn),這里我用到的按鈕,也可以換成其他的東西,做法都是類似的。
onclick點擊相應(yīng)的東西過后,便會觸發(fā)事件,調(diào)用函數(shù),然后判斷flag的值來進(jìn)行相應(yīng)的操作,隱藏/顯示div。
這里的flag是關(guān)鍵,這個變量在點擊事件發(fā)生時不斷在0.1之間變化,點擊一次即該函數(shù)被執(zhí)行一次,即循環(huán)一次,也就是判斷flag的值,從而達(dá)到顯示/隱藏的效果
演示效果
未下拉時
下拉后
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ajax請求添加自定義header參數(shù)代碼2. ASP基礎(chǔ)知識VBScript基本元素講解3. 解決android studio引用遠(yuǎn)程倉庫下載慢(JCenter下載慢)4. Kotlin + Flow 實現(xiàn)Android 應(yīng)用初始化任務(wù)啟動庫5. Python requests庫參數(shù)提交的注意事項總結(jié)6. Gitlab CI-CD自動化部署SpringBoot項目的方法步驟7. 利用CSS3新特性創(chuàng)建透明邊框三角8. 淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達(dá)式9. axios和ajax的區(qū)別點總結(jié)10. python操作mysql、excel、pdf的示例
