当前位置:   article > 正文

网站音乐播放器源码_网页音乐播放器源码

网页音乐播放器源码

一起白嫖,简要快速的建立一个网站音乐播放器,在我研究一番之后直接给大家总结出结果

还未完全美化好的案例图

一共四个文件

index.html 用于写页面

data.js  音乐地址声明

app.js 主要js操作,事件绑定等

style.css  样式表

上述的四个文件名可随意更改 看自己喜好

以及对应音乐和图片等放在对应路径下,也可以自己改,琢磨下就能看懂

index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>psh</title>
  7. <link rel="stylesheet" type="text/css" href="../static/css/vide.css">
  8. <body>
  9. <audio src="" id="audio-source"></audio>
  10. <section class="home-section">
  11. <div class="carousel">
  12. <img src="../static/img/xx1.jpg" class="active" alt="">
  13. <img src="../static/img/xx2.jpg" alt="">
  14. <img src="../static/img/xx3.jpg" alt="">
  15. <img src="../static/img/xx4.jpg" alt="">
  16. <img src="../static/img/xx5.jpg" alt="">
  17. </div>
  18. <h1 class="heading">最近播放</h1>
  19. <div class="playlists-group">
  20. <div class="playlist-card">
  21. <img src="../static/img/xx2.jpg" class="playlist-card-img" alt="">
  22. <p class="playlist-card-name">华语热歌</p>
  23. </div>
  24. <div class="playlist-card">
  25. <img src="../static/img/xx2.jpg" class="playlist-card-img" alt="">
  26. <p class="playlist-card-name">古风戏腔</p>
  27. </div>
  28. //+3
  29. </div>
  30. <h1 class="heading">根据你的喜好</h1>
  31. <div class="playlists-group">
  32. <div class="playlist-card">
  33. <img src="../static/img/xx2.jpg" class="playlist-card-img" alt="">
  34. <p class="playlist-card-name">失恋回忆</p>
  35. </div>
  36. <div class="playlist-card">
  37. <img src="../static/img/xx2.jpg" class="playlist-card-img" alt="">
  38. <p class="playlist-card-name">经典老歌</p>
  39. </div>
  40. //+3
  41. </div>
  42. </section>
  43. <section class="music-player-section ">
  44. <img src="../static/img/xx1.jpg" width="50px" class="back-btn icon hide" alt="">
  45. <img src="../static/img/xx5.jpg" width="50px" class="nav-btn icon hide" alt="">
  46. <h1 class="current-song-name">song 1</h1>
  47. <p class="artist-name hide">artist 1</p>
  48. <img src="../static/img/xx5.jpg" class="cover hide" alt="">
  49. <div class="seek-bar-container">
  50. <input type="range" class="music-seek-bar" value="0">
  51. <p class="current-time hide">00 : 00</p>
  52. <p class="duration hide">00 : 00</p>
  53. </div>
  54. <div class="controls">
  55. <span class="fas fa-redo"></span>
  56. <div class="main">
  57. <i class="fas fa-backward active"></i>
  58. <i class="fas fa-play active"></i>
  59. <i class="fas fa-pause"></i>
  60. <i class="fas fa-forward active"></i>
  61. </div>
  62. <input type="range" class="volume-slider" max="1" value="1" step="0.1">
  63. <span class="fas fa-volume-up"></span>
  64. </div>
  65. </section>
  66. <section class="playlist ">
  67. <img src="../static/img/xx1.jpg" width="50px" class="back-btn icon" alt="">
  68. <h1 class="title">播放列表</h1>
  69. <div class="queue active">
  70. <div class="queue-cover">
  71. <img src="../static/img/xx1.jpg" alt="">
  72. <i class="fas fa-pause"></i>
  73. </div>
  74. <p class="name">song 1</p>
  75. </div>
  76. // +7
  77. </section>
  78. <script type="application/javascript" src="../static/js/data.js"></script>
  79. <script type="application/javascript" src="../static/js/app.js"></script>
  80. </body>
  81. </html>

data.js

  1. let songs = [
  2. {
  3. name: '歌曲名字',
  4. path: '歌曲相对路径',
  5. artist: '歌手名字',
  6. cover: '封面图片相对路径'
  7. },
  8. //以下格式相同,自己添加想要的歌曲
  9. ]

