用html,jscode,css语言写一个音乐播放器界面的代码,包含进度条显示,切换上一曲下一曲,播放列表,以及歌词滚动功能
时间: 2023-12-01 21:02:42 浏览: 114
速来!!!JS+HTML+CSS歌曲播放界面+歌词滚动效果
以下是一个简单的音乐播放器界面代码,包含进度条显示,切换上一曲下一曲,播放列表,以及歌词滚动功能:
HTML 代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Music Player</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<div class="header">
<h1>Music Player</h1>
</div>
<div class="main">
<div class="player">
<div class="controls">
<button class="prev" onclick="prevSong()">Prev</button>
<button class="play" onclick="playPause()">Play</button>
<button class="next" onclick="nextSong()">Next</button>
</div>
<div class="info">
<div class="song-title">Song Title</div>
<div class="artist">Artist</div>
</div>
<div class="progress-container">
<div class="progress"></div>
</div>
</div>
<div class="playlist">
<h2>Playlist</h2>
<ul>
<li onclick="playSong('song1.mp3')">Song 1</li>
<li onclick="playSong('song2.mp3')">Song 2</li>
<li onclick="playSong('song3.mp3')">Song 3</li>
<li onclick="playSong('song4.mp3')">Song 4</li>
</ul>
</div>
<div class="lyrics">
<h2>Lyrics</h2>
<div class="lyrics-container">
<div class="lyrics-text">Lyrics Text</div>
</div>
</div>
</div>
</div>
<audio id="myAudio" src=""></audio>
<script src="script.js"></script>
</body>
</html>
```
CSS 代码:
```css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: Arial, sans-serif;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.header {
text-align: center;
margin-bottom: 20px;
}
.main {
display: flex;
flex-wrap: wrap;
}
.player {
flex-basis: 70%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-right: 20px;
}
.controls {
display: flex;
align-items: center;
margin-bottom: 20px;
}
.controls button {
font-size: 16px;
padding: 10px;
margin-right: 10px;
border-radius: 5px;
border: none;
background-color: #333;
color: #fff;
cursor: pointer;
}
.controls button:hover {
background-color: #555;
}
.info {
text-align: center;
margin-bottom: 20px;
}
.song-title {
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
.artist {
font-size: 18px;
color: #999;
}
.progress-container {
width: 100%;
height: 5px;
background-color: #ccc;
}
.progress {
width: 0%;
height: 100%;
background-color: #333;
}
.playlist {
flex-basis: 30%;
}
.playlist h2 {
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
.playlist ul {
list-style: none;
margin: 0;
padding: 0;
}
.playlist li {
font-size: 18px;
padding: 10px;
background-color: #f9f9f9;
border-bottom: 1px solid #ccc;
cursor: pointer;
}
.playlist li:hover {
background-color: #eee;
}
.lyrics {
flex-basis: 100%;
margin-top: 20px;
}
.lyrics h2 {
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
.lyrics-container {
height: 200px;
overflow: auto;
background-color: #f9f9f9;
border: 1px solid #ccc;
padding: 10px;
}
.lyrics-text {
font-size: 18px;
line-height: 1.5;
}
```
JavaScript 代码:
```javascript
var songs = ['song1.mp3', 'song2.mp3', 'song3.mp3', 'song4.mp3'];
var currentSong = 0;
var myAudio = document.getElementById('myAudio');
var playBtn = document.querySelector('.play');
var songTitle = document.querySelector('.song-title');
var artist = document.querySelector('.artist');
var progress = document.querySelector('.progress');
var lyrics = document.querySelector('.lyrics-text');
function playSong(song) {
currentSong = songs.indexOf(song);
myAudio.src = song;
myAudio.play();
playBtn.innerHTML = 'Pause';
}
function playPause() {
if (myAudio.paused) {
myAudio.play();
playBtn.innerHTML = 'Pause';
} else {
myAudio.pause();
playBtn.innerHTML = 'Play';
}
}
function prevSong() {
currentSong--;
if (currentSong < 0) {
currentSong = songs.length - 1;
}
playSong(songs[currentSong]);
}
function nextSong() {
currentSong++;
if (currentSong >= songs.length) {
currentSong = 0;
}
playSong(songs[currentSong]);
}
myAudio.addEventListener('timeupdate', function() {
var position = myAudio.currentTime / myAudio.duration;
progress.style.width = position * 100 + '%';
});
myAudio.addEventListener('ended', function() {
nextSong();
});
function showLyrics() {
lyrics.innerHTML = 'Lyrics Text';
lyrics.scrollTop = 0;
}
playSong(songs[currentSong]);
showLyrics();
```
在这个代码中,我们使用了一个数组 `songs` 来存储所有的歌曲,`currentSong` 变量来表示当前播放的歌曲。`playSong()` 函数用于播放指定的歌曲,`playPause()` 函数用于播放/暂停歌曲,`prevSong()` 和 `nextSong()` 函数用于切换上一曲和下一曲。我们还使用了 `timeupdate` 事件来更新进度条,`ended` 事件来自动播放下一曲。
在 HTML 中,我们使用了 `<audio>` 元素来播放音乐,使用了 `<button>` 元素来控制播放器的功能,并使用了 `<ul>` 元素来显示播放列表。我们还添加了一个 `<div>` 元素来显示歌词,并使用了 JavaScript 中的 `scrollTop` 属性来实现歌词滚动。
阅读全文