html代码
<div id="audio-btn" class="rotate">
<audio id="media" autoplay preload loop src="img/music.mp3"></audio>
</div>
js代码
function audioAutoPlay(id) {
var audio = document.getElementById(id);
var play = function () {
audio.play();
};
document.addEventListener("WeixinJSBridgeReady", play, false);
document.addEventListener('YixinJSBridgeReady', play, false);
play();
}
audioAutoPlay('media');
var audio = $('#media')[0];
$('#audio-btn').click(function(){
if($('#audio-btn').hasClass('rotate')) {
$('#audio-btn').removeClass('rotate');
audio.pause();
}else{
$('#audio-btn').addClass('rotate');
audio.play();
}
});
css样式+动画
#audio-btn {
position: fixed;
top: .8rem;
right: .8rem;
z-index: 10;
width: 1.25rem;
height: 1.25rem;
background: url(../img/btn-music.png) no-repeat center center;
background-size: 100% 100%;
-webkit-touch-callout: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.rotate {
animation:rotate-left 2s linear infinite;
-webkit-animation:rotate-left 2s linear infinite;
-moz-animation:rotate-left 2s linear infinite;
}
@keyframes rotate-left {
0%{
transform:rotate(360deg);
}
100%{
transform:rotate(0);
}
}