app.js

  1. const carousel = [...document.querySelectorAll('.carousel img')];
  2. let carouselImageIndex = 0;
  3. const changeCarousel = () => {
  4. carousel[carouselImageIndex].classList.toggle('active');
  5. if(carouselImageIndex >= carousel.length - 1){
  6. carouselImageIndex = 0;
  7. } else{
  8. carouselImageIndex++;
  9. }
  10. carousel[carouselImageIndex].classList.toggle('active');
  11. }
  12. setInterval(() => {
  13. changeCarousel();
  14. }, 3000);
  15. //导航
  16. const musicPlayerSection = document.querySelector('.music-player-section');
  17. let clickCount = 1;
  18. musicPlayerSection.addEventListener('click', () => {
  19. if(clickCount >= 2){
  20. musicPlayerSection.classList.add('active');
  21. clickCount = 1;
  22. return;
  23. }
  24. clickCount++;
  25. setTimeout(() => {
  26. clickCount = 1;
  27. }, 250);
  28. })
  29. /// 从音乐播放器返回
  30. const backToHomeBtn = document.querySelector('.music-player-section .back-btn');
  31. backToHomeBtn.addEventListener('click', () => {
  32. musicPlayerSection.classList.remove('active');
  33. })
  34. /// 访问播放列表
  35. const playlistSection = document.querySelector('.playlist');
  36. const navBtn = document.querySelector('.music-player-section .nav-btn');
  37. navBtn.addEventListener('click', () => {
  38. playlistSection.classList.add('active');
  39. })
  40. // 从播放列表返回到音乐播放器
  41. const backToMusicPlayer = document.querySelector('.playlist .back-btn');
  42. backToMusicPlayer.addEventListener('click', () => {
  43. playlistSection.classList.remove('active');
  44. })
  45. //导航完成
  46. /// 音乐
  47. let currentMusic = 0;
  48. const music = document.querySelector('#audio-source');
  49. const seekBar = document.querySelector('.music-seek-bar');
  50. const songName = document.querySelector('.current-song-name');
  51. const artistName = document.querySelector('.artist-name');
  52. const coverImage = document.querySelector('.cover');
  53. const currentMusicTime = document.querySelector('.current-time');
  54. const musicDuration = document.querySelector('.duration');
  55. const queue = [...document.querySelectorAll('.queue')];
  56. // 在此处选择所有按钮
  57. const forwardBtn = document.querySelector('i.fa-forward');
  58. const backwardBtn = document.querySelector('i.fa-backward');
  59. const playBtn = document.querySelector('i.fa-play');
  60. const pauseBtn = document.querySelector('i.fa-pause');
  61. const repeatBtn = document.querySelector('span.fa-redo');
  62. const volumeBtn = document.querySelector('span.fa-volume-up');
  63. const volumeSlider = document.querySelector('.volume-slider');
  64. // 音乐设置功能
  65. const setMusic = (i) => {
  66. seekBar.value = 0;
  67. let song = songs[i];
  68. currentMusic = i;
  69. music.src = song.path;
  70. songName.innerHTML = song.name;
  71. artistName.innerHTML = song.artist;
  72. coverImage.src = song.cover;
  73. setTimeout(() => {
  74. seekBar.max = music.duration;
  75. musicDuration.innerHTML = formatTime(music.duration);
  76. }, 300);
  77. currentMusicTime.innerHTML = '00 : 00';
  78. queue.forEach(item => item.classList.remove('active'));
  79. queue[currentMusic].classList.add('active');
  80. }
  81. setMusic(0);
  82. // 格式持续时间为 00 : 00 格式
  83. const formatTime = (time) => {
  84. let min = Math.floor(time / 60);
  85. if(min < 10){
  86. min = `0` + min;
  87. }
  88. let sec = Math.floor(time % 60);
  89. if(sec < 10){
  90. sec = `0` + sec;
  91. }
  92. return `${min} : ${sec}`;
  93. }
  94. // playBtn 点击事件
  95. playBtn.addEventListener('click', () => {
  96. music.play();
  97. playBtn.classList.remove('active');
  98. pauseBtn.classList.add('active');
  99. })
  100. // pauseBtn 点击事件
  101. pauseBtn.addEventListener('click', () => {
  102. music.pause();
  103. pauseBtn.classList.remove('active');
  104. playBtn.classList.add('active');
  105. })
  106. // 向前按钮
  107. forwardBtn.addEventListener('click', () => {
  108. if(currentMusic >= songs.length - 1){
  109. currentMusic = 0;
  110. } else{
  111. currentMusic++;
  112. }
  113. setMusic(currentMusic);
  114. playBtn.click();
  115. })
  116. // 后退按钮
  117. backwardBtn.addEventListener('click', () => {
  118. if(currentMusic <= 0){
  119. currentMusic = songs.length - 1;
  120. } else{
  121. currentMusic--;
  122. }
  123. setMusic(currentMusic);
  124. playBtn.click();
  125. })
  126. // 搜索栏事件
  127. setInterval(() => {
  128. seekBar.value = music.currentTime;
  129. currentMusicTime.innerHTML = formatTime(music.currentTime);
  130. if(Math.floor(music.currentTime) == Math.floor(seekBar.max)){
  131. if(repeatBtn.className.includes('active')){
  132. setMusic(currentMusic);
  133. playBtn.click();
  134. } else{
  135. forwardBtn.click();
  136. }
  137. }
  138. }, 500)
  139. seekBar.addEventListener('change', () => {
  140. music.currentTime = seekBar.value;
  141. })
  142. // 刷新按钮
  143. repeatBtn.addEventListener('click', () => {
  144. repeatBtn.classList.toggle('active');
  145. })
  146. // 音量部分
  147. volumeBtn.addEventListener('click', () => {
  148. volumeBtn.classList.toggle('active');
  149. volumeSlider.classList.toggle('active');
  150. })
  151. volumeSlider.addEventListener('input', () => {
  152. music.volume = volumeSlider.value;
  153. })
  154. queue.forEach((item, i) => {
  155. item.addEventListener('click', () => {
  156. setMusic(i);
  157. playBtn.click();
  158. })
  159. })

