基于jquery实现自定义的audio
预期效果

预期实现功能
- 自定义实现进度条及进度监控
- 可以手动定位进度
- 播放(每次只能播放一个,不允许多个同时播放)
- 暂停
实现方案
1. 进度条
进度条使用<input type="range" />
根据需求,可使用的属性如下:
<input
onchange="playTime(${element.id}, this)"
id="progress_${element.id}"
value="0"
type="range"
min="0"
max="100"
step="0"
/>
但是原生的样式不符合要求。但是调整样式又需要兼容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. 播放进度监控
通过audio的timeupdate事件监听播放进度,并通过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;
})
})
监听事件图省事挨个添加的。理论上应该搞一个事件委托,在父级进行监听。
以上就是基于jquery实现自定义的audio的详细内容,更多关于jquery自定义的audio的资料请关注脚本之家其它相关文章!
相关文章
jQuery的Ajax用户认证和注册技术实例教程(附demo源码)
这篇文章主要介绍了jQuery的Ajax用户认证和注册技术,结合完整实例较为详细的分析讲解了jQuery中ajax方法实现用户验证与注册的相关技巧与注意事项,并附带了demo源码供读者下载,需要的朋友可以参考下2015-12-12
jQuery中attr()与prop()函数用法实例详解(附用法区别)
这篇文章主要介绍了jQuery中attr()与prop()函数用法,结合实例形式详细分析了attr()与prop()函数的使用技巧与相关注意事项,并附带了attr()与prop()函数用法的区别,需要的朋友可以参考下2015-12-12
jQuery Selectors(选择器)的使用(四-五、内容篇&可见性篇)
本系列文章主要讲述jQuery框架的选择器(Selectors)使用方法,我将以实例方式进行讲述,以简单,全面为基础,不会涉及很深,我的学习方法:先入门,后进阶!2009-12-12
基于Jquery的仿Windows Aero弹出窗(漂亮的关闭按钮)
目前市面上已经有很多成熟好用的jquery弹出窗插件,像模态窗口插件(Modal Dialog Plugins)以及数不胜数的灯箱插件(lightbox plugins)。2010-09-09
jQuery插件FusionCharts实现的Marimekko图效果示例【附demo源码】
这篇文章主要介绍了jQuery插件FusionCharts实现的Marimekko图效果,结合实例形式分析了jQuery使用FusionCharts插件结合xml数据绘制Marimekko图的相关操作技巧,并附带demo源码供读者下载参考,需要的朋友可以参考下2017-03-03
利用jQuery实现CheckBox全选/全不选/反选的简单代码
下面小编就为大家带来一篇利用jQuery实现CheckBox全选/全不选/反选的简单代码。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-05-05


最新评论