반응형
Custom video player
Code
/* Get Our Elements */
const player = document.querySelector(".player");
const video = player.querySelector(".viewer");
const progress = player.querySelector(".progress");
const progressBar = player.querySelector(".progress__filled");
const toggle = player.querySelector(".toggle");
const skipButtons = player.querySelectorAll("[data-skip]");
const ranges = player.querySelectorAll(".player__slider");
/* Build out functions */
const togglePlay = () => {
const method = video.paused ? "play" : "pause";
video[method]();
};
const updateButton = (e) => {
const icon = e.target.value ? "►" : "❚ ❚";
toggle.textContent = icon;
};
const skip = (e) => {
video.currentTime += parseFloat(e.target.dataset.skip);
};
const handleRangeUpdate = (e) => {
video[e.target.name] = e.target.value;
};
const handleProgress = () => {
const percent = (video.currentTime / video.duration) * 100;
progressBar.style.flexBasis = `${percent}%`;
};
const scrub = (e) => {
//현재위치 / 바 길이 / 비디오 전체길이
const scrubTime = (e.offsetX / progress.offsetWidth) * video.duration;
video.currentTime = scrubTime;
};
/* Hook up the event listeners */
video.addEventListener("click", togglePlay);
video.addEventListener("play", updateButton);
video.addEventListener("pause", updateButton);
video.addEventListener("timeupdate", handleProgress);
toggle.addEventListener("click", togglePlay);
skipButtons.forEach((button) => button.addEventListener("click", skip));
ranges.forEach((range) => range.addEventListener("change", handleRangeUpdate));
ranges.forEach((range) => range.addEventListener("mousemove", handleRangeUpdate));
let mousedown = false;
progress.addEventListener("click", scrub);
progress.addEventListener("mousemove", (e) => mousedown && scrub(e));
progress.addEventListener("mousedown", () => (mousedown = true));
progress.addEventListener("mouseup", () => (mousedown = false));
배운 점 & 느낀 점
이번엔 js로 비디오를 조작 해 보았습니다.
video api 다루는 것에 조금은 익숙해졌습니다.
반응형
'공부 정리 > jsChallenge' 카테고리의 다른 글
js Challenge day 13 (0) | 2023.02.08 |
---|---|
js challenge day 12 (0) | 2023.02.07 |
js challenge day10 (0) | 2023.02.03 |
js challenge day 9 (0) | 2023.02.02 |
js challenge day 8 (2) | 2023.01.31 |
댓글