From d39fd06ef87e80c3d918ffd7f6f925b84d5104ab Mon Sep 17 00:00:00 2001 From: mohamadrezahedayat <57178712+mohamadrezahedayat@users.noreply.github.com> Date: Thu, 19 Nov 2020 16:31:37 +0330 Subject: [PATCH 1/2] Update script.js --- script.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/script.js b/script.js index 471e52c..11c25f0 100644 --- a/script.js +++ b/script.js @@ -62,18 +62,17 @@ let lastVolume = 1; // Mute function toggleMute() { - volumeIcon.className = ''; if (video.volume) { lastVolume = video.volume; video.volume = 0; - volumeIcon.classList.add('fas', 'fa-volume-mute'); - volumeIcon.setAttribute('title', 'Unmute'); volumeBar.style.width = 0; + setVolumeIcon(volume); + volumeIcon.setAttribute('title','Unmute'); } else { video.volume = lastVolume; - volumeIcon.classList.add('fas', 'fa-volume-up'); - volumeIcon.setAttribute('title', 'Mute'); volumeBar.style.width = `${lastVolume * 100}%`; + setVolumeIcon(volume); + volumeIcon.setAttribute('title','Mute'); } } @@ -90,17 +89,20 @@ function changeVolume(e) { volumeBar.style.width = `${volume * 100}%`; video.volume = volume; // Change icon depending on volume - volumeIcon.className = ''; - if (volume > 0.7) { - volumeIcon.classList.add('fas', 'fa-volume-up'); - } else if (volume < 0.7 && volume > 0) { - volumeIcon.classList.add('fas', 'fa-volume-down'); - } else if (volume === 0) { - volumeIcon.classList.add('fas', 'fa-volume-off'); - } + setVolumeIcon(volume); lastVolume = volume; } - +//set volumeIcon classlist +function setVolumeIcon(volume){ + volumeIcon.className = ""; + if (volume > 0.7) { + volumeIcon.classList.add("fas", "fa-volume-up"); + } else if (volume < 0.7 && volume > 0) { + volumeIcon.classList.add("fas", "fa-volume-down"); + } else if (volume === 0) { + volumeIcon.classList.add("fas", "fa-volume-off"); + } +} // Change Playback Speed -------------------- // function changeSpeed() { From c078c39cc0491ad39b9bb9dfde318149277d14ec Mon Sep 17 00:00:00 2001 From: mohamadrezahedayat <57178712+mohamadrezahedayat@users.noreply.github.com> Date: Thu, 19 Nov 2020 18:09:17 +0330 Subject: [PATCH 2/2] Update script.js --- script.js | 173 +++++++++++++++++++++++------------------------------- 1 file changed, 74 insertions(+), 99 deletions(-) diff --git a/script.js b/script.js index 11c25f0..70b7696 100644 --- a/script.js +++ b/script.js @@ -1,169 +1,144 @@ -const player = document.querySelector('.player'); -const video = document.querySelector('.video'); -const progressRange = document.querySelector('.progress-range'); -const progressBar = document.querySelector('.progress-bar'); -const playBtn = document.getElementById('play-btn'); -const volumeIcon = document.getElementById('volume-icon'); -const volumeRange = document.querySelector('.volume-range'); -const volumeBar = document.querySelector('.volume-bar'); -const speed = document.querySelector('.player-speed'); -const currentTime = document.querySelector('.time-elapsed'); -const duration = document.querySelector('.time-duration'); -const fullscreenBtn = document.querySelector('.fullscreen'); +const player = document.querySelector(".player"); +const video = document.querySelector("video"); +const progressRange = document.querySelector(".progress-range"); +const progressBar = document.querySelector(".progress-bar"); +const playBtn = document.getElementById("play-btn"); +const volumeIcon = document.getElementById("volume-icon"); +const volumeRange = document.querySelector(".volume-range"); +const volumeBar = document.querySelector(".volume-bar"); +const currentTime = document.querySelector(".time-elapsed"); +const duration = document.querySelector(".time-duration"); +const speed = document.querySelector(".player-speed"); +const fullscreenBtn = document.querySelector(".fullscreen"); // Play & Pause ----------------------------------- // - function showPlayIcon() { - playBtn.classList.replace('fa-pause', 'fa-play'); - playBtn.setAttribute('title', 'Play'); + playBtn.classList.replace("fa-pause", "fa-play"); + playBtn.setAttribute("title", "Play"); } - function togglePlay() { if (video.paused) { video.play(); - playBtn.classList.replace('fa-play', 'fa-pause'); - playBtn.setAttribute('title', 'Pause'); + playBtn.classList.replace("fa-play", "fa-pause"); + playBtn.setAttribute("title", "Pause"); } else { video.pause(); showPlayIcon(); } } - -// On video end, show play button icon -video.addEventListener('ended', showPlayIcon); +// On Video End, show play button icon +video.addEventListener("ended", showPlayIcon); // Progress Bar ---------------------------------- // - -// Format current time, duration +// Calculate Display Time Format function displayTime(time) { const minutes = Math.floor(time / 60); let seconds = Math.floor(time % 60); seconds = seconds > 9 ? seconds : `0${seconds}`; return `${minutes}:${seconds}`; } - // Update progress bar as video plays function updateProgress() { progressBar.style.width = `${(video.currentTime / video.duration) * 100}%`; currentTime.textContent = `${displayTime(video.currentTime)} /`; - duration.textContent = `${displayTime(video.duration)}`; + duration.textContent = displayTime(video.duration); } - -// Click to seek within the video +// Click to Seek within the Video function setProgress(e) { const newTime = e.offsetX / progressRange.offsetWidth; progressBar.style.width = `${newTime * 100}%`; video.currentTime = newTime * video.duration; } - // Volume Controls --------------------------- // let lastVolume = 1; - -// Mute -function toggleMute() { - if (video.volume) { - lastVolume = video.volume; - video.volume = 0; - volumeBar.style.width = 0; - setVolumeIcon(volume); - volumeIcon.setAttribute('title','Unmute'); - } else { - video.volume = lastVolume; - volumeBar.style.width = `${lastVolume * 100}%`; - setVolumeIcon(volume); - volumeIcon.setAttribute('title','Mute'); - } -} - // Volume Bar function changeVolume(e) { let volume = e.offsetX / volumeRange.offsetWidth; - // Rounding volume up or down + // Rounding Volume up or down if (volume < 0.1) { volume = 0; - } - if (volume > 0.9) { + } else if (volume > 0.9) { volume = 1; } volumeBar.style.width = `${volume * 100}%`; video.volume = volume; - // Change icon depending on volume + // change icon depending to volume setVolumeIcon(volume); - lastVolume = volume; + } //set volumeIcon classlist -function setVolumeIcon(volume){ - volumeIcon.className = ""; - if (volume > 0.7) { - volumeIcon.classList.add("fas", "fa-volume-up"); - } else if (volume < 0.7 && volume > 0) { - volumeIcon.classList.add("fas", "fa-volume-down"); - } else if (volume === 0) { - volumeIcon.classList.add("fas", "fa-volume-off"); - } +function setVolumeIcon(volume) { + volumeIcon.className = ""; + if (volume > 0.7) { + volumeIcon.classList.add("fas", "fa-volume-up"); + } else if (volume < 0.7 && volume > 0) { + volumeIcon.classList.add("fas", "fa-volume-down"); + } else if (volume === 0) { + volumeIcon.classList.add("fas", "fa-volume-off"); + } +} +// Mute/UnMute +function toggleMute() { + if (video.volume) { + lastVolume = video.volume; + video.volume = 0; + volumeBar.style.width = 0; + setVolumeIcon(video.volume); + volumeIcon.setAttribute("title", "Unmute"); + } else { + video.volume = lastVolume; + volumeBar.style.width = `${lastVolume * 100}%`; + setVolumeIcon(video.volume); + volumeIcon.setAttribute("title", "Mute"); + } } // Change Playback Speed -------------------- // - function changeSpeed() { video.playbackRate = speed.value; } - // Fullscreen ------------------------------- // - /* View in fullscreen */ -function openFullscreen(element) { - if (element.requestFullscreen) { - element.requestFullscreen(); - } else if (element.mozRequestFullScreen) { - /* Firefox */ - element.mozRequestFullScreen(); - } else if (element.webkitRequestFullscreen) { - /* Chrome, Safari and Opera */ - element.webkitRequestFullscreen(); - } else if (element.msRequestFullscreen) { - /* IE/Edge */ - element.msRequestFullscreen(); +function openFullscreen(elem) { + if (elem.requestFullscreen) { + elem.requestFullscreen(); + } else if (elem.webkitRequestFullscreen) { + /* Safari */ + elem.webkitRequestFullscreen(); + } else if (elem.msRequestFullscreen) { + /* IE11 */ + elem.msRequestFullscreen(); } - video.classList.add('video-fullscreen'); + video.classList.add("video-fullscreen"); } /* Close fullscreen */ function closeFullscreen() { if (document.exitFullscreen) { document.exitFullscreen(); - } else if (document.mozCancelFullScreen) { - /* Firefox */ - document.mozCancelFullScreen(); } else if (document.webkitExitFullscreen) { - /* Chrome, Safari and Opera */ + /* Safari */ document.webkitExitFullscreen(); } else if (document.msExitFullscreen) { - /* IE/Edge */ + /* IE11 */ document.msExitFullscreen(); } - video.classList.remove('video-fullscreen'); + video.classList.remove("video-fullscreen"); } let fullscreen = false; - -// Toggle fullscreen +// toggle fullscreen function toggleFullscreen() { - if (!fullscreen) { - openFullscreen(player); - } else { - closeFullscreen(); - } + !fullscreen?openFullscreen(player): closeFullscreen(); fullscreen = !fullscreen; } - -// Event Listeners -playBtn.addEventListener('click', togglePlay); -video.addEventListener('click', togglePlay); -video.addEventListener('timeupdate', updateProgress); -video.addEventListener('canplay', updateProgress); -progressRange.addEventListener('click', setProgress); -volumeRange.addEventListener('click', changeVolume); -volumeIcon.addEventListener('click', toggleMute); -speed.addEventListener('change', changeSpeed); -fullscreenBtn.addEventListener('click', toggleFullscreen); +//Event Listeners +playBtn.addEventListener("click", togglePlay); +video.addEventListener("click", togglePlay); +video.addEventListener("timeupdate", updateProgress); +video.addEventListener("canplay", updateProgress); +progressRange.addEventListener("click", setProgress); +volumeRange.addEventListener("click", changeVolume); +volumeIcon.addEventListener("click", toggleMute); +speed.addEventListener("change", changeSpeed); +fullscreenBtn.addEventListener("click", toggleFullscreen);