基于jquery實現(xiàn)自定義的audio
預期效果

預期實現(xiàn)功能
- 自定義實現(xiàn)進度條及進度監(jiān)控
- 可以手動定位進度
- 播放(每次只能播放一個,不允許多個同時播放)
- 暫停
實現(xiàn)方案
1. 進度條
進度條使用<input type="range" />
根據(jù)需求,可使用的屬性如下:
<input
onchange="playTime(${element.id}, this)"
id="progress_${element.id}"
value="0"
type="range"
min="0"
max="100"
step="0"
/>
但是原生的樣式不符合要求。但是調(diào)整樣式又需要兼容chrome和firefox。

最終的樣式代碼如下:
[type="range"] {
-webkit-appearance: none;
appearance: none;
margin: 0;
outline: 0;
background-color: transparent;
width: 22.125rem;
}
[type="range"]::-webkit-slider-runnable-track {
height: 4px;
background: #eee;
}
[type="range"]::-moz-range-track {
height: 4px;
background: #eee;
}
[type="range" i]::-webkit-slider-container {
height: 10px;
overflow: hidden;
}
[type="range" i]::-moz-range-progress {
height: 10px;
overflow: hidden;
}
input[type=range]::-moz-range-thumb {
-webkit-appearance: none;
appearance: none;
width: 2px;
height: 2px;
border-radius: 50%;
background-color: #B32B2B;
border: 1px solid transparent;
margin-top: -1px;
}
input[type="range"]::-moz-range-progress {
background-color: #B32B2B;
height: 4px;
}
[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 4px;
height: 4px;
border-radius: 50%;
background-color: #B32B2B;
border: 1px solid transparent;
border-image: linear-gradient(#B32B2B,#B32B2B) 0 fill / 0 2 0 0 / 0px 0px 0 2000px;
}
2.播放
在多個播放器同時存在時,我們需要做到同一個頁面同時播放的只有一個
首先渲染頁面
function musicList(id, index) {
let str = "";
musics?.slice(index, index + 10)?.forEach(element => {
str += `<div>
<div class="swiper-img-item">
<img src="./images/top.png" />
<div>${element.title}</div>
<audio class="topAudio" id="top_audio_${element.id}" src=${element.url} />
<img onclick="topPlay(${element.id})" id="top_play_${element.id}" class="second" src="./images/top_play.svg" />
<img onclick="topPause(${element.id})" style="display: none;" id="top_pause_${element.id}" class="second" src="./images/top_off.svg" />
</div>
</div>`
})
$(`#${id}`).find(".wrapper").append(str);
}
其次增加方法
function play(id) {
$("audio").each(function() {
var itemPlayId = $(this).attr("id")?.replace("audio", "play");
var itemPauseId = $(this).attr("id")?.replace("audio", "pause");
if ($(this).attr("id") != ("top_audio_"+id)) {
$(this).get(0).pause();
$("#"+itemPauseId).hide();
$("#"+itemPlayId).show();
} else {
$(this).get(0).play();
$("#"+itemPauseId).show();
$("#"+itemPlayId).hide();
}
})
}
3. 播放進度監(jiān)控
通過audio的timeupdate事件監(jiān)聽播放進度,并通過duration獲取總的長度。currentTime獲取當前長度。
$(".musicAudio").each(function() {
var that = $(this);
var audio = that.get(0);
audio.addEventListener("timeupdate", event => {
var id = that.attr("id").split("_").reverse()[0];
var curTime = dayjs(audio.currentTime*1000).format("mm:ss");
var duration = dayjs(audio.duration*1000).format("mm:ss");
$(`#time_${id}`).html(`${curTime}/${duration}`)
$(`#progress_${id}`).get(0).value = (audio.currentTime*1000/(audio.duration*1000))*100;
})
})
監(jiān)聽事件圖省事挨個添加的。理論上應(yīng)該搞一個事件委托,在父級進行監(jiān)聽。
以上就是基于jquery實現(xiàn)自定義的audio的詳細內(nèi)容,更多關(guān)于jquery自定義的audio的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
jquery下為Event handler傳遞動態(tài)參數(shù)的代碼
在jquery cook book里看到一篇給event handler傳遞動態(tài)參數(shù)的文章 感覺挺實用的 跟大家分享下2011-01-01
jQuery中與toggleClass等價的程序段 以及未來學習的方向
昨天開始學jQuery,js是我前端設(shè)計技術(shù)的一塊心病,一直沒有找到很好的學習辦法。最近突然開悟,可以學jQuery呀,這個東西比較好學。2010-03-03

