JavaScript實現音樂導航效果
本文實例為大家分享了JavaScript實現音樂導航效果的具體代碼,供大家參考,具體內容如下
效果展示
鼠標在導航欄上移動,每一項發出一種音符(do re mi fa so la xi),同樣鍵盤上的1-7數字也可以有同樣的效果。
資源下載
音樂導航(緩動動畫、會唱歌的導航)
代碼
index.html
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>音樂導航</title> <style> * { margin: 0; padding: 0; list-style: none; border: 0; } #nav { width: 706px; height: 40px; border: 1px solid #ccc; margin: 100px auto; overflow: hidden; } #nav ul { width: 710px; } #nav ul li { float: left; width: 100px; text-align: center; line-height: 40px; border-right: 1px dashed #ccc; position: relative; } #nav ul li a { /* a 標簽填充整個 li */ width: 100%; height: 100%; display: inline-block; } a { text-decoration: none; color: #000000; } span { width: 100px; height: 40px; background: skyblue; position: absolute; left: 0; top: 40px; z-index: -1; } </style></head><body> <nav id='nav'> <ul id='ul'> <li><a href='http://www.aoyou183.cn/bcjs/14309.html'>千千音樂</a><span></span><audio src='http://www.aoyou183.cn/bcjs/ rel='external nofollow' source/a1.mp3'></audio></li> <li><a href='http://www.aoyou183.cn/bcjs/14309.html'>echo回聲</a><span></span><audio src='http://www.aoyou183.cn/bcjs/ rel='external nofollow' source/a2.mp3'></audio></li> <li><a href='http://www.aoyou183.cn/bcjs/14309.html'>酷狗音樂</a><span></span><audio src='http://www.aoyou183.cn/bcjs/ rel='external nofollow' source/a3.mp3'></audio></li> <li><a href='http://www.aoyou183.cn/bcjs/14309.html'>QQ音樂</a><span></span><audio src='http://www.aoyou183.cn/bcjs/ rel='external nofollow' source/a4.mp3'></audio></li> <li><a href='http://www.aoyou183.cn/bcjs/14309.html'>酷我音樂</a><span></span><audio src='http://www.aoyou183.cn/bcjs/ rel='external nofollow' source/a5.mp3'></audio></li> <li><a href='http://www.aoyou183.cn/bcjs/14309.html'>網易云音樂</a><span></span><audio src='http://www.aoyou183.cn/bcjs/ rel='external nofollow' source/a6.mp3'></audio></li> <li><a href='http://www.aoyou183.cn/bcjs/14309.html'>蝦米音樂</a><span></span><audio src='http://www.aoyou183.cn/bcjs/ rel='external nofollow' source/a7.mp3'></audio></li> </ul> </nav><script src='http://www.aoyou183.cn/bcjs/js/myFunc.js'></script><script> window.onload = function () { // 1.獲取標簽 var ul = $('ul'); var allLis = ul.children; // 2.監聽鼠標進入 li 標簽 for(var i=0; i<allLis.length; i++){ allLis[i].onmouseover = function () { // 2.1 緩動動畫 buffer(this.children[1], {'top': 0}); // 2.2 播放音符 this.children[2].play(); this.children[2].currentTime = 0; }; // 2.3 監聽鼠標離開 allLis[i].onmouseout = function () { buffer(this.children[1], {'top': 40}); }; // 3.監聽鍵盤的點擊(1-7分別代表 do re mi fa so la xi) document.onkeydown = function (event) { var e = event || window.event; // console.log(e.keyCode); var keyCode = e.keyCode -49; buffer(allLis[keyCode].children[1], {'top': 0}, function () { buffer(allLis[keyCode].children[1], {'top': 40}) }) // 2.2 播放音符 allLis[keyCode].children[2].play(); allLis[keyCode].children[2].currentTime = 0; } } }</script></body></html>
js/myFunc.js
function $(id) { return typeof id === 'string' ? document.getElementById(id) : null;}/** * 緩動動畫函數 * @param obj * @param json * @param fn */function buffer(obj, json, fn) { // 1.1 清除定時器 clearInterval(obj.timer); // 1.3 設置定時器 var begin = 0, target = 0, speed = 0; obj.timer = setInterval(function () { // 1.3.0 標記 var flag = true; for(var k in json){ // 1.3.1 求出初始值 if('opacity' === k){ // 透明度 console.log(getCssStyleAttr(obj, k)); begin = Math.round(parseFloat(getCssStyleAttr(obj, k)) * 100) || 100; // 獲取 CSS 樣式值 target = parseInt(json[k] * 100); }else if('scrollTop' === k){ begin = Math.ceil(obj.scrollTop); target = parseInt(json[k]); }else { // 其他情況 begin = parseInt(getCssStyleAttr(obj, k)) || 0; // 獲取 CSS 樣式值 target = parseInt(json[k]); } // console.log(begin, target); // 1.4 求出步長 // 緩動動畫原理:盒子本身的位置 + 步長(不斷變化的,由大變小) // 步長:begin = begin + (end - begin) * 緩動系數 speed = (target - begin) * 0.2; // 1.6 判斷是否向上取整 speed = (target > begin) ? Math.ceil(speed) : Math.floor(speed); // 1.5 移動起來 if('opacity' === k){ // 透明度 // w3c 的瀏覽器 obj.style.opacity = (begin + speed) / 100; // ie obj.style.filter = 'alpha(opacity=' + (begin + speed) +')'; }else if('scrollTop' === k){ obj.scrollTop = begin + speed; }else { obj.style[k] = begin + speed + 'px'; } // 1.7 判斷 if(begin !== target){ flag = false; } } // 1.8 清除定時器 if(flag){ clearInterval(obj.timer); // 判斷有沒有回調函數 if(fn){ fn() } } }, 20)}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: