这篇文章将为大家详细讲解有关使用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="">千千音乐</a><span></span><audio src=" rel="external nofollow" source/a1.mp3"></audio></li>
<li><a href="">echo回声</a><span></span><audio src=" rel="external nofollow" source/a2.mp3"></audio></li>
<li><a href="">酷狗音乐</a><span></span><audio src=" rel="external nofollow" source/a3.mp3"></audio></li>
<li><a href="">QQ音乐</a><span></span><audio src=" rel="external nofollow" source/a4.mp3"></audio></li>
<li><a href="">酷我音乐</a><span></span><audio src=" rel="external nofollow" source/a5.mp3"></audio></li>
<li><a href="">网易云音乐</a><span></span><audio src=" rel="external nofollow" source/a6.mp3"></audio></li>
<li><a href="">虾米音乐</a><span></span><audio src=" rel="external nofollow" source/a7.mp3"></audio></li>
</ul>
</nav>
<script src="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制作一个简单的音乐导航功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。