<header>
<h2 class="title">HTML Events: timeupdate</h2>
<p class="description">Відстежує зміни часу відтворення відео.</p>
</header>
<main>
<div class="result">
<video id="video" width="400" controls>
<source src="https://www.w3schools.com/html/mov_bbb.mp4" type="video/mp4">
Ваш браузер не підтримує тег відео.
</video>
<div>
<button id="playPauseBtn">Відтворити</button>
<button id="resetBtn">Почати спочатку</button>
</div>
<div style="margin-top: 15px;">
<div style="background-color: #ddd; width: 100%; height: 10px; position: relative;">
<div id="progress" style="background-color: #76c7c0; height: 10px; width: 0%;"></div>
</div>
</div>
<div style="margin-top: 10px;">
Поточний час: <span id="currentTime">0.00</span> сек / Загальна тривалість: <span id="duration">0.00</span> сек
</div>
</div>
</main>
body {
font-size: 16px;
line-height: 1.5;
font-family: monospace;
}
header {
background-color: #f1f1f1;
margin-bottom: 25px;
padding: 15px;
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}
header h2.title {
padding-bottom: 15px;
border-bottom: 1px solid #999;
}
header p.description {
font-style: italic;
color: #222;
}
.result {
background-color: #f8f8f8;
padding: 15px;
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
box-shadow: 0px 0px 3px 0px rgba(118, 118, 118, 1);
}
#playPauseBtn, #resetBtn {
margin-right: 10px;
}
#progress {
transition: width 0.25s ease-in-out;
}
const video = document.getElementById('video');
const playPauseBtn = document.getElementById('playPauseBtn');
const resetBtn = document.getElementById('resetBtn');
const progress = document.getElementById('progress');
const currentTimeDisplay = document.getElementById('currentTime');
const durationDisplay = document.getElementById('duration');
// Оновлення відображення часу та прогресу
video.addEventListener('timeupdate', () => {
const currentTime = video.currentTime;
const duration = video.duration;
const percentage = (currentTime / duration) * 100;
// Оновлюємо прогрес-бар
progress.style.width = `${percentage}%`;
// Оновлюємо поточний час і тривалість
currentTimeDisplay.textContent = currentTime.toFixed(2);
durationDisplay.textContent = duration.toFixed(2);
});
// Відтворення/паузи відео
playPauseBtn.addEventListener('click', () => {
if (video.paused) {
video.play();
playPauseBtn.textContent = 'Пауза';
} else {
video.pause();
playPauseBtn.textContent = 'Відтворити';
}
});
// Почати відео з початку
resetBtn.addEventListener('click', () => {
video.currentTime = 0;
video.play();
playPauseBtn.textContent = 'Пауза';
});
// Оновлення тривалості відео після завантаження метаданих
video.addEventListener('loadedmetadata', () => {
durationDisplay.textContent = video.duration.toFixed(2);
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.