JavaScript CSS jQuery 网页音乐播放器

效果展示

在这里插入图片描述

总结

  1. 音乐播放的功能基本实现了
  2. 播放器最小化充分利用了弹性盒子,很好用
  3. 专辑图像的旋转是使用CSS实现的,暂停的使用会有些接僵硬.改善的话可以用JavaScript定时器实现.
  4. 其他都还好,最重要的JavaScript写代码的思路要清晰,有条理,很关键
  5. 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;
}

/* div {
	margin: 0;
	outline: solid white 1px;
} */

/* .banner div {
	outline: none;
} */

/* .player div{
	border: none;
} */

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

//获取audio标签
const music = $(".player .playBtn .play audio")[0]
let playFlag = false
$(".banner").on("click", function(e) {
	//获取点击专辑的ID
	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()
		//获取当前正在播放歌曲的ID
		let musicID = getMusicID()
		//开启头像转动
		rotateFace(musicID)
	}

})


$(".player .playBtn .prev").on("click", function() {
	//获取当前正在播放歌曲的ID
	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() {
	//获取当前正在播放歌曲的ID
	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) {
	// console.log(albumID);
	let bgUrl = null
	if (albumID != 0) {
		bgUrl = "url(../img/" + albumID + "_bg.jpg)"
	}
	$(".container").css("background-image", bgUrl)
}

function rotateFace(albumID) {
	if (albumID != 0) {
		//去掉所有的rotate属性,然后再给特定的专辑加上rotate属性
		$(".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)"
		//获取头像元素and修改对应元素css属性
		$(".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) {
	//拼接mp3文件路径
	let mp3src = "../mp3/" + albumID + ".mp3"

	//设置audio标签的src属性
	music.src = mp3src
	music.setAttribute("data-id", albumID)
	//播放歌曲
	music.play()
	playFlag = true
}


function stopRotateFace() {
	//去掉所有的rotate属性,然后再给特定的专辑加上rotate属性
	$(".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)
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值