style.css

  1. /*播放器*/
  2. @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700;900&display=swap');
  3. *{
  4. margin: 0;
  5. padding: 0;
  6. box-sizing: border-box;
  7. }
  8. :root{
  9. --background: #141414;
  10. --text-color: #fff;
  11. --primary-color: #63ff69;
  12. --secondary-color: #000;
  13. --alpha-color: rgba(0, 0, 0, 0.5);
  14. --shadow: 0 15px 40px var(--alpha-color);
  15. }
  16. html{
  17. background: var(--background);
  18. display: flex;
  19. justify-content: center;
  20. }
  21. body{
  22. width: 100%;
  23. height: 100vh;
  24. max-width: 375px;
  25. position: relative;
  26. background: var(--background);
  27. font-family: 'roboto', sans-serif;
  28. color: var(--text-color);
  29. }
  30. ::-webkit-scrollbar{
  31. display: none;
  32. }
  33. .home-section{
  34. width: 100%;
  35. padding: 20px;
  36. height: 100%;
  37. padding-bottom: 100px;
  38. overflow-y: auto;
  39. }
  40. /* carousel */
  41. .carousel{
  42. width: 100%;
  43. height: 200px;
  44. overflow: hidden;
  45. border-radius: 20px;
  46. box-shadow: var(--shadow);
  47. position: relative;
  48. }
  49. .carousel img{
  50. position: absolute;
  51. width: 100%;
  52. height: 100%;
  53. object-fit: cover;
  54. opacity: 0;
  55. transition: 1s;
  56. }
  57. .carousel img.active{
  58. opacity: 1;
  59. }
  60. .heading{
  61. margin: 30px 0 10px;
  62. text-transform: capitalize;
  63. font-weight: 400;
  64. font-size: 30px;
  65. }
  66. /* playlists card */
  67. .playlists-group{
  68. position: relative;
  69. width: 100%;
  70. min-height: 200px;
  71. height: auto;
  72. display: flex;
  73. flex-wrap: nowrap;
  74. overflow-x: auto;
  75. }
  76. .playlist-card{
  77. flex: 0 0 auto;
  78. max-width: 150px;
  79. height: 100%;
  80. margin-right: 20px;
  81. }
  82. .playlist-card-img{
  83. width: 100%;
  84. height: 150px;
  85. object-fit: cover;
  86. border-radius: 20px;
  87. }
  88. .playlist-card-name{
  89. width: 100%;
  90. text-align: justify;
  91. font-size: 20px;
  92. text-transform: capitalize;
  93. padding: 5px;
  94. }
  95. .music-player-section{
  96. width: 100%;
  97. height: 100px;
  98. position: fixed;
  99. bottom: 0;
  100. left: 0;
  101. background: var(--alpha-color);
  102. backdrop-filter: blur(50px);
  103. transition: 1s;
  104. }
  105. .music-seek-bar{
  106. -webkit-appearance: none;
  107. width: 100%;
  108. position: absolute;
  109. top: -4px;
  110. height: 8px;
  111. background: var(--secondary-color);
  112. overflow: hidden;
  113. }
  114. .music-seek-bar::-webkit-slider-thumb{
  115. -webkit-appearance: none;
  116. height: 10px;
  117. width: 5px;
  118. background: var(--primary-color);
  119. cursor: pointer;
  120. box-shadow: -400px 0 0 400px var(--primary-color);
  121. }
  122. .current-song-name{
  123. font-weight: 300;
  124. font-size: 20px;
  125. text-align: center;
  126. margin-top: 5px;
  127. text-transform: capitalize;
  128. }
  129. .controls{
  130. position: relative;
  131. width: 80%;
  132. margin: auto;
  133. display: flex;
  134. justify-content: center;
  135. align-items: center;
  136. height: 60px;
  137. font-size: 30px;
  138. }
  139. .controls span{
  140. display: none;
  141. opacity: 0;
  142. transition: 1s;
  143. }
  144. .music-player-section.active .controls{
  145. justify-content: space-between;
  146. }
  147. .music-player-section.active .controls span{
  148. font-size: 25px;
  149. display: block;
  150. opacity: 0.5;
  151. }
  152. .music-player-section.active .controls span.active{
  153. color: var(--primary-color);
  154. opacity: 1;
  155. }
  156. .controls .main i{
  157. margin: 0 5px;
  158. display: none;
  159. }
  160. .controls .main i.active{
  161. display: inline;
  162. }
  163. .music-player-section .hide{
  164. display: none;
  165. opacity: 0;
  166. transition: 1s;
  167. }
  168. .music-player-section.active .hide{
  169. display: block;
  170. opacity: 1;
  171. }
  172. .music-player-section.active{
  173. width: 100%;
  174. height: 100%;
  175. padding: 30px;
  176. display: flex;
  177. flex-direction: column;
  178. }
  179. .music-player-section.active .music-seek-bar{
  180. position: relative;
  181. display: block;
  182. border-radius: 50px;
  183. margin: auto;
  184. }
  185. .music-player-section.active .current-song-name{
  186. font-size: 40px;
  187. }
  188. .music-player-section.active .controls{
  189. width: 100%;
  190. font-size: 50px;
  191. }
  192. .artist-name{
  193. text-align: center;
  194. font-size: 20px;
  195. text-transform: capitalize;
  196. }
  197. .cover{
  198. width: 30vh;
  199. height: 30vh;
  200. object-fit: cover;
  201. margin: auto;
  202. border-radius: 20px;
  203. box-shadow: var(--shadow);
  204. }
  205. .current-time{
  206. position: absolute;
  207. margin-top: 5px;
  208. left: 30px;
  209. }
  210. .duration{
  211. position: absolute;
  212. margin-top: 5px;
  213. right: 30px;
  214. }
  215. .icon{
  216. position: absolute;
  217. top: 60px;
  218. transform: scale(1.3);
  219. }
  220. .back-btn{
  221. left: 40px;
  222. }
  223. .nav-btn{
  224. right: 40px;
  225. }
  226. /* volume button */
  227. .volume-slider{
  228. -webkit-appearance: none;
  229. width: 100px;
  230. height: 40px;
  231. position: absolute;
  232. right: -35px;
  233. bottom: 80px;
  234. transform: rotate(-90deg);
  235. border-radius: 20px;
  236. background: var(--alpha-color);
  237. overflow: hidden;
  238. opacity: 0;
  239. display: none;
  240. }
  241. .volume-slider.active{
  242. opacity: 1;
  243. display: block;
  244. }
  245. .volume-slider::-webkit-slider-thumb{
  246. -webkit-appearance: none;
  247. height: 40px;
  248. width: 10px;
  249. background: var(--primary-color);
  250. box-shadow: -200px 0 1px 200px var(--primary-color);
  251. }
  252. .playlist{
  253. width: 100%;
  254. height: 100%;
  255. position: fixed;
  256. top: 0;
  257. right: -100%;
  258. padding: 30px 0;
  259. background: var(--background);
  260. z-index: 3;
  261. transition: 1s;
  262. overflow: auto;
  263. }
  264. .playlist.active{
  265. right: 0;
  266. }
  267. .title{
  268. font-weight: 300;
  269. font-size: 40px;
  270. text-align: center;
  271. margin-top: 15px;
  272. text-transform: capitalize;
  273. margin-bottom: 30px;
  274. }
  275. .queue{
  276. width: 100%;
  277. height: 80px;
  278. padding: 0 30px;
  279. display: flex;
  280. align-items: center;
  281. border-top: 2px solid var(--alpha-color);
  282. }
  283. .queue-cover{
  284. width: 60px;
  285. height: 60px;
  286. border-radius: 10px;
  287. overflow: hidden;
  288. margin-right: 20px;
  289. position: relative;
  290. }
  291. .queue-cover img{
  292. width: 100%;
  293. height: 100%;
  294. object-fit: cover;
  295. }
  296. .queue-cover i{
  297. position: absolute;
  298. top: 50%;
  299. left: 50%;
  300. transform: translate(-50%, -50%);
  301. font-size: 30px;
  302. color: var(--primary-color);
  303. display: none;
  304. }
  305. .queue.active i{
  306. display: block;
  307. }
  308. .queue .name{
  309. font-size: 22px;
  310. text-transform: capitalize;
  311. }

注意!!!!! data.js必须在app.js之前引入,且两者都在body标签末尾处引入,不然会报错

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/247670
推荐阅读
相关标签
  

闽ICP备14008679号