自动播放背景音乐

本文介绍了如何使用HTML、JavaScript和CSS实现网页背景音乐的自动播放功能,包括html代码段、js代码段以及用于控制音乐播放和动画效果的css样式。

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);
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值