CSS音乐播放器是一种使用CSS和HTML来创建的简单音乐播放界面,它通常不包含音频播放的实际功能,这部分功能由HTML5的<audio>元素提供。CSS用于控制播放器的外观和动画效果。
<audio> 元素:用于嵌入音频文件到网页中。以下是一个简单的CSS音乐播放器的示例代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Music Player</title>
<style>
.music-player {
display: flex;
align-items: center;
justify-content: center;
background: #f0f0f0;
padding: 20px;
}
.play-pause {
width: 50px;
height: 50px;
background: url('play-icon.png') no-repeat center center;
background-size: cover;
cursor: pointer;
}
.play-pause.playing {
background-image: url('pause-icon.png');
}
.progress {
width: 100%;
height: 5px;
background: #ddd;
margin-top: 10px;
}
.progress .bar {
height: 100%;
background: #007bff;
width: 0%;
}
</style>
</head>
<body>
<div class="music-player">
<div class="play-pause" id="playPause"></div>
<audio id="audioPlayer" src="music.mp3"></audio>
<div class="progress">
<div class="bar" id="progressBar"></div>
</div>
</div>
<script>
const audioPlayer = document.getElementById('audioPlayer');
const playPauseBtn = document.getElementById('playPause');
const progressBar = document.getElementById('progressBar');
playPauseBtn.addEventListener('click', () => {
if (audioPlayer.paused) {
audioPlayer.play();
playPauseBtn.classList.add('playing');
} else {
audioPlayer.pause();
playPauseBtn.classList.remove('playing');
}
});
audioPlayer.addEventListener('timeupdate', () => {
const progress = (audioPlayer.currentTime / audioPlayer.duration) * 100;
progressBar.style.width = `${progress}%`;
});
</script>
</body>
</html>这个示例代码展示了一个基本的音乐播放器,包括播放/暂停按钮和进度条。通过CSS控制播放器的外观,通过JavaScript控制音频播放和进度条更新。