Blog 音乐播放卡片样式
13
2024-07-22
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>音乐播放卡片</title><!-- 引入 Font Awesome CSS 文件 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"
integrity="sha512-abcdefg..." crossorigin="anonymous">
<style>
/* 样式开始 */
.music-card1 {
background-color: #282828;
/* 卡片背景色 */
border-radius: 10px;
/* 边框圆角 */
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
/* 阴影效果 */
overflow: hidden;
width: 300px;
/* 卡片宽度 */
height: 300px;
/* 卡片高度 */
margin: 30px auto;
/* 外边距,使卡片居中 */
position: relative;
}
.album-cover1 img {
width: 100%;
height: 100%;
object-fit: cover;
/* 图片填充方式 */
}
.overlay1 {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
/* 覆盖层高度 */
background: linear-gradient(to bottom, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.5) 100%);
/* 渐变遮罩层 */
z-index: 1;
/* 叠放顺序 */
pointer-events: none;
/* 不接受鼠标事件 */
}
.controls1 {
position: absolute;
bottom: 20px;
/* 按钮距底部距离 */
right: 10px;
/* 按钮距右侧距离 */
z-index: 2;
/* 叠放顺序 */
}
#playPauseButton1 {
background-color: transparent;
border: none;
cursor: pointer;
font-size: 50px;
color: #fff;
transition: color 0.3s ease;
width: 50px;
/* 增加按钮宽度以容纳图标 */
height: 50px;
/* 增加按钮高度以容纳图标 */
display: flex;
justify-content: center;
align-items: center;
}
#playPauseButton1:hover {
color: #ff4a4a;
}
/* 样式结束 */
</style>
</head>
<body>
<div class="music-card1">
<div class="album-cover1">
<img src="/upload/cover.jpg" alt="Album Cover">
</div>
<div class="overlay1"></div>
<div class="controls1">
<!-- 使用 Font Awesome 的播放图标 -->
<button id="playPauseButton1" class="fas fa-play"></button>
</div>
<script>
// JavaScript代码开始
let audio = new Audio('http://music.mp3');
let isPlaying = false;
const playPauseButton1 = document.getElementById('playPauseButton1');
playPauseButton1.addEventListener('click', () => {
if (isPlaying) {
audio.pause();
playPauseButton1.classList.remove('fa-pause');
playPauseButton1.classList.add('fa-play');
} else {
audio.play();
playPauseButton1.classList.remove('fa-play');
playPauseButton1.classList.add('fa-pause');
}
isPlaying = !isPlaying; // 更新播放状态
});
audio.addEventListener('ended', () => {
isPlaying = false;
playPauseButton1.classList.remove('fa-pause');
playPauseButton1.classList.add('fa-play');
});
// JavaScript代码结束
</script>
</div>
</body>
</html>
- 0
- 0
-
分享