JavaScript實(shí)現(xiàn)音樂(lè)導(dǎo)航效果
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)音樂(lè)導(dǎo)航效果的具體代碼,供大家參考,具體內(nèi)容如下
效果展示
鼠標(biāo)在導(dǎo)航欄上移動(dòng),每一項(xiàng)發(fā)出一種音符(do re mi fa so la xi),同樣鍵盤(pán)上的1-7數(shù)字也可以有同樣的效果。
資源下載
音樂(lè)導(dǎo)航(緩動(dòng)動(dòng)畫(huà)、會(huì)唱歌的導(dǎo)航)
代碼
index.html
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>音樂(lè)導(dǎo)航</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 標(biāo)簽填充整個(gè) 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.cgvv.com.cn/bcjs/14309.html'>千千音樂(lè)</a><span></span><audio src='http://www.cgvv.com.cn/bcjs/ rel='external nofollow' source/a1.mp3'></audio></li> <li><a href='http://www.cgvv.com.cn/bcjs/14309.html'>echo回聲</a><span></span><audio src='http://www.cgvv.com.cn/bcjs/ rel='external nofollow' source/a2.mp3'></audio></li> <li><a href='http://www.cgvv.com.cn/bcjs/14309.html'>酷狗音樂(lè)</a><span></span><audio src='http://www.cgvv.com.cn/bcjs/ rel='external nofollow' source/a3.mp3'></audio></li> <li><a href='http://www.cgvv.com.cn/bcjs/14309.html'>QQ音樂(lè)</a><span></span><audio src='http://www.cgvv.com.cn/bcjs/ rel='external nofollow' source/a4.mp3'></audio></li> <li><a href='http://www.cgvv.com.cn/bcjs/14309.html'>酷我音樂(lè)</a><span></span><audio src='http://www.cgvv.com.cn/bcjs/ rel='external nofollow' source/a5.mp3'></audio></li> <li><a href='http://www.cgvv.com.cn/bcjs/14309.html'>網(wǎng)易云音樂(lè)</a><span></span><audio src='http://www.cgvv.com.cn/bcjs/ rel='external nofollow' source/a6.mp3'></audio></li> <li><a href='http://www.cgvv.com.cn/bcjs/14309.html'>蝦米音樂(lè)</a><span></span><audio src='http://www.cgvv.com.cn/bcjs/ rel='external nofollow' source/a7.mp3'></audio></li> </ul> </nav><script src='http://www.cgvv.com.cn/bcjs/js/myFunc.js'></script><script> window.onload = function () { // 1.獲取標(biāo)簽 var ul = $('ul'); var allLis = ul.children; // 2.監(jiān)聽(tīng)鼠標(biāo)進(jìn)入 li 標(biāo)簽 for(var i=0; i<allLis.length; i++){ allLis[i].onmouseover = function () { // 2.1 緩動(dòng)動(dòng)畫(huà) buffer(this.children[1], {'top': 0}); // 2.2 播放音符 this.children[2].play(); this.children[2].currentTime = 0; }; // 2.3 監(jiān)聽(tīng)鼠標(biāo)離開(kāi) allLis[i].onmouseout = function () { buffer(this.children[1], {'top': 40}); }; // 3.監(jiān)聽(tīng)鍵盤(pán)的點(diǎn)擊(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;}/** * 緩動(dòng)動(dòng)畫(huà)函數(shù) * @param obj * @param json * @param fn */function buffer(obj, json, fn) { // 1.1 清除定時(shí)器 clearInterval(obj.timer); // 1.3 設(shè)置定時(shí)器 var begin = 0, target = 0, speed = 0; obj.timer = setInterval(function () { // 1.3.0 標(biāo)記 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 求出步長(zhǎng) // 緩動(dòng)動(dòng)畫(huà)原理:盒子本身的位置 + 步長(zhǎng)(不斷變化的,由大變小) // 步長(zhǎng):begin = begin + (end - begin) * 緩動(dòng)系數(shù) speed = (target - begin) * 0.2; // 1.6 判斷是否向上取整 speed = (target > begin) ? Math.ceil(speed) : Math.floor(speed); // 1.5 移動(dòng)起來(lái) 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 清除定時(shí)器 if(flag){ clearInterval(obj.timer); // 判斷有沒(méi)有回調(diào)函數(shù) if(fn){ fn() } } }, 20)}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 利用CSS3新特性創(chuàng)建透明邊框三角2. PHP循環(huán)與分支知識(shí)點(diǎn)梳理3. 讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題4. 解析原生JS getComputedStyle5. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)6. css代碼優(yōu)化的12個(gè)技巧7. 前端從瀏覽器的渲染到性能優(yōu)化8. ASP基礎(chǔ)入門(mén)第三篇(ASP腳本基礎(chǔ))9. ASP實(shí)現(xiàn)加法驗(yàn)證碼10. 無(wú)線標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)
