Skip to content

Commit

Permalink
day31
Browse files Browse the repository at this point in the history
  • Loading branch information
duyvn0612 committed Jun 13, 2024
1 parent 9b0b50e commit e2ce420
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 45 deletions.
57 changes: 48 additions & 9 deletions Day_31/css/style.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Day_31/css/style.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions Day_31/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<title>audio</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="progress">
<div class="progress-bar">
<div class="audio">
<div class="player-audio">
<i class="fa-solid fa-play"></i>
</div>
<span>00:00</span>
<div class="progress">
<span></span>
<div class="progress-bar">
<span id="rewind"></span>
</div>
</div>
<span>00:00</span>
</div>
<audio src="mp3/tuyetyeuthuong.mp3" controls hidden></audio>
<script src="js/script.js"></script>
<script
src="https://kit.fontawesome.com/331f6e8450.js"
crossorigin="anonymous"
></script>
</body>
</html>
129 changes: 115 additions & 14 deletions Day_31/js/script.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,151 @@
var progress = document.querySelector(".progress");
var progressBar = document.querySelector(".progress-bar");
var spanDrag = document.querySelector("span");
var spanDrag = document.querySelector("#rewind");
var progressWidth = progress.clientWidth;
var positionSpanDragDown = 0;
var positionSpanDragUp = 0;
var positionLastSpanDrag = 0;
var offsetX = 0;
// var clientXDrag = 0;
console.log(progressWidth);
var rateAudio = 0;
var flagCheckTimeUpdate = true;
function handlerProgressDown(e) {
console.log("down");
if (flagCheckTimeUpdate) {
positionSpanDragUp = (audio.currentTime / audio.duration) * progressWidth;
flagCheckTimeUpdate = false;
}
offsetX = e.offsetX;
positionLastSpanDrag = offsetX;
positionSpanDragUp = offsetX;
positionSpanDragDown = e.clientX;
console.log(positionSpanDragDown);
var rate = (offsetX / progressWidth) * 100;
rateAudio = rate;
audio.currentTime = (rateAudio / 100) * audio.duration;
progressBar.style.width = `${rate}%`;

document.addEventListener("mousemove", handlerDrag);
}

function handlerDrag(e) {
console.log("move");
var clientXDrag = e.clientX;
console.log(clientXDrag);
positionLastSpanDrag =
clientXDrag - positionSpanDragDown + positionSpanDragUp;
// console.log(positionLastSpanDrag);
var rate = (positionLastSpanDrag / progressWidth) * 100;
// console.log(rate);
if (rate < 0) rate = 0;
if (rate > 100) rate = 100;
if (rate <= 0) {
rate = 0;
}
if (rate >= 100) {
rate = 100;
}
rateAudio = rate;
console.log(`rate: ${rate}`);
currentTime.innerText = `${getTime((rateAudio / 100) * audio.duration)}`;
progressBar.style.width = `${rate}%`;
}
function handlerMouseUp() {
console.log("up");
if (positionLastSpanDrag < 0) positionLastSpanDrag = 0;
if (positionLastSpanDrag > progressWidth) {
positionLastSpanDrag = progressWidth;
}
if (rateAudio >= 100) {
rateAudio = 100;
audio.currentTime = audio.duration;
positionLastSpanDrag = progressWidth;
audio.pause();
}
if (flagCheckTimeUpdate === false) {
flagCheckTimeUpdate = true;
audio.currentTime = (rateAudio / 100) * audio.duration;
}
positionSpanDragUp = positionLastSpanDrag;
// console.log(`positionSpanDragUp mouseup: ${positionSpanDragUp}`);
document.removeEventListener("mousemove", handlerDrag);
}

function handlerProgressMove(e) {
// console.log(e.target.className);
if (
e.target.className === "progress" ||
e.target.className === "progress-bar"
) {
var timeView = (e.offsetX / progressWidth) * audio.duration;
// console.log(getTime(timeView));
timerView.classList.add("action-timer-view");
timerView.innerText = getTime(timeView);
timerView.style.left = `${e.offsetX}px`;
}
}

progress.addEventListener("mousedown", handlerProgressDown);
progress.addEventListener("mouseover", function () {
progress.addEventListener("mousemove", handlerProgressMove);
});
progress.addEventListener("mouseout", function () {
timerView.classList.remove("action-timer-view");
progress.removeEventListener("mousemove", handlerProgressMove);
});

