本文實例為大家分享了JS實現音樂播放器的具體代碼,供大家參考,具體內容如下
首先,使用HTML搭好我們的框架結構。這一步為了提高我們代碼的可讀性,多寫注釋。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
<!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 = "images/1.jpg" id = "img" alt = "" /></ div > <!-- 歌曲封面 end --> </ div > <!-- 歌曲信息 end --> <!-- 信息和音量 --> < div class = "mtv" > <!-- 文字信息 --> < div class = "text" > < h1 id = "sName" >歌曲名稱</ h1 > < h2 id = "singer" >歌手</ h2 > </ div > <!-- 文字信息 end --> <!-- 音量控制 --> < div class = "vol01" > < div class = "volTop" id = "volText" > 100% </ div > < div class = "volBottom" > < div class = "volPro" id = "vol" > < div class = "volColor" id = "volCol" ></ div > < div class = "volBlock" id = "volB" ></ div > </ div > < div class = "vImg" > < img src = "images/音量.png" alt = "" /> </ div > </ div > </ div > <!-- 音量控制 end --> </ div > <!-- 信息和音量 end --> <!-- 進度條 --> < div class = "progress" > < span class = "cTime time" id = "cTime" >00:00</ span > <!-- 當前時間 --> < span class = "tTime time" id = "tTime" >00:00</ span > <!-- 總的時間 --> < div class = "clears" ></ div > <!-- 進度條小 --> < div class = "proBar" id = "songPro" > < div class = "proColor" id = "proBar" ></ div > </ div > <!-- 進度條小 end --> </ div > <!-- 進度條 end --> <!-- 控制按鈕 --> < div class = "ctrls" > < div class = "btn" > < img src = "images/prev.png" class = "prevBtn" id = "prevBtn" alt = "" /> < img src = "images/next.png" class = "nextPrev" id = "nextBtn" alt = "" /> </ div > < button type = "button" class = "playBtn" id = "playBtn" >< img src = "images/播放.png" alt = "" /></ button > </ div > <!-- 控制按鈕 end --> < audio src = "#" id = "music" ></ audio > </ div > <!-- 音樂播放器 end --> < script src = "js/js.js" ></ script > </ body > </ html > |
第二步,搭好框架后,就用CSS給他寫上你喜歡的樣式。注意這里我是將樣式分為了公共樣式和獨立樣式分開寫的,提高代碼可重復利用,當然你也可以使用SASS更方便,這里我只提供了獨立樣式,公共樣式可以在網上自主尋找。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
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>h 1 { font-size : 24px ; line-height : 30px ; font-weight : normal ; } .text>h 2 { font-family : "Microsoft YaHei" ; font-size : 14px ; font-weight : 300 ; line-height : 1.7 ; } .vol 01 { 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.3 s; } .nextPrev:hover { opacity: 0.5 ; } .prevBtn { left : 0 ; opacity: 0.1 ; transition: all 0.3 s; } .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.3 s; } .playBtn:hover { opacity: 0.5 ; background : #46847b ; } |
第三步,也是讓音樂播放器具有靈魂的東西,使用JS添加功能。
1.獲取ID(當然這里也可以獲取類名或是元素,看你自己的習慣)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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.添加音樂播放功能。
導入音樂
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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" }]; |
音樂播放
1
2
3
4
5
6
7
8
9
10
|
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); |
音樂播放暫停
1
2
3
4
5
6
7
8
9
|
playBtn.addEventListener( "click" , function (event) { if (music.paused) { music.play(); event.currentTarget.innerHTML = '<img src="images/暫停.png" id="codetool">
音樂切換
3.添加進度條。
小功能:點擊進度條,音樂也跟著變化跳轉到指定的時間。
4.添加音量調節模塊。
全部JS代碼 這里我使用了{}作用域,讓let 或 const聲明的變量只在 let 或 const命令所在的代碼塊 {} 內有效,在 {} 之外不能訪問(ECMAScript 6(簡稱ES6)中新增的)。
|