一个可以和连接后台的音乐网站代码
时间: 2024-03-15 22:44:12 浏览: 111
音乐网站代码
4星 · 用户满意度95%
这是一个简单的音乐网站示例,使用了 HTML、CSS、JavaScript 和 PHP 技术,实现了连接后台的功能。这个示例包含了如下功能:
- 显示音乐列表
- 播放音乐
- 暂停音乐
- 播放下一首音乐
- 播放上一首音乐
- 音乐列表循环播放
- 搜索音乐
HTML 代码:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>音乐网站</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<div class="header">
<h1>音乐网站</h1>
<form class="search-form">
<input type="text" placeholder="搜索音乐">
<button type="submit">搜索</button>
</form>
</div>
<div class="music-list">
<ul></ul>
</div>
<div class="player">
<audio></audio>
<div class="controls">
<button class="prev-btn"></button>
<button class="play-btn"></button>
<button class="next-btn"></button>
</div>
<div class="progress">
<div class="progress-bar"></div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS 代码:
```
.container {
width: 80%;
margin: 0 auto;
font-family: Arial, sans-serif;
}
.header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20px;
}
.search-form {
display: flex;
align-items: center;
border: 1px solid #ccc;
border-radius: 5px;
padding: 5px;
}
.search-form input[type="text"] {
border: none;
outline: none;
width: 200px;
}
.search-form button[type="submit"] {
background-color: #ccc;
border: none;
outline: none;
padding: 5px 10px;
border-radius: 5px;
margin-left: 10px;
cursor: pointer;
}
.music-list ul {
list-style: none;
padding: 0;
}
.music-list li {
padding: 10px;
border-bottom: 1px solid #ccc;
cursor: pointer;
}
.music-list li:hover {
background-color: #f5f5f5;
}
.music-list li.active {
background-color: #ccc;
}
.player {
margin-top: 20px;
display: flex;
align-items: center;
justify-content: space-between;
}
.controls {
display: flex;
align-items: center;
}
.prev-btn, .play-btn, .next-btn {
width: 30px;
height: 30px;
background-size: contain;
background-repeat: no-repeat;
border: none;
outline: none;
cursor: pointer;
}
.prev-btn {
background-image: url("prev.png");
}
.play-btn {
background-image: url("play.png");
}
.pause-btn {
background-image: url("pause.png");
}
.next-btn {
background-image: url("next.png");
}
.progress {
width: 80%;
height: 5px;
background-color: #ccc;
margin-left: 20px;
position: relative;
}
.progress-bar {
height: 100%;
background-color: #2196f3;
position: absolute;
top: 0;
left: 0;
}
```
JavaScript 代码:
```
var audio = document.querySelector("audio");
var playBtn = document.querySelector(".play-btn");
var pauseBtn = document.querySelector(".pause-btn");
var prevBtn = document.querySelector(".prev-btn");
var nextBtn = document.querySelector(".next-btn");
var progressBar = document.querySelector(".progress-bar");
var musicList = document.querySelector(".music-list ul");
var musicItems = null;
var currentMusic = 0;
var musicListLoop = true;
// 获取音乐列表
function getMusicList() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "get_music_list.php");
xhr.onreadystatechange = function() {
if (xhr.readyState === xhr.DONE && xhr.status === 200) {
var musicListData = JSON.parse(xhr.responseText);
renderMusicList(musicListData);
playMusic(currentMusic);
}
};
xhr.send();
}
// 渲染音乐列表
function renderMusicList(musicListData) {
musicList.innerHTML = "";
for (var i = 0; i < musicListData.length; i++) {
var musicItem = document.createElement("li");
musicItem.innerHTML = musicListData[i].name;
musicList.appendChild(musicItem);
}
musicItems = musicList.querySelectorAll("li");
for (var i = 0; i < musicItems.length; i++) {
musicItems[i].addEventListener("click", function(event) {
currentMusic = this.getAttribute("data-index");
playMusic(currentMusic);
});
musicItems[i].setAttribute("data-index", i);
}
}
// 播放音乐
function playMusic(index) {
var musicItem = musicItems[index];
audio.src = musicItem.getAttribute("data-src");
audio.play();
musicItems[currentMusic].classList.remove("active");
musicItem.classList.add("active");
currentMusic = index;
playBtn.style.display = "none";
pauseBtn.style.display = "inline-block";
}
// 暂停音乐
function pauseMusic() {
audio.pause();
playBtn.style.display = "inline-block";
pauseBtn.style.display = "none";
}
// 播放下一首音乐
function playNextMusic() {
var nextMusic = currentMusic + 1;
if (nextMusic >= musicItems.length) {
if (musicListLoop) {
nextMusic = 0;
} else {
return;
}
}
playMusic(nextMusic);
}
// 播放上一首音乐
function playPrevMusic() {
var prevMusic = currentMusic - 1;
if (prevMusic < 0) {
if (musicListLoop) {
prevMusic = musicItems.length - 1;
} else {
return;
}
}
playMusic(prevMusic);
}
// 更新进度条
function updateProgressBar() {
var currentTime = audio.currentTime;
var duration = audio.duration;
var progress = (currentTime / duration) * 100;
progressBar.style.width = progress + "%";
}
// 搜索音乐
function searchMusic(keyword) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "search_music.php?keyword=" + keyword);
xhr.onreadystatechange = function() {
if (xhr.readyState === xhr.DONE && xhr.status === 200) {
var musicListData = JSON.parse(xhr.responseText);
renderMusicList(musicListData);
}
};
xhr.send();
}
// 初始化
function init() {
getMusicList();
// 按钮事件
playBtn.addEventListener("click", function(event) {
playMusic(currentMusic);
});
pauseBtn.addEventListener("click", function(event) {
pauseMusic();
});
prevBtn.addEventListener("click", function(event) {
playPrevMusic();
});
nextBtn.addEventListener("click", function(event) {
playNextMusic();
});
// 进度条事件
audio.addEventListener("timeupdate", function(event) {
updateProgressBar();
});
progressBar.addEventListener("click", function(event) {
var offsetX = event.offsetX;
var width = progressBar.offsetWidth;
var percent = offsetX / width;
var duration = audio.duration;
audio.currentTime = duration * percent;
});
// 搜索事件
var searchForm = document.querySelector(".search-form");
var searchInput = searchForm.querySelector("input[type='text']");
searchForm.addEventListener("submit", function(event) {
event.preventDefault();
var keyword = searchInput.value;
searchMusic(keyword);
});
}
init();
```
PHP 代码:
```
<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "music";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// 获取音乐列表
$sql = "SELECT * FROM music";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$musicList = array();
while($row = $result->fetch_assoc()) {
$music = array(
"name" => $row["name"],
"src" => $row["src"]
);
array_push($musicList, $music);
}
echo json_encode($musicList);
} else {
echo "0 results";
}
$conn->close();
?>
```
在这个示例中,音乐列表数据存储在 MySQL 数据库中,可以通过 PHP 脚本从数据库中获取音乐列表数据,并且返回给 JavaScript。同时,Html、CSS和JS相应的文件也需要在服务器上进行部署。这个示例中的 PHP 脚本需要根据实际情况进行修改,包括数据库连接信息、SQL 查询语句等。
阅读全文