HTML部分
代码语言:javascript复制<audio id="music"
loop
src="http://sb.sycdn.kuwo.cn/83e006b5c69684edcda2a24b44643822/5cbbda8f/resource/n3/48/26/1720584363.mp3">
您的浏览器不支持播放
</audio>
CSS部分
代码语言:javascript复制<style>
span#music_ctrl {
background: url("img/music.png");
background-size: 30px 30px;
width: 30px;
height: 30px;
position: absolute;
top: 15px;
right: 15px;
border: 1px solid #cccccc;
border-radius: 50%;
/*animation: playing infinite linear 2s;*/
}
@keyframes playing {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
</style>
注意:music.png 随意去网上找一张自己中意 的即可。
JS
代码语言:javascript复制<script>
window.onload = () => {
let music = document.querySelector('#music');
let music_ctrl = document.querySelector('#music_ctrl');
// 为音乐按钮图标加一个点击事件,用来控制播放与否。
music_ctrl.addEventListener('click', () => {
if (music.paused) {
music.play();
music_ctrl.style.animation = 'playing infinite linear 2s';
} else {
music.pause();
music_ctrl.style.animation = 'none';
}
});
}
</script>
效果