您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何編寫JavaScript實(shí)現(xiàn)下拉菜單的顯示隱藏”,在日常操作中,相信很多人在如何編寫JavaScript實(shí)現(xiàn)下拉菜單的顯示隱藏問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何編寫JavaScript實(shí)現(xiàn)下拉菜單的顯示隱藏”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
具體內(nèi)容如下
有時(shí)需要這種頁面效果:
鼠標(biāo)移動(dòng)到元素上面時(shí),實(shí)現(xiàn)下拉菜單
鼠標(biāo)移開元素后,下拉菜單不見了
1、一個(gè)盒子里包含上下兩部分,下面部分為子菜單,先設(shè)置為隱藏:display: none;
2、當(dāng)鼠標(biāo)移動(dòng)到盒子上:觸發(fā)事件- - -onmouseover ,js設(shè)置下面部分子菜單的display值為- - -block,使子菜單顯示
3、鼠標(biāo)移開盒子:觸發(fā)事件- - -onmouseout ,js又設(shè)置下面部分子菜單的display值為- - -none,使子菜單又隱藏起來
4、字體顏色,背景顏色等樣式的改變,根據(jù)所需進(jìn)行相應(yīng)變化
<!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> <style> * { margin: 0; padding: 0; box-sizing: border-box; } ul li { list-style: none; } a { text-decoration: none; color: #4c4c4c; } a:hover { color: #e88415; } .box { width: 80px; margin: 50px auto; font-size: 14px; color: #4c4c4c; } .weibo { position: relative; background-color: #fcfcfc; } .weibo a { display: block; height: 40px; line-height: 40px; padding-left: 20px; } .change { color: #f9a74f; background-color: #edeef0; } i { position: absolute; top: 50%; right: 15px; margin-top: -4px; width: 5px; height: 5px; border-bottom: 1px solid orangered; border-right: 1px solid orangered; transform: rotate(45deg); } .weiboList { display: none; } .weiboList li a { display: block; width: 80px; height: 33px; line-height: 33px; padding-left: 15px; border-bottom: 1px solid #fecc5b; background-color: #fff; } .weiboList li a:hover { background-color: #fff5da; } </style> </head> <body> <div class="box"> <div class="weibo"><a href="#" >微博<i class="select"></i></a></div> <ul class="weiboList"> <li><a href="#" >私信</a></li> <li><a href="#" >評(píng)論</a></li> <li><a href="#" >@我</a></li> </ul> </div> <script> var box = document.querySelector('.box'); var weibo = document.querySelector('.weibo'); var weiboList = document.querySelector('.weiboList'); box.onmouseover = function() { weibo.className = 'weibo change' weiboList.style.display = 'block'; } box.onmouseout = function() { weibo.className = 'weibo'; weiboList.style.display = 'none'; } </script> </body> </html>
頁面效果:
到此,關(guān)于“如何編寫JavaScript實(shí)現(xiàn)下拉菜單的顯示隱藏”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。