-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
321 lines (285 loc) · 9.23 KB
/
Copy pathscript.js
File metadata and controls
321 lines (285 loc) · 9.23 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
const $ = document.querySelector.bind(document);
const $$ = document.querySelectorAll.bind(document);
const PLAYER_STORAGE_KEY = 'F8_PLAYER'
const player = $('.player');
const cd = $('.cd');
const heading = $('header h2');
const cdThumb = $('.cd-thumb');
const audio = $('#audio');
const playBtn = $('.btn-toggle-play');
const progress = $('#progress');
const nextBtn = $('.btn-next');
const prevBtn = $('.btn-prev');
const randomBtn = $('.btn-random');
const repeatBtn = $('.btn-repeat');
const playList = $('.playlist')
const app = {
currentIndex: 0,
isPlaying: false,
isRandom: false,
isRepeat: false,
config: JSON.parse(localStorage.getItem(PLAYER_STORAGE_KEY)) || {},
songs: [
{
name: "Đôi lời",
singer: "Hoàng Dũng",
path: 'song1.mp3',
image: 'song1.jpg'
},
{
name: "Gói nắng mang về",
singer: "Hoàng Dũng",
path: 'song2.mp3',
image: 'song2.jpg'
},
{
name: "Nàng thơ",
singer: "Hoàng Dũng",
path: 'song3.mp3',
image: 'song3.jpg'
},
{
name: "Một điều chưa nói",
singer: "Hoàng Dũng",
path: 'song4.mp3',
image: 'song4.jpg'
},
{
name: "Thằng điên",
singer: "Justatee & Phương Ly",
path: 'song5.mp3',
image: 'song5.jpg'
},
{
name: "Đã lỡ yêu em nhiều",
singer: "Justatee",
path: 'song6.mp3',
image: 'song6.jpg'
},
{
name: "Cơn mưa cuối",
singer: "Justatee & Binz",
path: 'song7.mp3',
image: 'song7.jpg'
},
{
name: "Cô nàng khác người",
singer: "MCK & Pixel & Neko",
path: 'song8.mp3',
image: 'song8.jpg'
},
{
name: "Không cần cố",
singer: "MCK & Pixel & Tlinh",
path: 'song9.mp3',
image: 'song9.jpg'
},
{
name: "Chìm sâu",
singer: "MCK & Trungtran",
path: 'song10.mp3',
image: 'song10.jpg'
}
],
setConFig: function(key, value) {
this.config[key] = value
localStorage.setItem(PLAYER_STORAGE_KEY, JSON.stringify(this.config))
},
render: function() {
const htmls = this.songs.map((song, index) => {
return `
<div class="song ${index === this.currentIndex ? 'active' : ''}"
data-index="${index}">
<div class="thumb"
style="background-image: url('${song.image}')">
</div>
<div class="body">
<h3 class="title">${song.name}</h3>
<p class="author">${song.singer}</p>
</div>
<div class="option">
<i class="fas fa-ellipsis-h"></i>
</div>
</div>`
})
$('.playlist').innerHTML = htmls.join('')
},
defineProperties: function() {
Object.defineProperty(this, 'currentSong', {
get: function() {
return this.songs[this.currentIndex];
}
})
},
handleEvents: function() {
const _this = this
const cdWidth = cd.offsetWidth
// Xử lý CD quay / dừng
const cdThumbAnimate = cdThumb.animate([
{ transform: 'rotate(360deg'}
], {
duration: 10000, //10s
iterations: Infinity
})
cdThumbAnimate.pause()
// Xử lý phóng tỏ / thu nhỏ CD
document.onscroll = function() {
const scrollTop = window.scrollY || document.documentElement.scrollTop;
const newCdWidth = cdWidth - scrollTop;
cd.style.width = newCdWidth > 0 ? newCdWidth + "px" : 0;
cd.style.opacity = newCdWidth / cdWidth;
}
// Xử lý khi click play
playBtn.onclick = function() {
if(_this.isPlaying) {
audio.pause();
} else {
audio.play();
}
}
// Khi song được play
audio.onplay = function() {
_this.isPlaying = true;
player.classList.add('playing')
cdThumbAnimate.play()
}
// Khi song bị pause
audio.onpause = function() {
_this.isPlaying = false;
player.classList.remove('playing')
cdThumbAnimate.pause()
}
// Khi tiến độ bài hát thay đổi
audio.ontimeupdate = function() {
if (audio.duration) {
const progressPercent = Math.floor(audio.currentTime / audio.duration * 100);
progress.value = progressPercent
}
}
// Xử lý khi tua bài hát
progress.oninput = function(e) {
const seekTime = audio.duration * e.target.value / 100;
audio.currentTime = seekTime;
}
// Khi next song
nextBtn.onclick = function() {
if (_this.isRandom) {
_this.playRandomSong()
} else {
_this.nextSong()
}
audio.play()
_this.render()
_this.scrollToActiveSong()
}
// Khi prev song
prevBtn.onclick = function() {
if (_this.isRandom) {
_this.playRandomSong()
} else {
_this.prevSong()
}
audio.play()
_this.render()
_this.scrollToActiveSong()
}
// Xử lý Random bật tắt random song
randomBtn.onclick = function(e) {
_this.isRandom = !_this.isRandom
_this.setConFig('isRandom', _this.isRandom)
randomBtn.classList.toggle('active', _this.isRandom);
}
// Xử lý lặp lại một bài hát
repeatBtn.onclick = function(e) {
_this.isRepeat = !_this.isRepeat
_this.setConFig('isRepeat', _this.isRepeat)
repeatBtn.classList.toggle('active', _this.isRepeat);
}
// Xử lý next song khi audio ended
audio.onended = function() {
if (_this.isRepeat) {
audio.play()
} else {
nextBtn.click()
}
}
// Lắng nghe hành vi click vào playList
playList.onclick = function(e) {
const songNode = e.target.closest('.song:not(.active)');
if (songNode || e.target.closest('.option')) {
// Xử lý khi click vào bài hát
if (songNode) {
_this.currentIndex = Number(songNode.dataset.index);
_this.loadCurrentSong();
audio.play();
_this.render()
}
// Xử lý khi click vào song option
if (e.target.closest('.option')) {
}
}
}
},
scrollToActiveSong: function () {
setTimeout(function () {
if ((app.currentIndex === 0, 1)) {
$(".song.active").scrollIntoView({
behavior: "smooth",
block: "end",
});
} else {
$(".song.active").scrollIntoView({
behavior: "smooth",
block: "nearest",
});
}
}, 300);
},
loadCurrentSong: function() {
heading.textContent = this.currentSong.name;
cdThumb.style.backgroundImage = `url('${this.currentSong.image}')`;
audio.src = this.currentSong.path;
},
loadConfig: function() {
this.isRandom = this.config.isRandom
this.isRepeat = this.config.isRepeat
},
nextSong: function() {
this.currentIndex++
if (this.currentIndex >= this.songs.length) {
this.currentIndex = 0
}
this.loadCurrentSong()
},
prevSong: function() {
this.currentIndex--
if (this.currentIndex < 0) {
this.currentIndex = this.songs.length - 1;
}
this.loadCurrentSong()
},
playRandomSong: function() {
let newIndex
do {
newIndex = Math.floor(Math.random() * this.songs.length)
} while (newIndex === this.currentIndex)
this.currentIndex = newIndex
this.loadCurrentSong()
},
start: function() {
// Gán cấu hình từ config vào ứng dụng
this.loadConfig()
// Định nghĩa các thuộc tính cho object
this.defineProperties()
// Lắng nghe & xử lý các sự kiện (DOM Events)
this.handleEvents();
// Tải thông tin bài hát đầu tiên vào UI khi chạy ứng dụng
this.loadCurrentSong();
// Render Playlist
this.render();
// Hiển thị trạng thái ban đầu của button repeat & random
randomBtn.classList.toggle('active', _this.isRandom);
repeatBtn.classList.toggle('active', _this.isRepeat);
}
}
app.start()