jq+css+html簡單實現導航下拉菜單


相信導航欄下拉菜單是web開發最常見的一個item了.這里就不做介紹了,直接上code.

Html部分

 1  <div class="_nav">
 2         <ul id="sddm">
 3             <li><a href="/Home/Index" id="a_nav_Home" class="a_nav_Menu">Home</a>
 4                 <div id="nav_Home" class="div_nav_Menu">
 5                 </div>
 6             </li>
 7             <li><a href="#" id="a_nav_Employee" class="a_nav_Menu">Employee</a>
 8                 <div id="nav_Employee" class="div_nav_Menu">
 9                     @Html.ActionLink("Test", "Test", new { area = "BMOManagement", controller = "Test" })
10                     @Html.ActionLink("Test", "Test", new { area = "BMOManagement", controller = "Test" })
11                     @Html.ActionLink("Test", "Test", new { area = "BMOManagement", controller = "Test" })
12                     @Html.ActionLink("Test", "Test", new { area = "BMOManagement", controller = "Test" })
13                     @Html.ActionLink("Test", "Test", new { area = "BMOManagement", controller = "Test" })
14                 </div>
15             </li>
16             <li><a href="#" id="a_nav_Billing" class="a_nav_Menu">Billing</a>
17                 <div id="nav_Billing" class="div_nav_Menu">
18                     <a href="#">B Test1</a>
19                     <a href="#">B Test2</a>
20                 </div>
21             </li>
22             <li><a href="#" id="a_nav_Forecast" class="a_nav_Menu">Forecast</a>
23                 <div id="nav_Forecast" class="div_nav_Menu">
24                     <a href="#">F Test1</a>
25                     <a href="#">F Test2</a>
26                     <a href="#">F Test3</a>
27                 </div>
28             </li>
29         </ul>
30     </div>
View Code

CSS部分

/*********************------------------------------Menu CSS------------------------*********************/

 1 #sddm {
 2             margin: 0 auto;
 3         }
 4 
 5             #sddm li {
 6                 margin: 0;
 7                 padding: 0;
 8                 list-style: none;
 9                 float: left;
10                 font: bold 12px arial;
11             }
12 
13                 #sddm li a {
14                     display: block;
15                     margin: 0 1px 0 0;
16                     padding: 4px 10px;
17                     width: 60px;
18                     color: #49AB6E;
19                     text-align: center;
20                     text-decoration: none;
21                 }
22 
23                     #sddm li a:hover {
24                         background: #49AB6E;
25                         color: white;
26                     }
27 
28             #sddm div {
29                 position: absolute;
30                 visibility: hidden;
31                 margin: 0;
32                 padding: 0;
33             }
34 
35                 #sddm div a {
36                     position: relative;
37                     display: block;
38                     margin: 0;
39                     width: auto;
40                     white-space: nowrap;
41                     text-align: center;
42                     text-decoration: none;
43                     background: #49AB6E;
44                     color: white;
45                     font: 12px arial;
46                 }
47 
48                     #sddm div a:hover {
49                         background: #E5D6B8;
50                         color: white;
51                     }
View Code

JS部分

 

 1 var timeout = 1;
 2 var closetimer = 0;
 3 var ddmenuitem = 0;
 4 
 5 // close showed layer
 6 function mclose() {
 7     if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';
 8 }
 9 
10 // cancel close timer
11 function mcancelclosetime() {
12     if (closetimer) {
13         window.clearTimeout(closetimer);
14         closetimer = null;
15     }
16 }
17 
18 // close layer when click-out
19 document.onclick = mclose;
20 // -->
21 
22 $(function () {
23     // open hidden layer
24     $(".a_nav_Menu").mouseover(function () {
25         var currentId = $(this).attr("id");
26 
27         // cancel close timer
28         mcancelclosetime();
29 
30         // close old layer
31         if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';
32         $("#" + currentId.replace("a_","")).width($("#" + currentId).parent().width() - 1);
33         $("#" + currentId).css({ "background": "#49AB6E", "color": "white" });
34 
35         // get new layer and show it
36         ddmenuitem = document.getElementById(currentId.replace("a_", ""));
37         ddmenuitem.style.visibility = 'visible';
38     });
39 
40     // go close timer
41     $(".a_nav_Menu").mouseout(function () {
42         var currentId = $(this).attr("id");
43         $("#" + currentId).css({ "background": "", "color": "#49AB6E" });
44         
45         closetimer = window.setTimeout(mclose, timeout);
46     });
47 
48     // go close timer
49     $(".div_nav_Menu").mouseout(function () {
50         var currentId = $(this).attr("id");
51         $("#a_" + currentId).css({ "background": "", "color": "#49AB6E" });
52         closetimer = window.setTimeout(mclose, timeout);
53     });
54 
55     $(".div_nav_Menu").mouseover(function () {
56         var currentId = $(this).attr("id");
57         $("#a_" + currentId).css({ "background": "#49AB6E", "color": "white" });
58         mcancelclosetime();
59     });
60 })
View Code

 

 

這里只是簡單實現一下,如果頁面要求比較高,可能就不是很實用了,這個只是當時接了一個私活,因為比較注重業務而不是code所以就寫的比較快。


作  者:請叫我頭頭哥
出  處:http://www.cnblogs.com/toutou/
關於作者:專注於基礎平台的項目開發。如有問題或建議,請多多賜教!
版權聲明:本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。
特此聲明:所有評論和私信都會在第一時間回復。也歡迎園子的大大們指正錯誤,共同進步。或者直接私信
聲援博主:如果您覺得文章對您有幫助,可以點擊文章右下角“推薦”一下。您的鼓勵是作者堅持原創和持續寫作的最大動力!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM