本文實例為大家分享了javascript鼠標滑過顯示二級菜單效果,供大家參考,具體內容如下
1. 關鍵代碼:使用 switch 或 if 判斷語句,改變對應的二級菜單顯示方式為 block 或 none
function selectTabMenu(i){ switch(i){ case 71: document.getElementById("TabMenuCon71").style.display="block"; document.getElementById("TabMenuCon72").style.display="none"; document.getElementById("TabMenuCon73").style.display="none"; document.getElementById("TabMenuCon74").style.display="none"; document.getElementById("TabMenuCon75").style.display="none"; document.getElementById("TabMenuCon76").style.display="none"; break; ... } }
2. 主導航 綁定事件
<ul class="nav">
<li><a href="#" onmouseover="selectTabMenu(71)">首頁</a></li>
3. 二級菜單
<ul id="TabMenuCon71" class="TabMenuCon"> <li><a href="#">公告</a></li> <li><<a href="#">信息</a></li> </ul>
4. 完整代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>水平導航-二級菜單</title> <style type="text/css"> *{ padding:0px; margin: 0px; } .navBar{ height: 30px; background-color: #2B383E; text-align: center; } ul{ list-style: none; } .nav li{ float: left; } .nav li a{ display: block; padding: 0 20px; height: 30px; line-height: 30px; text-decoration: none; color: #fff; } .nav li a:hover{ background-color: #fff; color: #4DB6E7 } .TabMenuCon{ clear: both; display: none; } .TabMenuCon li{ display: inline-block; } </style> <script type="text/javascript"> function selectTabMenu(i){ switch(i){ case 71: document.getElementById("TabMenuCon71").style.display="block"; document.getElementById("TabMenuCon72").style.display="none"; document.getElementById("TabMenuCon73").style.display="none"; document.getElementById("TabMenuCon74").style.display="none"; document.getElementById("TabMenuCon75").style.display="none"; document.getElementById("TabMenuCon76").style.display="none"; break; case 72: document.getElementById("TabMenuCon71").style.display="none"; document.getElementById("TabMenuCon72").style.display="block"; document.getElementById("TabMenuCon73").style.display="none"; document.getElementById("TabMenuCon74").style.display="none"; document.getElementById("TabMenuCon75").style.display="none"; document.getElementById("TabMenuCon76").style.display="none"; break; case 73: document.getElementById("TabMenuCon71").style.display="none"; document.getElementById("TabMenuCon72").style.display="none"; document.getElementById("TabMenuCon73").style.display="block"; document.getElementById("TabMenuCon74").style.display="none"; document.getElementById("TabMenuCon75").style.display="none"; document.getElementById("TabMenuCon76").style.display="none"; break; case 74: document.getElementById("TabMenuCon71").style.display="none"; document.getElementById("TabMenuCon72").style.display="none"; document.getElementById("TabMenuCon73").style.display="none"; document.getElementById("TabMenuCon74").style.display="block"; document.getElementById("TabMenuCon75").style.display="none"; document.getElementById("TabMenuCon76").style.display="none"; break; case 75: document.getElementById("TabMenuCon71").style.display="none"; document.getElementById("TabMenuCon72").style.display="none"; document.getElementById("TabMenuCon73").style.display="none"; document.getElementById("TabMenuCon74").style.display="none"; document.getElementById("TabMenuCon75").style.display="block"; document.getElementById("TabMenuCon76").style.display="none"; break; case 76: document.getElementById("TabMenuCon71").style.display="none"; document.getElementById("TabMenuCon72").style.display="none"; document.getElementById("TabMenuCon73").style.display="none"; document.getElementById("TabMenuCon74").style.display="none"; document.getElementById("TabMenuCon75").style.display="none"; document.getElementById("TabMenuCon76").style.display="block"; break; } } </script> </head> <body> <div class="navBar"> <ul class="nav"> <li><a href="#" onmouseover="selectTabMenu(71)">首頁</a></li> <li><a href="#" onmouseover="selectTabMenu(72)">關於協會</a></li> <li><a href="#" onmouseover="selectTabMenu(73)">協會動態</a></li> <li><a href="#" onmouseover="selectTabMenu(74)">協會活動</a></li> <li><a href="#" onmouseover="selectTabMenu(75)">會員</a></li> <li><a href="#" onmouseover="selectTabMenu(76)">資源空間</a></li> </ul> </div> <div id="TabMenuCon"> <ul id="TabMenuCon71" class="TabMenuCon"> <li><a href="#">公告</a></li> <li><<a href="#">信息</a></li> </ul> <ul id="TabMenuCon72" class="TabMenuCon"> <li>協會簡介</li> <li>組織機構</li> <li>協會章程</li> </ul> <ul id="TabMenuCon73" class="TabMenuCon"> <li>協會新聞</li> <li>活動預告</li> <li>求職招聘</li> </ul> <ul id="TabMenuCon74" class="TabMenuCon"> <li>義務維修月</li> <li>平面設計活動</li> <li>程序設計活動</li> <li>戶外拓展</li> </ul> <ul id="TabMenuCon75" class="TabMenuCon"> <li>會員登錄</li> <li>會員注冊</li> <li>繳納會費</li> <li>會員信息管理</li> <li>修改密碼</li> </ul> <ul id="TabMenuCon76" class="TabMenuCon"> <li>PS教程</li> <li>前端設計</li> <li>Flash教程</li> </ul> </div> </body> </html>
以上就是本文的全部內容,希望對大家的學習有所幫助。