亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

javascript實(shí)現(xiàn)下拉菜單效果

 更新時(shí)間:2021年02月09日 14:08:32   作者:愛前端的茂茂  
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)下拉菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

用Javascript實(shí)現(xiàn)下拉菜單,供大家參考,具體內(nèi)容如下

正在學(xué)習(xí)大前端中,有代碼和思路不規(guī)范不正確的地方往多多包涵,感謝指教

下拉菜單,或者側(cè)拉菜單在實(shí)際開發(fā)當(dāng)中非常的實(shí)用

代碼:

<!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="#" >4654tyyut</a></div>
 <div><a href="#" >4654</a></div>
 <div><a href="#" >sdf</a></div>
 <div><a href="#" >sdf</a></div>
 <div><a href="#" >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)行實(shí)現(xiàn),這里我用到的按鈕,也可以換成其他的東西,做法都是類似的。

onclick點(diǎn)擊相應(yīng)的東西過后,便會(huì)觸發(fā)事件,調(diào)用函數(shù),然后判斷flag的值來進(jìn)行相應(yīng)的操作,隱藏/顯示div。

這里的flag是關(guān)鍵,這個(gè)變量在點(diǎn)擊事件發(fā)生時(shí)不斷在0.1之間變化,點(diǎn)擊一次即該函數(shù)被執(zhí)行一次,即循環(huán)一次,也就是判斷flag的值,從而達(dá)到顯示/隱藏的效果

演示效果

未下拉時(shí)

下拉后

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論