DDR爱好者之家 Design By 杰米
本文实例为大家分享了JS实现音乐播放器的具体代码,供大家参考,具体内容如下
首先,使用HTML搭好我们的框架结构。这一步为了提高我们代码的可读性,多写注释。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>原生JS音乐播放器</title> <link rel="stylesheet" href="css/public.css" > <link rel="stylesheet" href="css/css.css" > </head> <body> <!-- 音乐播放器 --> <div class="music"> <!-- 歌曲信息 --> <div class="word"> <div class="clears"></div> <!-- 歌曲封面 --> <div class="img"> <img src="/UploadFiles/2021-04-02/1.jpg">第二步,搭好框架后,就用CSS给他写上你喜欢的样式。注意这里我是将样式分为了公共样式和独立样式分开写的,提高代码可重复利用,当然你也可以使用SASS更方便,这里我只提供了独立样式,公共样式可以在网上自主寻找。
html { background: #92b991; } .music { width: 670px; height: 400px; background: url("../images/bg.jpg") no-repeat center; margin-left: auto; margin-right: auto; border: 1px #3d5e59 solid; margin-top: 150px; border-radius: 20px; box-shadow: 0 0 20px rgba(0, 0, 0, 0.3); position: relative; } .img { width: 200px; height: 200px; border: 2px #fff solid; overflow: hidden; border-radius: 100%; margin-top: 50px; margin-left: 35%; } .mtv { width: 670px; position: relative; } .text { color: #fff; margin-right: 30px; margin-left: 30px; margin-bottom: 10px; } .text>h1 { font-size: 24px; line-height: 30px; font-weight: normal; } .text>h2 { font-family: "Microsoft YaHei"; font-size: 14px; font-weight: 300; line-height: 1.7; } .vol01 { position: absolute; height: 30px; width: 120px; right: 30px; top: -8%; } .volPro { width: 100%; height: 5px; margin-top: 44.5px; border: 1px #fff solid; border-radius: 5px; } .volColor { height: 5px; width: 100%; color: #fff; pointer-events: none; } .volBlock { pointer-events: none; position: absolute; top: 42px; width: 10px; height: 10px; border: 1px #fff solid; background: #f4f3f3; border-radius: 100%; left: 100%; margin-left: -5px; } .volTop { color: #fff; font-size: 10px; position: absolute; top: 25px; right: 0; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } .vImg { position: absolute; top: 38px; left: -26px; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } .time { font-family: "Microsoft YaHei"; font-size: 10px; line-height: 1.5; color: #fff; margin-top: 25px; } .cTime { float: left; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } .tTime { float: right; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } .progress { margin-left: 30px; margin-right: 30px; } .proBar { width: 100%; height: 2px; overflow: hidden; background: #B292FF; margin-top: 5px; border-radius: 5px; } .proColor { height: 7px; width: 30%; background: #fff; pointer-events: none; } .ctrls { text-align: center; margin-top: 20px; margin-left: 30px; margin-right: 30px; } .btn { position: absolute; top: 50px; } .nextPrev { right: 0; margin-left: 200px; opacity: 0.1; transition: all 0.3s; } .nextPrev:hover { opacity: 0.5; } .prevBtn { left: 0; opacity: 0.1; transition: all 0.3s; } .prevBtn:hover { opacity: 0.5; } .playBtn { height: 200px; width: 200px; border-radius: 200px; opacity: 0.3; position: absolute; top: 52px; left: 250px; margin-left: -14px; transition: all 0.3s; } .playBtn:hover { opacity: 0.5; background: #46847b; }第三步,也是让音乐播放器具有灵魂的东西,使用JS添加功能。
1.获取ID(当然这里也可以获取类名或是元素,看你自己的习惯)。
let music = document.getElementById("music"); let playBtn = document.getElementById("playBtn"); let prevBtn = document.getElementById("prevBtn"); let nextBtn = document.getElementById("nextBtn"); let img = document.getElementById("img"); let sName = document.getElementById("sName"); let singer = document.getElementById("singer"); let tTime = document.getElementById("tTime"); let proBar = document.getElementById("proBar"); let cTime = document.getElementById("cTime"); let songPro = document.getElementById("songPro"); let vol = document.getElementById("vol"); let volCol = document.getElementById("volCol"); let volB = document.getElementById("volB"); let volText = document.getElementById("volText");2.添加音乐播放功能。
导入音乐
let songs = [{ mp3: "music/01.mp3", singer: "赵薇", name: "不能和你分手", img: "images/1.jpg" }, { mp3: "music/04.mp3", singer: "陈粒", name: "易燃易爆炸", img: "images/2.jpg" }, { mp3: "music/06.mp3", singer: "胡夏/郁可唯", name: "知否知否", img: "images/3.jpg" }];音乐播放
let changeMusic = function(index) { music.src = songs[index].mp3; img.src = songs[index].img; sName.innerHTML = songs[index].name; singer.innerHTML = songs[index].singer; proBar.style.width = 0; }; let index = 0; changeMusic(index);音乐播放暂停
playBtn.addEventListener("click", function(event) { if (music.paused) { music.play(); event.currentTarget.innerHTML = '<img src="/UploadFiles/2021-04-02/暂停.png">音乐切换
prevBtn.addEventListener("click", function(event) { //上一首 index--; if (index <= -1) { index = songs.length - 1; } changeMusic(index); }); nextBtn.addEventListener("click", function(event) { //下一首 index++; if (index > songs.length - 1) { index = 0; } changeMusic(index); }); music.addEventListener('ended',function () { //自动播放下一首 index++; if (index > songs.length - 1) { index = 0; } changeMusic(index); })3.添加进度条。
music.addEventListener("loadedmetadata", function(event) { //提示音频的元数据已加载 tTime.innerHTML = parseInt(music.duration / 60) + ":" + parseInt(music.duration % 60); }); music.addEventListener("timeupdate", function(event) { //监听音乐事实播放事件 let jd = music.currentTime / music.duration; let bfb = jd * 100 + "%"; proBar.style.width = bfb; if (music.currentTime < 10) { cTime.innerHTML = "0:0" + Math.floor(music.currentTime); } else if (music.currentTime < 60) { cTime.innerHTML = "0:" + Math.floor(music.currentTime); } else { let minet = parseInt(music.currentTime / 60); let sec = music.currentTime - minet * 60; if (sec < 10) { cTime.innerHTML = "0" + minet + ":" + "0" + parseInt(sec); } else { cTime.innerHTML = "0" + minet + ":" + parseInt(sec); } } });小功能:点击进度条,音乐也跟着变化跳转到指定的时间。
songPro.addEventListener("click", function(event) { let x = event.offsetX; let bfb = x / 610 * 100; proBar.style.width = bfb + "%"; music.currentTime = music.duration * bfb / 100; });4.添加音量调节模块。
let getBfb = function(event) { let x = event.clientX; //返回当事件被触发时鼠标指针向对于浏览器页面(或客户区)的水平坐标。 let volX = vol.getBoundingClientRect().x; //getBoundingClientRect()获取元素的宽高位置 let disX = x - volX; disX = Math.max(0, disX); //返回最大数字 disX = Math.min(120, disX); //返回最小数字 return disX / 120; }; let setVol = function(event) { let bfb = Math.floor(getBfb(event) * 10000) / 100; volCol.style.width = bfb + "%"; volB.style.left = bfb + "%"; volText.innerHTML = Math.floor(bfb) + "%"; music.volume = bfb / 100; }; vol.addEventListener("mousedown", function() { document.addEventListener("mousemove", setVol); }); document.addEventListener("mouseup", function() { document.removeEventListener("mousemove", setVol); });全部JS代码
这里我使用了{}作用域,让let 或 const声明的变量只在 let 或 const命令所在的代码块 {} 内有效,在 {} 之外不能访问(ECMAScript 6(简称ES6)中新增的)。
{ let music = document.getElementById("music"); let playBtn = document.getElementById("playBtn"); let prevBtn = document.getElementById("prevBtn"); let nextBtn = document.getElementById("nextBtn"); let img = document.getElementById("img"); let sName = document.getElementById("sName"); let singer = document.getElementById("singer"); let tTime = document.getElementById("tTime"); let proBar = document.getElementById("proBar"); let cTime = document.getElementById("cTime"); let songPro = document.getElementById("songPro"); let vol = document.getElementById("vol"); let volCol = document.getElementById("volCol"); let volB = document.getElementById("volB"); let volText = document.getElementById("volText"); let songs = [{ mp3: "music/01.mp3", singer: "赵薇", name: "不能和你分手", img: "images/1.jpg" }, { mp3: "music/04.mp3", singer: "陈粒", name: "易燃易爆炸", img: "images/2.jpg" }, { mp3: "music/06.mp3", singer: "胡夏/谭维维", name: "知否知否", img: "images/3.jpg" }]; let changeMusic = function(index) { music.src = songs[index].mp3; img.src = songs[index].img; sName.innerHTML = songs[index].name; singer.innerHTML = songs[index].singer; proBar.style.width = 0; }; let index = 0; changeMusic(index); playBtn.addEventListener("click", function(event) { if (music.paused) { music.play(); event.currentTarget.innerHTML = '<img src="/UploadFiles/2021-04-02/暂停.png">最终效果展示:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月23日
2024年11月23日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]