Home  >  Article  >  Web Front-end  >  HTML5 combined with javascript to implement a simple music player

HTML5 combined with javascript to implement a simple music player

王林
王林forward
2020-12-22 09:54:162482browse

HTML5 combined with javascript to implement a simple music player

Let’s take a look at the final implementation first:

(Learning video sharing: html5 video tutorial)

HTML5 combined with javascript to implement a simple music player

1. HTML code

<div id="music">
	<div id="container">
		<h3 id="musicName">一月,银装轻舞-紫竹笛韵</h3>
		<img  src="image/一月,银装轻舞-紫竹笛韵.jpg" id="musicImg" alt="HTML5 combined with javascript to implement a simple music player" >
		<audio src="./music/一月,银装轻舞-紫竹笛韵.mp3" controls id="audio"></audio>
		<div class="btn">
			<button id="play">play</button>
			<button id="pause">pause</button>
			<button id="prev">prev</button>
			<button id="next">next</button>
		</div>
	</div>
</div>

2. Implementation of playback and pause switching effects

// 播放
play.onclick = function(){
	if(audio.paused){
		audio.play();
	}
}
 
// 暂停
pause.onclick = function(){
	if(audio.played){
		audio.pause();
	}
}

Automatically switch to the next song

audio.addEventListener(&#39;ended&#39;,function(){
	next.onclick();
},false);

3.When switching songs The song image and the current background also change accordingly

// 上一首
prev.onclick = function(){
	num = (num + len - 1) % len;
	audio.src = &#39;./music/&#39; + music[num] + &#39;.mp3&#39;;
	musicName.innerHTML = music[num];
	bgImage.style.backgroundImage = &#39;url(./image/&#39; + music[num] + &#39;.jpg)&#39;;
	musicImg.src = &#39;./image/&#39; + music[num] + &#39;.jpg&#39;;
	audio.play();
}
 
// 下一首
next.onclick = function(){
	num = (num + 1) % len;
	audio.src = &#39;./music/&#39; + music[num] + &#39;.mp3&#39;;
	musicName.innerHTML = music[num];
	bgImage.style.backgroundImage = &#39;url(./image/&#39; + music[num] + &#39;.jpg)&#39;;
	musicImg.src = &#39;./image/&#39; + music[num] + &#39;.jpg&#39;;
	audio.play();
}

4. To achieve transparent background image and opaque content effect

#music {
	width: 500px;
	height: 500px;
	border-radius: 10px;
	margin: 20px auto;
	position: relative;
	background: url(./image/一月,银装轻舞-紫竹笛韵.jpg) no-repeat;
	background-size: cover;
	text-align: center;
}
#container {
	position: absolute;
	left: 0;
	right: 0;
	top: 0;
	bottom: 0;
	width: 500px;
	height: 500px;
	text-align: center;
	background:rgba(255,255,255,0.6);
}

Related recommendations: html5 tutorial

The above is the detailed content of HTML5 combined with javascript to implement a simple music player. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete