效果展示
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210527215220899.gif#pic_center)
总结
- 音乐播放的功能基本实现了
- 播放器最小化充分利用了弹性盒子,很好用
- 专辑图像的旋转是使用CSS实现的,暂停的使用会有些接僵硬.改善的话可以用JavaScript定时器实现.
- 其他都还好,最重要的JavaScript写代码的思路要清晰,有条理,很关键
- C币大佬:https://download.csdn.net/download/zhuoss/19135697
index.html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" type="text/css" href="index.css" />
<title></title>
</head>
<body>
<div class="container">
<div class="banner" id="banner" data-id="0">
<div class="album1" data-id="1"></div>
<div class="album2" data-id="2"></div>
<div class="album3" data-id="3"></div>
<div class="album4" data-id="4"></div>
<div class="album5" data-id="5"></div>
</div>
<div class="player">
<div class="mini">
</div>
<div class="picture">
</div>
<div class="title">
龙卷风-邓紫棋
</div>
<div class="playBtn">
<div class="prev">
</div>
<div class="play">
<audio src="../mp3/1.mp3" data-id="1">
</audio>
</div>
<div class="next">
</div>
</div>
<div class="miniBtn">
</div>
</div>
</div>
<script src="jquery.js" type="text/javascript" charset="utf-8"></script>
<script src="index.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>
index.css
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
overflow: hidden;
}
.container {
height: 100%;
background-image: url(../img/0_bg.gif);
background-repeat: no-repeat;
background-size: cover;
}
.banner {
background-image: url(../img/m_bg.jpg);
background-repeat: no-repeat;
background-size: cover;
width: 1000px;
height: 250px;
position: relative;
top: 100px;
}
.banner div {
width: 100px;
height: 100px;
position: absolute;
border-radius: 100px;
}
.banner .album3 {
width: 125px;
height: 125px;
position: absolute;
border-radius: 125px;
top: 70px;
left: 320px;
background-image: url(../img/3.jpg);
background-repeat: no-repeat;
background-size: cover;
}
.banner .album1 {
width: 80px;
height: 80px;
top: 10px;
left: 90px;
position: absolute;
background-image: url(../img/1.jpg);
background-repeat: no-repeat;
background-size: cover;
}
.rotate {
transform: rotate(360deg);
animation: rotation 8s linear infinite;
}
@keyframes rotation {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.banner .album2 {
width: 100px;
height: 100px;
top: 130px;
left: 160px;
background-image: url(../img/2.jpg);
background-repeat: no-repeat;
background-size: cover;
}
.banner .album4 {
width: 100px;
height: 100px;
top: 15px;
left: 500px;
background-image: url(../img/4.jpg);
background-repeat: no-repeat;
background-size: cover;
}
.banner .album5 {
width: 90px;
height: 90px;
top: 130px;
left: 600px;
background-image: url(../img/2.jpg);
background-repeat: no-repeat;
background-size: cover;
}
.player {
height: 100px;
position: absolute;
bottom: 100px;
display: flex;
align-items: center;
justify-content: space-between;
background-color: rgba(0, 0, 0, 1);
}
.player .picture {
background-image: url(../img/1.jpg);
background-repeat: no-repeat;
background-size: cover;
width: 100px;
height: 100px;
}
.player .title {
width: 200px;
height: 100px;
font-size: 25px;
color: white;
text-align: center;
line-height: 100px;
}
.player .playBtn {
width: 200px;
height: 100px;
display: flex;
align-items: center;
justify-content: space-around;
}
.player .miniBtn {
width: 20px;
height: 100px;
background-image: url(../img/player_bg.png);
background-repeat: no-repeat;
background-position: -2px -5px;
}
.player .miniBtn:hover {
filter: brightness(2)
}
.player .playBtn .prev {
width: 40px;
height: 40px;
background-image: url(../img/player_bg.png);
background-repeat: no-repeat;
background-position: -70px 4px;
}
.player .playBtn .prev:hover {
background-image: url(../img/player_bg.png);
background-repeat: no-repeat;
background-position: -70px -26px;
}
.player .playBtn .play {
width: 40px;
height: 40px;
background-image: url(../img/player_bg.png);
background-repeat: no-repeat;
background-position: -105px 0px;
}
.player .playBtn .play:hover {
background-image: url(../img/player_bg.png);
background-repeat: no-repeat;
background-position: -105px -43px;
}
.player .playBtn .next {
width: 40px;
height: 40px;
background-image: url(../img/player_bg.png);
background-repeat: no-repeat;
background-position: -146px 5px;
}
.player .playBtn .next:hover {
background-image: url(../img/player_bg.png);
background-repeat: no-repeat;
background-position: -146px -25px;
}
index.js
const music = $(".player .playBtn .play audio")[0]
let playFlag = false
$(".banner").on("click", function(e) {
let albumID = $(e.target).data("id");
switchBg(albumID)
rotateFace(albumID)
switchPlayFace(albumID)
swichPlayTitle(albumID)
swichPlayBtn(albumID)
playSong(albumID)
})
$(".player .playBtn .play").on("click", function() {
if (playFlag == true) {
music.pause()
playFlag = false
stopRotateFace()
togglePlayButton()
} else {
music.play()
playFlag = true
togglePlayButton()
let musicID = getMusicID()
rotateFace(musicID)
}
})
$(".player .playBtn .prev").on("click", function() {
let musicID = getMusicID()
if (musicID == 1) {
musicID = 5
} else {
musicID--
}
switchBg(musicID)
rotateFace(musicID)
switchPlayFace(musicID)
swichPlayTitle(musicID)
swichPlayBtn(musicID)
playSong(musicID)
})
$(".player .playBtn .next").on("click", function() {
let musicID = getMusicID()
if (musicID == 5) {
musicID = 1
} else {
musicID++
}
switchBg(musicID)
rotateFace(musicID)
switchPlayFace(musicID)
swichPlayTitle(musicID)
swichPlayBtn(musicID)
playSong(musicID)
})
$(".player .miniBtn").on("click", function() {
$(".player .picture").toggle(1000)
$(".player .title").toggle(1000)
$(".player .playBtn").toggle(1000)
$(".player .miniBtn").css("background-position", "-48px -5px")
})
function switchBg(albumID) {
let bgUrl = null
if (albumID != 0) {
bgUrl = "url(../img/" + albumID + "_bg.jpg)"
}
$(".container").css("background-image", bgUrl)
}
function rotateFace(albumID) {
if (albumID != 0) {
$(".banner").children().removeClass("rotate")
let albumClass = ".album" + albumID
$(albumClass).addClass("rotate")
}
}
function switchPlayFace(albumID) {
if (albumID != 0) {
let albumClass = "album" + albumID
let picUrl = "url(../img/" + albumID + ".jpg)"
$(".player .picture").css("background-image", picUrl)
}
}
function swichPlayTitle(albumID) {
if (albumID != 0) {
let songTitle = "歌曲名称" + albumID
$(".player .title").text(songTitle)
}
}
function swichPlayBtn(albumID) {
$(".player .playBtn .play").css("background-position", "-335px -90px")
}
function playSong(albumID) {
let mp3src = "../mp3/" + albumID + ".mp3"
music.src = mp3src
music.setAttribute("data-id", albumID)
music.play()
playFlag = true
}
function stopRotateFace() {
$(".banner").children().removeClass("rotate")
}
function togglePlayButton() {
if (playFlag == true) {
$(".player .playBtn .play").css("background-position", "-335px -90px")
} else {
$(".player .playBtn .play").css("background-position", "-105px 0px")
}
}
function getMusicID() {
return Number($(".player audio")[0].dataset.id)
}