<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>模拟播放暂停特效</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
.player-container {
text-align: center;
background-color: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.play-btn {
width: 80px;
height: 80px;
background-color: #4CAF50;
border-radius: 50%;
border: none;
color: white;
font-size: 24px;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
margin-bottom: 20px;
}
.play-btn:hover {
background-color: #45a049;
transform: scale(1.05);
}
.play-btn:active {
transform: scale(0.95);
}
.play-icon, .pause-icon {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
transition: opacity 0.3s ease;
}
.pause-icon {
opacity: 0;
}
.playing .play-icon {
opacity: 0;
}
.playing .pause-icon {
opacity: 1;
}
.progress-container {
width: 300px;
height: 6px;
background-color: #ddd;
border-radius: 3px;
margin: 20px auto;
overflow: hidden;
}
.progress-bar {
height: 100%;
width: 0%;
background-color: #4CAF50;
transition: width 0.1s linear;
}
.time-display {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: #666;
font-size: 14px;
}
.title {
margin-bottom: 20px;
color: #333;
}
.footer {
margin-top: 30px;
font-size: 12px;
color: #999;
}
.footer a {
color: #4CAF50;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<div class="player-container">
<h2 class="title">音乐播放器</h2>
<button class="play-btn" id="playButton">
<div class="play-icon">▶</div>
<div class="pause-icon">❚❚</div>
</button>
<div class="progress-container">
<div class="progress-bar" id="progressBar"></div>
</div>
<div class="time-display">
<span id="currentTime">0:00</span>
<span id="duration">3:45</span>
</div>
<div class="footer">
</div>
</div>
<script>
const playButton = document.getElementById('playButton');
const progressBar = document.getElementById('progressBar');
const currentTimeDisplay = document.getElementById('currentTime');
const durationDisplay = document.getElementById('duration');
let isPlaying = false;
let progress = 0;
let interval;
// 模拟音频时长 (3分45秒)
const totalDuration = 225; // 秒
// 格式化时间为 MM:SS
function formatTime(seconds) {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs < 10 ? '0' : ''}${secs}`;
}
// 更新进度条
function updateProgress() {
progress += 0.1;
const percentage = (progress / totalDuration) * 100;
progressBar.style.width = `${percentage}%`;
currentTimeDisplay.textContent = formatTime(progress);
if (progress >= totalDuration) {
clearInterval(interval);
isPlaying = false;
playButton.classList.remove('playing');
progress = 0;
progressBar.style.width = '0%';
currentTimeDisplay.textContent = '0:00';
}
}
// 播放/暂停按钮点击事件
playButton.addEventListener('click', function() {
isPlaying = !isPlaying;
if (isPlaying) {
playButton.classList.add('playing');
interval = setInterval(updateProgress, 100);
} else {
playButton.classList.remove('playing');
clearInterval(interval);
}
});
// 初始化显示总时长
durationDisplay.textContent = formatTime(totalDuration);
</script>
</body>
</html>
1万+

被折叠的 条评论
为什么被折叠?