// spanDrag.addEventListener("mousemove", function (e) {
// e.stopPropagation();
// });

spanDrag.addEventListener("mousedown", function (e) {
e.stopPropagation();
document.addEventListener("mousemove", handlerDrag);
if (flagCheckTimeUpdate) {
positionSpanDragUp = (audio.currentTime / audio.duration) * progressWidth;
flagCheckTimeUpdate = false;
}
positionSpanDragDown = e.clientX;
document.addEventListener("mousemove", handlerDrag);
});
document.addEventListener("mouseup", handlerMouseUp);
// Edit Audio
var playerAudio = document.querySelector(".player-audio i");
var audio = document.querySelector("audio");
var currentTime = progress.previousElementSibling;
var totalTime = progress.nextElementSibling;
var timerView = progressBar.previousElementSibling;
// console.log(timerView);
function getTime(time) {
var mins = Math.floor(time / 60);
var secs = Math.floor(time - mins * 60);
return `${mins < 10 ? `0${mins}` : mins}:${secs < 10 ? `0${secs}` : secs}`;
}
// 278.282449
window.addEventListener("load", function () {
totalTime.innerText = getTime(audio.duration);

currentTime.innerText = getTime(audio.currentTime);
// event ended hết nhạc
audio.addEventListener("ended", function () {
playerAudio.classList.replace("fa-pause", "fa-play");
});
// event play
audio.addEventListener("play", function () {
playerAudio.classList.replace("fa-play", "fa-pause");
});
// event pause
audio.addEventListener("pause", function () {
playerAudio.classList.replace("fa-pause", "fa-play");
});
// event timeupdate
audio.addEventListener("timeupdate", function (e) {
if (flagCheckTimeUpdate) {
currentTime.innerText = getTime(audio.currentTime);
// console.log();
console.log(audio.currentTime, audio.duration);
// console.log(getTime(audio.currentTime));
rateAudio = (audio.currentTime / audio.duration) * 100;
progressBar.style.width = `${rateAudio}%`;
}
});
// event playerAudio click
playerAudio.addEventListener("click", handlerPlayerClick);
});

function handlerPlayerClick(e) {
e.stopPropagation();
if (audio.paused) {
audio.play();
} else {
audio.pause();
}
}
Binary file added Day_31/mp3/saochuanamtaynhau.mp3
Binary file not shown.
Binary file added Day_31/mp3/tuyetyeuthuong.mp3
Binary file not shown.
78 changes: 60 additions & 18 deletions Day_31/scss/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,70 @@
box-sizing: border-box;
}

.progress {
.audio {
margin-top: 4rem;
display: flex;
justify-content: center;
align-items: center;
gap: 1rem;
position: relative;
margin: 2rem auto;
width: 80%;
height: 0.6rem;
background-color: #bcb5b5;
.progress-bar {
user-select: none;
span {
user-select: none;
}
.player-audio {
position: absolute;
top: 0;
left: 0;
bottom: 0;
background-color: red;
width: 00%;
span {
left: 50%;
bottom: 2rem;
font-size: 1.8rem;
i {
cursor: pointer;
}
}
.progress {
position: relative;
// margin: 2rem auto;
width: 80%;
height: 0.4rem;
border-radius: 99px;
background-color: #b4b8be;
> span {
display: none;
}
.action-timer-view {
position: absolute;
bottom: 9px;
display: inline-block;
translate: -50% 0;
color: white;
font-size: 10px;
background-color: #ff8b00;
padding: 3px 5px;
border-radius: 99px;
}
cursor: pointer;
.progress-bar {
position: absolute;
top: 0;
right: 0;
translate: 50% -25%;
width: 1rem;
height: 1rem;
border-radius: 50%;
background-color: blueviolet;
left: 0;
bottom: 0;
background-color: #4bd064;
border-radius: 99px;
width: 0%;

#rewind {
user-select: none;
position: absolute;
top: 0;
right: 0;
translate: 50% -30%;
width: 1rem;
height: 1rem;
border-radius: 50%;
background-color: #4bd064;
cursor: pointer;
// box-shadow: 0px 0px 2px 0px;
}
}
}
}

0 comments on commit e2ce420

Please sign in to comment.