js30关第十一关

点击播放暂停

1
2
3
4
5
function togglePlay() {
const method = video.paused ? 'play' : 'pause';
video[method]();
}
video.addEventListener('click', togglePlay);

更新进度条

1
2
3
4
5
function handleProgress() {
const percent = (video.currentTime / video.duration) * 100;
progressBar.style.flexBasis = `${percent}%`;
}
video.addEventListener('timeupdate', handleProgress);

调整音量以及倍速,使用 this.name获取对象,

1
2
3
4
5
function handleRangeUpdate() {
video[this.name] = this.value;
}
ranges.forEach(range => range.addEventListener('change', handleRangeUpdate));
ranges.forEach(range => range.addEventListener('mousemove', handleRangeUpdate));

设置点击进度条

1
2
3
4
5
function scrub(e) {
const scrubTime = (e.offsetX / progress.offsetWidth) * video.duration;
video.currentTime = scrubTime;
}
progress.addEventListener('mousemove', (e) => mousedown && scrub(e));

设置自定义属性data-skip="-10",使用this.dataset.skip获取数

1
2
3
4
5
function skip() {
video.currentTime += parseFloat(this.dataset.skip);
}
skipButtons.forEach(button => button.addEventListener('click', skip));