-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (124 loc) · 3.58 KB
/
index.js
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
/**
* @description: 处理歌词格式
* @return lrc array
* @author: gmx
*/
function parseLrc(lrcStr) {
var lines = lrcStr.split("\n");
const lrcArr = [];
for (let i = 0; i < lines.length; i++) {
let [times, words] = lines[i].split("]");
lrcArr.push({ time: parseTime(times.substring(1)), text: words });
}
return lrcArr;
}
/**
* @description: 创建li歌词dom
* @return {*}
* @author: gmx
*/
function generateLyrics() {
var frag = document.createDocumentFragment();
for (let i = 0; i < lyrics.length; i++) {
var li = document.createElement("li");
li.textContent = lyrics[i].text;
frag.appendChild(li);
}
ul.appendChild(frag);
}
/**
* @description: 查找高亮歌词的index
* @return {*}
* @author: gmx
*/
function findIndex() {
var currentTime = audio.currentTime;
var index = lyrics.findIndex((e) => e.time > currentTime);
if (index === -1) {
index = lyrics.length;
}
return index - 1;
}
/**
* @description: 歌词偏移及高亮
* @return {*}
* @author: gmx
*/
function setOffset() {
var index = findIndex();
var offset = liHeight * index + liHeight / 2 - containerHeight / 2;
if (offset < 0) {
offset = 0;
} else if (offset > maxOffset) {
offset = maxOffset;
}
ul.style.transform = `translateY(-${offset}px)`;
// 去掉前行歌词高亮
var li = document.querySelector(".active");
if (li) {
li.classList.remove("active");
}
ul.children[index]?.classList.add("active");
}
/**
* @description: 分秒转换为秒
* @return {*}
* @author: gmx
*/
function parseTime(timeStr) {
var [minite, other] = timeStr.split(":");
var [second, _] = other.split(".");
var seconds = Number(minite) * 60 + Number(second);
return seconds;
}
const audio = document.getElementById('audio');
const playPauseBtn = document.getElementById('playPauseBtn');
const nextBtn = document.getElementById('nextBtn');
const progressContainer = document.getElementById('progressContainer');
const progressBar = document.getElementById('progressBar');
const timeDisplay = document.getElementById('timeDisplay');
const lyricsContainer = document.querySelector('.lyrics-container');
const ul = document.querySelector("ul");
const lyrics = parseLrc(lrc);
generateLyrics();
// 容器高度
const containerHeight = lyricsContainer.clientHeight;
const liHeight = ul.children[0].clientHeight;
// 最大偏移量
const maxOffset = ul.clientHeight - containerHeight;
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${minutes}:${secs < 10 ? '0' : ''}${secs}`;
}
playPauseBtn.addEventListener('click', () => {
if (audio.paused) {
audio.play();
playPauseBtn.classList.remove('play');
playPauseBtn.classList.add('pause');
} else {
audio.pause();
playPauseBtn.classList.remove('pause');
playPauseBtn.classList.add('play');
}
});
nextBtn.addEventListener('click', () => {
// 这里可以添加切换到下一首歌曲的逻辑
audio.currentTime = 0;
audio.play();
playPauseBtn.classList.remove('play');
playPauseBtn.classList.add('pause');
});
audio.addEventListener('timeupdate', () => {
const progressPercent = (audio.currentTime / audio.duration) * 100;
progressBar.style.width = `${progressPercent}%`;
timeDisplay.textContent = `${formatTime(audio.currentTime)} / ${formatTime(audio.duration)}`;
setOffset();
});
audio.addEventListener('ended', () => {
playPauseBtn.classList.remove('pause');
playPauseBtn.classList.add('play');
});
audio.addEventListener('loadedmetadata', () => {
timeDisplay.textContent = `00:00 / ${formatTime(audio.duration)}`;
});