本作品源代码GitHub:点击查看本作品源代码
迷你音乐播放器
transform: translate(0rem, -.15rem); /* 代替绝对定位 */
@font-face {
font-family: 'iconfont';
src: url('../iconfont/iconfont.eot');
src: url('../iconfont/iconfont.eot?#iefix') format('embedded-opentype'),
url('../iconfont/iconfont.woff') format('woff'),
url('../iconfont/iconfont.ttf') format('truetype'),
url('../iconfont/iconfont.svg#iconfont') format('svg');
}
.iconfont{
font-family:"iconfont" !important;
font-size: 1.5rem;
font-style: normal;
-webkit-font-smoothing: antialiased;
-webkit-text-stroke-width: 0.2px;
-moz-osx-font-smoothing: grayscale;
color: #fff;
}
* {
padding: 0;
margin: 0;
}
body {
background: url("../images/bg.jpg");
}
header {
width: 100%;
height: 2rem;
text-align: center;
color: #fff;
}
/* 自定义字体,阿里图标 */
@font-face {
font-family: 'iconfont';
src: url('../iconfont/iconfont.eot');
src: url('../iconfont/iconfont.eot?#iefix') format('embedded-opentype'),
url('../iconfont/iconfont.woff') format('woff'),
url('../iconfont/iconfont.ttf') format('truetype'),
url('../iconfont/iconfont.svg#iconfont') format('svg');
}
.iconfont{
font-family:"iconfont" !important;
font-size: 1.5rem;
font-style: normal;
-webkit-font-smoothing: antialiased;
-webkit-text-stroke-width: 0.2px;
-moz-osx-font-smoothing: grayscale;
color: #fff;
}
header h3 {
font-size: 0.5rem;
line-height: 1.3rem;
}
header p {
font-size: 0.3rem;
}
.singer {
width: 8rem;
height: 8rem;
border: 1px solid #fff;
margin: auto;
border-radius: 50%;
}
.singer div {
width: 7.6rem;
height: 7.6rem;
margin: .2rem;
border-radius: 50%;
background: rgba(255, 255, 255, .5);
}
.rotate {
animation: rot 10s linear infinite;
}
@keyframes rot {
to {
transform: rotate(360deg);
}
}
.singer div img {
width: 7rem;
height: 7rem;
border-radius: 50%;
margin: 0.3rem;
}
.progress {
width: 9rem;
height: 0.1rem;
background: #c03;
margin: 1rem auto;
}
.bg {
width: 0;
height: 0.1rem;
background: #0f9
}
.bar {
width: .2rem;
height: .2rem;
background: blue;
border-radius: 50%;
transform: translate(0rem, -.15rem); /* 代替绝对定位 */
}
.btn {
width: 8rem;
margin: auto;
}
.btn div {
float: left;
margin: .5rem;
}
.pause {
display: none;
}
var oPlay = document.getElementsByClassName("play")[0];
var oPause = document.getElementsByClassName("pause")[0];
/* 点击实现播放功能 */
oPlay.onclick = function () {
myMusic.play();
oPause.style.display = "block";
this.style.display = "none";
oSinger.className = "singer rotate";
};
/* 点击实现暂停功能 */
oPause.onclick = function () {
myMusic.pause();
oPlay.style.display = "block";
this.style.display = "none";
oSinger.className = "singer";
};
/* 进度条功能 */
var w = oProgress.offsetWidth - oBar.offsetWidth; //计算小滑块可移动的范围
myMusic.addEventListener("timeupdate", function () {
var s = this.currentTime/this.duration; //当前时间/总时间
oBar.style.transform = "translate("+s*w +"px, -0.15rem)"; //滑块随音乐移动
oBg.style.width = s*w + "px";
});
//触屏事件拖拽进度条
var x = 0, l = 0, _left = 0;
oBar.addEventListener("touchstart", function (e) {
var x = e.changedTouches[0].pageX; //获取手指按下去的位置
document.addEventListener("touchmove", move);
document.addEventListener("touchend", function () { //触屏结束后需要解绑触屏事件
l = _left;
});
});
function move(e) {
var x1 = e.changedTouches[0].pageX;
var _left = x1 -x + l;
if (_left < 0) {
_left = 0;
} else if (_left > w) {
_left = w;
}
oBar.style.transform = "translate("+_left+"px, -0.15rem)";
oBg.style.width = _left + "px";
myMusic.currentTime = _left/w*myMusic.duration;
}
//自动播放下一曲
myMusic.addEventListener("ended", function (e){
n ++;
if (n > 4) {
myMusic.pause();
myMusic.src = "";
oSinger.className = "singer";
oBg.style.width = 0;
oBar.style.transform = "translate("+0+"px, -0.15rem)";
oPlay.style.display = "block";
oPause.style.display = "none";
}
myMusic.src = "mp3/" +musicList[n];
myMusic.play();
});
//用于显示当前播放的歌曲名
myMusic.addEventListener("canplay", function () {
oMusicTitle.innerHTML = musicList[n];
});
window.onload = function () {
var myMusic = document.getElementById("myMusic");
var oPlay = document.getElementsByClassName("play")[0];
var oPause = document.getElementsByClassName("pause")[0];
var oProgress = document.getElementsByClassName("progress")[0];
var oBar = document.getElementsByClassName("bar")[0];
var oBg = document.getElementsByClassName("bg")[0];
var oSinger = document.getElementsByClassName("singer")[0];
var oPrve = document.getElementsByClassName("prev")[0];
var oNext = document.getElementsByClassName("next")[0];
var oMusicTitle = document.getElementsByClassName("musicTitle")[0];
/* 点击实现播放功能 */
oPlay.onclick = function () {
myMusic.play();
oPause.style.display = "block";
this.style.display = "none";
oSinger.className = "singer rotate";
};
/* 点击实现暂停功能 */
oPause.onclick = function () {
myMusic.pause();
oPlay.style.display = "block";
this.style.display = "none";
oSinger.className = "singer";
};
/* 进度条功能 */
var w = oProgress.offsetWidth - oBar.offsetWidth; //计算小滑块可移动的范围
myMusic.addEventListener("timeupdate", function () {
var s = this.currentTime/this.duration; //当前时间/总时间
oBar.style.transform = "translate("+s*w +"px, -0.15rem)"; //滑块随音乐移动
oBg.style.width = s*w + "px";
});
//触屏事件拖拽进度条
var x = 0, l = 0, _left = 0;
oBar.addEventListener("touchstart", function (e) {
var x = e.changedTouches[0].pageX; //获取手指按下去的位置
document.addEventListener("touchmove", move);
document.addEventListener("touchend", function () {
l = _left;
});
});
function move(e) {
var x1 = e.changedTouches[0].pageX;
var _left = x1 -x + l;
if (_left < 0) {
_left = 0;
} else if (_left > w) {
_left = w;
}
oBar.style.transform = "translate("+_left+"px, -0.15rem)";
oBg.style.width = _left + "px";
myMusic.currentTime = _left/w*myMusic.duration;
}
//用数组保存音乐
var musicList = ["儿童歌曲-捉泥鳅.mp3", "李茂山-迟来的爱.mp3", "宋祖英-好日子.mp3",
"谭嘉仪-小幸运.mp3", "王菲-爱不可及.mp3"];
var n = 0; //索引
//页面加载完成后显示的歌曲
oMusicTitle.innerHTML = musicList[n];
myMusic.src = "mp3/"+ musicList[n];
if (myMusic.currentTime > myMusic.duration) {
n ++;
myMusic.src = "mp3/"+ musicList[n];
myMusic.play();
}
//上一曲功能
oPrve.onclick = function () {
n --;
if (n < 0) {
n = 4;
}
myMusic.src = "mp3/"+ musicList[n];
myMusic.play();
oSinger.className = "singer rotate";
oPause.style.display = "block";
oPlay.style.display = "none";
};
//下一曲功能
oNext.onclick = function () {
n++;
if (n > 4) {
n = 0;
}
myMusic.src = "mp3/" +musicList[n];
myMusic.play();
oSinger.className = "singer rotate";
oPause.style.display = "block";
oPlay.style.display = "none";
};
//自动播放下一曲
myMusic.addEventListener("ended", function (e){
n ++;
if (n > 4) {
myMusic.pause();
myMusic.src = "";
oSinger.className = "singer";
oBg.style.width = 0;
oBar.style.transform = "translate("+0+"px, -0.15rem)";
oPlay.style.display = "block";
oPause.style.display = "none";
}
myMusic.src = "mp3/" +musicList[n];
myMusic.play();
});
//用于显示当前播放的歌曲名
myMusic.addEventListener("canplay", function () {
oMusicTitle.innerHTML = musicList[n];
});
};