-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
117 lines (89 loc) · 2.47 KB
/
main.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
var volumeObj = {
_vol: 0.3,
get value () {
return this._vol
},
set value(val) {
this._vol = val
audio.volume = val
}
}
var progressObj = {
_pro: 0,
get value () {
return this._pro
},
set value (val) {
this._pro = val
audio.currentTime = audio.duration * val
}
}
/* 检查是否引入了slider.js */
if (typeof(slider) === 'function') {
slider('progress-btn', 'progress', 'x', progressObj)
}
else {
console.log('slider.js should import first!')
}
/*
* 操作音量按钮
*/
let volumeSlide = document.querySelector('.volume-wrapper'),
volume = document.querySelector('.volume')
// 音量条的hover
volume.addEventListener('click', function() {
if (getComputedStyle(volumeSlide).visibility === 'hidden') {
volumeSlide.style.visibility = "visible"
// 音量对应的slider组件
slider('volume-btn', 'volume-slider', 'y', volumeObj)
} else {
volumeSlide.style.visibility = "hidden"
}
})
let audio = document.querySelector('audio'),
play = document.querySelector('#play'),
time = document.querySelector('#time'),
cover = document.querySelector('.cover') ,
progress = document.querySelector('.progress'),
progressBtn = document.querySelector('.progress-btn')
fullWidth = progress.parentElement.getBoundingClientRect().width
play.addEventListener('click', toggle)
audio.addEventListener('timeupdate', function() {
current = secToMin(audio.currentTime)
duration = secToMin(audio.duration)
time.textContent = `${current}/${duration}`
let progressBtnWidth = progressBtn.getBoundingClientRect().width,
playedWidth = fullWidth * (audio.currentTime/ audio.duration)
progress.style.width = playedWidth + "px"
progressBtn.style.left = playedWidth - progressBtnWidth / 2 + "px"
})
// play pause toggle
function toggle () {
if (!audio) {
return false
}
if (this.className === 'play') {
this.className = 'pause'
audio.play()
cover.style.animationPlayState = "running"
} else {
this.className = 'play'
audio.pause()
cover.style.animationPlayState = "paused"
}
}
// 时间格式转换
function secToMin (secs) {
if(secs <= 0) {
return false
}
if (secs < 60) {
return secs < 10? `00:0${parseInt(secs)}` : `00:${parseInt(secs)}`
}
let min = parseInt(secs / 60),
sec = parseInt(secs - min * 60)
if (sec < 10) {
sec = `0${sec}`
}
return min > 9 ? `${min}:${sec}` : `0${min}:${sec}`
}