根据老黑的教程制作的播放器。
本帖最后由 加林森 于 2022-2-20 12:22 编辑 <br /><br /><style type="text/css">/*2022*/#waiDiv { margin:10px; float:right; padding:8px; width:400px; background:#696969; border-radius:10px; box-shadow:2px 2px 4px #000; display:flex; flex-direction:column; position:relative; }
#audDiv { width:100%; display:flex; flex-direction:row; align-items:center; font-size:10px; position: relative; }
#add1 { margin-left: 12px; background:#eee; text-align:center; width:20px; height:20px; line-height:20px; border-radius:50%; cursor:pointer; }
#add1:hover { color: red; }
#mLiDiv { margin-top: 10px; background:#eee; color:#000; min-height:100px; padding:10px; border:1px solid olive; font-size:14px; column-count: 2;}
#mLiDiv a { text-decoration: none; cursor:pointer; }
#mLiDiv a:hover { color:red; }
#prompt { position:absolute; left:200px; top:50px; width:400px; padding:6px 12px; background:silver; font-size:12px; display:none; box-shadow:1px 1px 1px #666; border-radius:2px; }
#prompt input { outline:none; }
#prompt input { margin:4px;padding:4px; width:392px; }
#prompt input { border-radius:4px; cursor:pointer;border:1px solid gray; border-radius:3px; box-shadow: 1px 1px 2px #444; }
#audDiv input, #audDiv input { cursor:pointer; }
#paDiv { margin: auto; width: 220px; display: flex; align-items: center; border: 1px solid olive; border-radius: 8px 0px 8px 0px; background: rgba(0,0,0,.8); box-shadow: 1px 1px 2px #000; position: relative; }
#jindu { position: relative; width: 200px; height: 8px; line-height: 8px; font-size: 10px; color: #eee; text-align: center; background: linear-gradient(90deg, olive, green) no-repeat; background-size: 8px 0px; cursor: pointer; }
#btn-ro { width: 20px; height: 20px; line-height: 20px; font-size: 12px; background: linear-gradient(blue, silver, red); outline:none; color: white; border-radius: 50%; text-align: center; cursor: pointer; animation: rol linear 2s infinite; }
#btn-ro:hover { opacity: 0.8; }
#btn-ro:active { opacity: 1; }
@keyframes rol { to { transform:rotate(360deg); } }</style><div style="position:relative;left:-203px;width:1000px;height:750px;background:#eee url('https://pic.imgdb.cn/item/620ce9492ab3f51d9167e772.jpg') no-repeat; border:2px solid tan;">
<div id="waiDiv"><div id="audDiv"><div id="paDiv"><div id="btn-ro">·</div><div id="jindu"><div id="jd-go"></div></div></div><input id="muted" type="checkbox" onclick="aud.muted = this.checked ? true : false" />静音<input id="dqxh" type="radio" name="rad" checked="checked" onclick="howplay()" />单曲<input id="lhbf" type="radio" name="rad" onclick="howplay()" />轮播 <div id="add1">+</div></div><div id="mLiDiv"></div><div id="prompt"><div>添歌</div><input type="text" id="mName" placeholder="歌曲名称" /><br /><input type="text" id="mUrl" placeholder="歌曲地址" /><br /><div style="text-align:center;"><input id="subMe" type="button" value=" 添加 " /><input id="cancelMe" type="button" value=" 算了 " /></div></div></div></div><script>/*2022*/
var muAr = [
["https://www.joy127.com/url/88664.mp3","旷野的呼唤"],
["https://www.joy127.com/url/88658.mp3","兰亭序"],
["https://www.joy127.com/url/88656.mp3","成瘾成疯又成魔(R7版)"],
["https://www.joy127.com/url/88654.mp3","分开的那天"],
["https://www.joy127.com/url/88660.mp3","毒药"],
["https://www.joy127.com/url/88653.mp3","阿尔山的姑娘"],
["https://www.joy127.com/url/88651.mp3","遥远的她"],
["https://www.joy127.com/url/88652.mp3","情人知己"],
["https://www.joy127.com/url/88649.mp3","突然的幸福"],
["https://www.joy127.com/url/88642.mp3","特别的爱给特别的你"],
["https://www.joy127.com/url/88645.mp3","这个世界"]
];
var btn = document.getElementById('btn-ro');
var jindu = document.getElementById('jindu');
var aud = document.getElementById('myPlayer');
var mLi = document.getElementById('mLiDiv');
var mAdd = document.getElementById('add1');
var prom = document.getElementById('prompt');
var cancelMe = document.getElementById('cancelMe');
var subMe = document.getElementById('subMe');
var dqxh = document.getElementById('dqxh');
var lhbf = document.getElementById('lhbf');
var aud = document.createElement('audio');
aud.loop = true;
var playIdx = 0; //当前曲索引
if(aud.paused) btn.style.animationPlayState="paused";
//写歌单
var str = "";
for(j=0; j<muAr.length; j++) {
str += (j+1) + ".<a id='list" + j + "' onclick='iPlay(" + j + ");howplay();' >" + muAr + "</a><br />";
}
mLi.innerHTML += str;
function iPlay(idx){ //播放函数
playIdx = idx;
aud.src = muAr;
aud.play();
aud.addEventListener('timeupdate', tmMsg, true);
aud.addEventListener('ended', function() { btn.style.animationPlayState="paused"; }, true);
btn.style.animationPlayState="running";
nameRed(playIdx);
}
function howplay(){ //单曲&轮播处理
dqxh.checked ? aud.loop = true : (aud.loop = false,playNext(),aud.addEventListener('ended',playNext,false));
prom.style.display = "none";
}
function playNext(){ //处理下一首
if(aud.paused) iPlay(playIdx);
playIdx += 1;
if(playIdx >= muAr.length) playIdx = 0;
}
function nameRed(){ //歌单着色
for(k=0;k<muAr.length;k++){
let listId = "list" + k;
document.getElementById(listId).style.removeProperty("color");
}
listId = "list" + playIdx;
document.getElementById(listId).style.color = "red";
}
function tmMsg(){ //进度条
let auT = Math.floor(aud.duration - aud.currentTime);
let auM = auT / 60;
let auMs = parseInt(auM);
if (auMs <10) auMs = "0" + auMs;
let auS = auT % 60;
let auSs = Math.round(auS);
if (auSs < 10) auSs = "0" + auSs;
let jd = (100*aud.currentTime)/aud.duration;
if(jd>0) {
jindu.innerHTML = "- " + auMs +":" + auSs;
jindu.style.backgroundSize = jd+ "% 8px";
}
}
jindu.onclick = function(){ //进度控制
let w = offset(jindu,"left");
let x = (event.clientX - w) * aud.duration / jindu.clientWidth;
aud.currentTime = x;
}
function offset(obj,direction){//获取元素总偏移量
let offsetDir = "offset" + direction.toUpperCase()+direction.substring(1);
let realNum = obj;
let positionParent = obj.offsetParent;
while(positionParent != null){
realNum += positionParent;
positionParent = positionParent.offsetParent;
}
return realNum;
}
btn.onclick = function(){ //光盘按钮点击事件
aud.paused ? (aud.play(), btn.style.animationPlayState="running") : (aud.pause(), btn.style.animationPlayState="paused");
}
mAdd.onclick = function(){ prom.style.display = "block"; } //呼出加歌界面
cancelMe.onclick = function(){ prom.style.display = "none"; } //放弃加歌
subMe.onclick = function(){ //加歌并播放新歌
let uri = document.getElementById('mUrl');
let name = document.getElementById('mName');
let tnum = muAr.length;
let str1 = uri.value.trim();
let str2 = name.value.trim();
if(str1 !="" && str2 != ""){
muAr = ;
mLi.innerHTML += (tnum+1) + ".<a id='list" + tnum + "' onclick='iPlay(" + tnum + ");howplay();' >" + str2 + "</a><br />";
iPlay(tnum);
howplay();
uri.value = "";
name.value = "";
}
prom.style.display = "none";
}
//自动播放(默认播放第一首)
iPlay(playIdx);
</script>
@马黑黑 队长做成了,很赞。还可以整体往左调一些{:4_187:} 红影 发表于 2022-2-16 19:02
队长做成了,很赞。还可以整体往左调一些
今天整了半天这个时候才调整出来,头都大了。{:4_189:} 最外层 div 要定位、左移 马黑黑 发表于 2022-2-16 19:26
最外层 div 要定位、左移
好的好的,谢! 加林森 发表于 2022-2-16 19:33
好的好的,谢!
就是 HTML 代码里的,这句:
<div style="margin:auto;position:relative;width:1000px;height:750px;background:#eee url('https://pic.imgdb.cn/item/620cc8ce2ab3f51d9139b0c4.jpg') no-repeat; border:2px solid tan;">
改为:
<div style="position:relative;left:-203px;width:1000px;height:750px;background:#eee url('https://pic.imgdb.cn/item/620cc8ce2ab3f51d9139b0c4.jpg') no-repeat; border:2px solid tan;">
为什么要改成酱紫?
你是从我的网站要的代码,我那里无需移动外层元素。论坛环境不一样,要左移,所以必须包含两个因素:① position定位为relative;② left为负值。
马黑黑 发表于 2022-2-16 19:44
就是 HTML 代码里的,这句:
谢谢老黑的指点!{:4_190:} 马黑黑 发表于 2022-2-16 19:44
就是 HTML 代码里的,这句:
现在对了。谢谢啊。{:4_183:} 红影 发表于 2022-2-16 19:02
队长做成了,很赞。还可以整体往左调一些
现在终于摆正了 加林森 发表于 2022-2-16 19:52
现在对了。谢谢啊。
{:4_199:} 加林森 发表于 2022-2-16 19:49
谢谢老黑的指点!
不客气。慢慢会得要领的 马黑黑 发表于 2022-2-16 21:00
不客气。慢慢会得要领的
嗯嗯,我还可以改变颜色的吧。 马黑黑 发表于 2022-2-16 21:00
谢谢你帮忙啊。 加林森 发表于 2022-2-16 21:02
谢谢你帮忙啊。
{:4_190:} 加林森 发表于 2022-2-16 21:01
嗯嗯,我还可以改变颜色的吧。
可以的 加林森 发表于 2022-2-16 19:05
今天整了半天这个时候才调整出来,头都大了。
找音乐也挺麻烦,那个127的很多音乐无效了。都不知道怎样去找能放得出来的。 红影 发表于 2022-2-16 21:06
找音乐也挺麻烦,那个127的很多音乐无效了。都不知道怎样去找能放得出来的。
就是,得先听听音乐是不是完整的,还要调整代码,真有点累人的。 马黑黑 发表于 2022-2-16 21:06
可以的
嗯嗯,我慢慢试了。 马黑黑 发表于 2022-2-16 21:05
干杯!{:4_191:}