您的位置:首页技术文章
文章详情页

JavaScript实现音乐导航效果

【字号: 日期:2023-06-14 17:47:32浏览:16作者:猪猪

本文实例为大家分享了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='https://www.haobala.com/bcjs/14309.html'>千千音乐</a><span></span><audio src='https://www.haobala.com/bcjs/ rel='external nofollow' source/a1.mp3'></audio></li> <li><a href='https://www.haobala.com/bcjs/14309.html'>echo回声</a><span></span><audio src='https://www.haobala.com/bcjs/ rel='external nofollow' source/a2.mp3'></audio></li> <li><a href='https://www.haobala.com/bcjs/14309.html'>酷狗音乐</a><span></span><audio src='https://www.haobala.com/bcjs/ rel='external nofollow' source/a3.mp3'></audio></li> <li><a href='https://www.haobala.com/bcjs/14309.html'>QQ音乐</a><span></span><audio src='https://www.haobala.com/bcjs/ rel='external nofollow' source/a4.mp3'></audio></li> <li><a href='https://www.haobala.com/bcjs/14309.html'>酷我音乐</a><span></span><audio src='https://www.haobala.com/bcjs/ rel='external nofollow' source/a5.mp3'></audio></li> <li><a href='https://www.haobala.com/bcjs/14309.html'>网易云音乐</a><span></span><audio src='https://www.haobala.com/bcjs/ rel='external nofollow' source/a6.mp3'></audio></li> <li><a href='https://www.haobala.com/bcjs/14309.html'>虾米音乐</a><span></span><audio src='https://www.haobala.com/bcjs/ rel='external nofollow' source/a7.mp3'></audio></li> </ul> </nav><script src='https://www.haobala.com/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)}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持好吧啦网。

标签: JavaScript
相关文章: