版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第原生JS實(shí)現(xiàn)音樂播放器的示例代碼functionprevHandle(){//根據(jù)播放模式重新加載歌曲
constmodeIndex=modeControl.index;
constsongListLens=songList.length;
if(modeIndex==0){//順序播放
letindex=--control.index;
index==-1(index=songListLens-1):index;
control.index=index%songListLens;
}elseif(modeIndex==1){//隨機(jī)播放
constrandomNum=Math.random()*(songListLens-1);
control.index=Math.round(randomNum);
}elseif(modeIndex==2){//單曲
reload(songList);
functionnextHandle(){
constmodeIndex=modeControl.index;
constsongListLens=songList.length;
if(modeIndex==0){//順序播放
control.index=++control.index%songListLens;
}elseif(modeIndex==1){//隨機(jī)播放
constrandomNum=Math.random()*(songListLens-1);
control.index=Math.round(randomNum);
}elseif(modeIndex==2){//單曲
reload(songList);
播放進(jìn)度條控制
功能:實(shí)時(shí)更新播放進(jìn)度,點(diǎn)擊進(jìn)度條調(diào)整歌曲播放進(jìn)度
audio所用API:audiotimeupdate事件,audio.currentTime
//播放進(jìn)度實(shí)時(shí)更新
audioFile.file.addEventListener('timeupdate',lyricAndProgressMove);
//通過拖拽調(diào)整進(jìn)度
gressDot.addEventListener('click',adjustProgressByDrag);
//通過點(diǎn)擊調(diào)整進(jìn)度
gressWrap.addEventListener('click',adjustProgressByClick);
播放進(jìn)度實(shí)時(shí)更新:通過修改相應(yīng)DOM元素的位置或者寬度進(jìn)行修改
functionlyricAndProgressMove(){
constaudio=audioFile.file;
constcontrolIndex=control.index;
//歌曲信息初始化
constsongLyricItem=document.getElementsByClassName('song-lyric-item');
if(songLyricItem.length==0)return;
letcurrentTime=audioFile.currentTime=Math.round(audio.currentTime);
letduration=audioFile.duration=Math.round(audio.duration);
//進(jìn)度條移動(dòng)
constprogressWrapWidth=gressWrap.offsetWidth;
constcurrentBarPOS=currentTime/duration*100;
gressBar.style.width=`${currentBarPOS.toFixed(2)}%`;
constcurrentDotPOS=Math.round(currentTime/duration*progressWrapWidth);
gressDot.style.left=`${currentDotPOS}px`;
songInfo.currentTimeSpan.innerText=formatTime(currentTime);
拖拽調(diào)整進(jìn)度:通過拖拽移動(dòng)進(jìn)度條,并且同步更新歌曲播放進(jìn)度
functionadjustProgressByDrag(){
constfragBox=gressDot;
constprogressWrap=gressWrap
drag(fragBox,progressWrap)
functiondrag(fragBox,wrap){
constwrapWidth=wrap.offsetWidth;
constwrapLeft=getOffsetLeft(wrap);
functiondragMove(e){
letdisX=e.pageX-wrapLeft;
changeProgressBarPos(disX,wrapWidth)
fragBox.addEventListener('mousedown',()={//拖拽操作
//點(diǎn)擊放大方便操作
fragBox.style.width=`14px`;fragBox.style.height=`14px`;fragBox.style.top=`-7px`;
document.addEventListener('mousemove',dragMove);
document.addEventListener('mouseup',()={
document.removeEventListener('mousemove',dragMove);
fragBox.style.width=`10px`;fragBox.style.height=`10px`;fragBox.style.top=`-4px`;
functionchangeProgressBarPos(disX,wrapWidth){//進(jìn)度條狀態(tài)更新
constaudio=audioFile.file
constduration=audioFile.duration
letdotPos
letbarPos
if(disX0){
dotPos=-4
barPos=0
audio.currentTime=0
}elseif(disX0disXwrapWidth){
dotPos=disX
barPos=100*(disX/wrapWidth)
audio.currentTime=duration*(disX/wrapWidth)
}else{
dotPos=wrapWidth-4
barPos=100
audio.currentTime=duration
gressDot.style.left=`${dotPos}px`
gressBar.style.width=`${barPos}%`
點(diǎn)擊進(jìn)度條調(diào)整:通過點(diǎn)擊進(jìn)度條,并且同步更新歌曲播放進(jìn)度
functionadjustProgressByClick(e){
constwrap=gressWrap;
constwrapWidth=wrap.offsetWidth;
constwrapLeft=getOffsetLeft(wrap);
constdisX=e.pageX-wrapLeft;
changeProgressBarPos(disX,wrapWidth)
歌詞顯示及高亮
功能:根據(jù)播放進(jìn)度,實(shí)時(shí)更新歌詞顯示,并高亮當(dāng)前歌詞(通過添加樣式)
audio所用API:audiotimeupdate事件,audio.currentTime
//歌詞顯示實(shí)時(shí)更新
audioFile.file.addEventListener('timeupdate',lyricAndProgressMove);
functionlyricAndProgressMove(){
constaudio=audioFile.file;
constcontrolIndex=control.index;
constsongLyricItem=document.getElementsByClassName('song-lyric-item');
if(songLyricItem.length==0)return;
letcurrentTime=audioFile.currentTime=Math.round(audio.currentTime);
letduration=audioFile.duration=Math.round(audio.duration);
lettotalLyricRows=lyric.totalLyricRows=songLyricItem.length;
letLyricEle=lyric.ele=songLyricItem[0];
letrowsHeight=lyric.rowsHeight=LyricEleLyricEle.offsetHeight;
//歌詞移動(dòng)
lrcs[controlIndex].lyric.forEach((item,index)={
if(currentTime===item.time){
lyric.currentRows=index;
songLyricItem[index].classList.add('song-lyric-item-active');
index0songLyricItem[index-1].classList.remove('song-lyric-item-active');
if(index5indextotalLyricRows-5){
songInfo.lyricWrap.scrollTo(0,`${rowsHeight*(index-5)}`)
播放模式設(shè)置
功能:點(diǎn)擊跳轉(zhuǎn)播放模式,并修改相應(yīng)圖標(biāo)
audio所用API:無
//播放模式設(shè)置
control.mode.addEventListener('click',changePlayMode);
functionchangePlayMode(){
modeControl.index=++modeControl.index%3;
constmode=control.mode;
modeControl.index
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 多污染物聯(lián)合暴露下社區(qū)慢病的健康風(fēng)險(xiǎn)評價(jià)
- 2025年高職無人機(jī)應(yīng)用技術(shù)(無人機(jī)植保技術(shù))試題及答案
- 2025年大學(xué)化工類(化工基礎(chǔ)實(shí)訓(xùn))試題及答案
- 2026年電動(dòng)車維修(電池案例)試題及答案
- 2025年中職計(jì)算機(jī)應(yīng)用(數(shù)據(jù)處理)試題及答案
- 2025年高職(旅游管理綜合實(shí)訓(xùn))規(guī)劃優(yōu)化實(shí)操試題及答案
- 2025年中職第一學(xué)年(中醫(yī)養(yǎng)生保健)中醫(yī)食療養(yǎng)生試題及答案
- 2026年污水處理(工業(yè)廢水處理工藝)試題及答案
- 2025年中職建筑施工(混凝土澆筑)試題及答案
- 2025年中職第二學(xué)年(水產(chǎn)養(yǎng)殖實(shí)訓(xùn))魚塘水質(zhì)調(diào)節(jié)實(shí)操綜合測試試題及答案
- 魯科版高中化學(xué)必修一教案全冊
- 提高隧道初支平整度合格率
- 2023年版測量結(jié)果的計(jì)量溯源性要求
- 建筑能耗與碳排放研究報(bào)告
- GB 29415-2013耐火電纜槽盒
- 中國古代經(jīng)濟(jì)試題
- 軟件定義汽車:產(chǎn)業(yè)生態(tài)創(chuàng)新白皮書
- 磷石膏抹灰專項(xiàng)施工方案
- 水電水利工程施工質(zhì)量管理培訓(xùn)講義
- ArcMap制圖-地圖版面設(shè)計(jì)實(shí)驗(yàn)報(bào)告
- 建設(shè)部37號令危大工程標(biāo)識牌樣表
評論
0/150
提交評論