本文介紹如何使用純Javascript來開發一款簡單的JS插件,本插件能夠實現鼠標懸停在導航上時,下方的滑動條自動從當前菜單滑動到所選菜單當中去。javascript
本項目的源代碼寄宿於GitHub,記得點小星星哦:html
https://github.com/dosboy0716/sliding-nav前端
效果以下圖:java
本插件只須要以下的三步,就能夠在您的項目中使用:git
一、在</body>標記結束前,引用sliding-nav.js文件github
二、在須要滑動條的菜單容器上加類名 sliding-nav,當前項使用類名:active 函數
三、使用屬性來定定外觀:sn-color="顏色" sn-radius="圓度" sn-height="高度"優化
<script src="/path/to/sliding-nav.js"></script> <ul class="nav sliding-nav" sn-color="#F00" sn-radius="0px" sn-height="3px"> <li class="active">菜單項1</li> <li>菜單項2</li> <li>菜單項3</li> <ul>
導航菜單通常使用上圖的層次型結構,外層容器使用<ul> 標記,菜單項使用<li>標記,假設若是要顯示黃色小橫條,如何定位很重要。動畫
通過分析,雖然在視覺上小橫條位於UL以內,爲了避免破壞原來導航的樣式,小黃條必須使用absolute的絕對定位,而且初始位置與ul標記相同。spa
所以,咱們把小橫條插入<ul>標記的前面,如上面的小灰點,它就是小橫條的初始位置即(left=0,top=0)的位置。
那麼咱們如何讓小條看起來在菜單項的正下方呢?
實現上面的功能可使用以下的代碼:
function init() { var navs = document.getElementsByClassName('sliding-nav'); for (var i = 0; i < navs.length; i++) { //建立一個DIV與當前導航豎向對齊 var indi = document.createElement("div"); indi.id = "slna-indicator" indi.style.borderRadius = navs[i].getAttribute("sn-radius") || "0px" indi.style.height = navs[i].getAttribute("sn-height") || "3px" indi.style.backgroundColor = navs[i].getAttribute("sn-color") || "#F00" indi.style.position = "absolute" indi.style.transition = "0.5s" //查找當前子菜單項,若是有類名active或者是selected就視爲當前項,若是沒有使用第1項 var selected = navs[i].getElementsByClassName('active') if (selected.length == 0) { selected = navs[i].getElementsByClassName('selected') } if (selected.length == 0) { selected = navs[i].children } if (selected.length == 0) { throw Error('Sorry, Navigation bar has no item at all!'); } selected = selected[0]; indi.style.width = selected.offsetWidth + "px"; indi.style.top = selected.offsetHeight + "px"; indi.style.left = selected.offsetLeft + "px"; navs[i].parentElement.insertBefore(indi, navs[i]); //未完成,下面插入代碼以綁定事件 } }
如上的代碼構建了初始化函數init(),此函數:
查找全部含有類名sliding-nav的標記,而且按照上面的方法,在前面插入div標記充當「指示條」,而且查找「活動」的菜單項,找到後經過這個菜單項的各個屬性給「指示條」定位。
咱們把"指示條"div 標記transition屬性設置成了0.5s,那麼只要在事件裏直接設置該div的以下:
因此能夠在如上的代碼末尾,插入以下的代碼實現事件與動畫:
for (var j = 0; j < navs[i].children.length; j++) { hover(navs[i].children[j], function(e, elem) { indi.style.width = elem.offsetWidth + "px"; indi.style.left = elem.offsetLeft + "px"; }); //移出導航就恢復默認 hover(navs[i], null, function(e, elem) { indi.style.width = selected.offsetWidth + "px"; indi.style.left = selected.offsetLeft + "px"; }); }
其中代碼,用到了自定義函數hover,該函數相似於實現hover事件,JS原生只有mouseover和mouseout事件。
hover(綁定DOM元素,移入事件函數,移出事件函數)
函數做用是給DOM元素綁定鼠標移入和鼠標移出事件,具體實現的過程,能夠看做者原代碼。
本文實現的全部原代碼以下,但願讀者提出更加優化的建議,咱們一塊兒打造更加惟美的前端體驗。
1 /* 2 3 Usage 4 1. Include file sliding-nav.js before tag</body> in a HTML file. 5 6 <script src="/path/to/sliding-nav.js"></script> 7 8 2. Use class name sliding-nav in a navigation bar element,and use .active for a selected menu item. 9 3. Use following attributes to change its color,radius and height:sn-color, sn-radius,sn-height. (if no these attributes, default settings following will be used) 10 11 <ul class="nav sliding-nav" sn-color="#F00" sn-radius="0px" sn-height="3px"> 12 <li class="active">menu-item 1</li> 13 <li>menu-item 2</li> 14 <li>menu-item 3</li> 15 <ul> 16 17 使用方法 18 一、在</body>標記結束前,引用sliding-nav.js文件 19 二、在須要滑動條的菜單容器上加類名 sliding-nav,當前項使用類名:active 20 三、使用屬性來定定外觀:sn-color="顏色" sn-radius="圓度" sn-height="高度" 21 22 23 <script src="/path/to/sliding-nav.js"></script> 24 <ul class="nav sliding-nav" sn-color="#F00" sn-radius="0px" sn-height="3px"> 25 <li class="active">菜單項1</li> 26 <li>菜單項2</li> 27 <li>菜單項3</li> 28 <ul> 29 30 Sliding Navigation Bar By yan,ZHANG 31 Mailto: 26959368@qq.com 32 2020.02.06 33 */ 34 35 36 window.onload = function() { 37 init(); 38 }; 39 40 function bind(elem, ev, callback) { 41 if (document.all) { 42 elem.attachEvent("on" + ev, callback); 43 } else { 44 elem.addEventListener(ev, callback, false); 45 } 46 } 47 48 function unbind(elem, ev, callback) { 49 if (typeof(callback) == "function") { 50 if (document.all) { 51 elem.detachEvent("on" + ev, callback); 52 } else { 53 elem.removeEventListener(ev, callback, false); 54 } 55 } else { 56 if (document.all) { 57 elem.detachEvent("on" + ev); 58 } else { 59 elem.removeEventListener(ev, false); 60 } 61 } 62 } 63 64 function hover(elem, overCallback, outCallback) { //實現hover事件 65 var isHover = false; //判斷是否懸浮在上方 66 var preOvTime = new Date().getTime(); //上次懸浮時間 67 function over(e) { 68 var curOvTime = new Date().getTime(); 69 isHover = true; //處於over狀態 70 if (curOvTime - preOvTime > 10) { //時間間隔超過10毫秒,認爲鼠標完成了mouseout事件 71 overCallback && overCallback(e, elem); 72 } 73 preOvTime = curOvTime; 74 } 75 76 function out(e) { 77 var curOvTime = new Date().getTime(); 78 preOvTime = curOvTime; 79 isHover = false; 80 setTimeout(function() { 81 if (!isHover) { 82 outCallback && outCallback(e, elem); 83 } 84 }, 10); 85 } 86 bind(elem, "mouseover", over); 87 bind(elem, "mouseout", out); 88 }; 89 90 91 92 93 function init() { 94 95 var navs = document.getElementsByClassName('sliding-nav'); 96 97 for (var i = 0; i < navs.length; i++) { 98 99 100 //建立一個DIV與當前導航豎向對齊 101 var indi = document.createElement("div"); 102 indi.id = "slna-indicator" 103 104 indi.style.borderRadius = navs[i].getAttribute("sn-radius") || "0px" 105 indi.style.height = navs[i].getAttribute("sn-height") || "3px" 106 indi.style.backgroundColor = navs[i].getAttribute("sn-color") || "#F00" 107 108 indi.style.position = "absolute" 109 indi.style.transition = "0.5s" 110 111 //查找當前子菜單項,若是有類名active或者是selected就視爲當前項,若是沒有使用第1項 112 var selected = navs[i].getElementsByClassName('active') 113 if (selected.length == 0) { 114 selected = navs[i].getElementsByClassName('selected') 115 } 116 if (selected.length == 0) { 117 selected = navs[i].children 118 } 119 120 if (selected.length == 0) { 121 throw Error('Sorry, Navigation bar has no item at all!'); 122 } 123 124 selected = selected[0]; 125 126 indi.style.width = selected.offsetWidth + "px"; 127 indi.style.top = selected.offsetHeight + "px"; 128 indi.style.left = selected.offsetLeft + "px"; 129 navs[i].parentElement.insertBefore(indi, navs[i]); 130 131 for (var j = 0; j < navs[i].children.length; j++) { 132 133 hover(navs[i].children[j], function(e, elem) { 134 135 indi.style.width = elem.offsetWidth + "px"; 136 indi.style.left = elem.offsetLeft + "px"; 137 138 }); 139 140 //移出導航就恢復默認 141 hover(navs[i], null, function(e, elem) { 142 indi.style.width = selected.offsetWidth + "px"; 143 indi.style.left = selected.offsetLeft + "px"; 144 }); 145 146 } 147 148 149 150 151 } 152 153 }