前言

一开始看到UI设计稿,我内心是十分抗拒的。觉得用原生audio的样式就可以了,也不是特别丑,毕竟时间给的不多,自定义样式还要改逻辑啥的。在网上搜索了一番有没有合适的插件,没有看到心动的。最后还是硬着头皮自己写了。

参考了一个博客,很感谢这位博主,逻辑都可以用。不过原博用的jquery,我自己用vue,而且我的页面可能有不止一个audio,设计稿和原博也很不一样,所以改代码还是花了一番心思。

我这里是按照我的设计稿做出来的样子,如果你的设计稿跟我的不一样,那也只需要改一下css部分和html,js还是能直接套用的。

如果急用的同学可以直接跳到最后看完整的代码,复制了不出意外可以直接用的,只需要把播放按钮的图片改成你自己的图片,再添加可用的audio链接就可以了。

html代码:

@timeupdate.stop="updateProgress(index)" @ended.stop="audioEnded(index)">

{{ item.audio_name }}

{{ audioArr[index].currentTime }}

{{ audioArr[index].duration }}

js代码:export default {

data() {

return {

audioNodes: [], // 用于存放所有 audio 的 DOM 节点

audioArr: [], // 用于维护 audio 的总时长和当前播放时间的数组

content: [ // 存放audio的地址信息,一般是接口返回

{"audio_name":"20210112_1056.m4a","audio_url":"https://xxx.com/android1610420451567524.m4a"},

{"audio_name":"20210112_1056.m4a","audio_url":"https://xxx.com/android1610420451567524.m4a"},

]

}

},

mounted() {

this.audioNodes = document.getElementsByClassName('list-box')

this.content.forEach((item, index) => {

this.$set(this.audioArr, index, { duration: '', currentTime: '00:00' })

})

},

methods: {

// 切换播放,暂停按钮的事件

playAudio(index) {

let audio = this.audioNodes[index].firstChild

if (audio.paused) {

audio.play()

} else {

audio.pause()

}

},

// 获取音频的总时长

fillTime(event, index) {

this.audioArr[index].duration = this.transTime(event.target.duration)

},

// 将秒数转化成(分:秒)格式

transTime(time) {

let duration = parseInt(time)

let minute = parseInt(duration / 60).toString().padStart(2, '0')

let sec = (duration % 60).toString().padStart(2, '0')

return `${minute}:${sec}`

},

// 进度条播放的事件

updateProgress(index) {

let audio = this.audioNodes[index].firstChild

let value = Math.round((Math.floor(audio.currentTime) / Math.floor(audio.duration)) * 100, 0)

this.audioArr[index].currentTime = this.transTime(audio.currentTime)

let progressTag = this.audioNodes[index].getElementsByClassName('pgs-play')[0]

progressTag.style.left = `${value}%`

},

// 播放结束的处理动作

audioEnded(index) {

let audio = this.audioNodes[index].firstChild

audio.currentTime = 0

audio.pause()

},

}

}

css代码:.pgs {

background-color: #D8D4D1;

text-align: center;

position: relative;

height: 2px;

margin-bottom: 10px;

}

.pgs-play {

position: absolute;

top: -2.5px;

left: 0;

width: 7px;

height: 7px;

border-radius: 50%;

background-color: #B03F28;

z-index: 1;

}

.play-icon {

width: 45px;

height: 45px;

margin-right: 15px;

}

audio {

display: block;

height: 0;

}

.audio-box {

background: #F5F5F5;

border: 1px solid #D8D4D1;

border-radius: 5px;

width: 100%;

padding: 15px 12px;

box-sizing: border-box;

}

.audio-name {

font-size: 15px;

color: #252120;

margin-bottom: 15px;

overflow:hidden; //超出的文本隐藏

text-overflow:ellipsis; //溢出用省略号显示

white-space:nowrap; //溢出不换行

}

.control-row {

display: flex;

align-items: flex-end;

}

.row-right {

flex-grow: 100;

}

.time-row {

position: relative;

color: #B0AFAD;

font-size: 12px;

margin-bottom: 3px;

}

.audio-time {

position: absolute;

right: 0;

}

完整代码(audio.vue)可以直接套用,只需要改control-row里面的图片地址,这是播放按钮的图片。

.page-container {

.pgs {

background-color: #D8D4D1;

text-align: center;

position: relative;

height: 2px;

margin-bottom: 10px;

}

.pgs-play {

position: absolute;

top: -2.5px;

left: 0;

width: 7px;

height: 7px;

border-radius: 50%;

background-color: #B03F28;

z-index: 1;

}

.play-icon {

width: 45px;

height: 45px;

margin-right: 15px;

}

audio {

display: block;

height: 0;

}

.audio-box {

background: #F5F5F5;

border: 1px solid #D8D4D1;

border-radius: 5px;

width: 100%;

padding: 15px 12px;

box-sizing: border-box;

}

.audio-name {

font-size: 15px;

color: #252120;

margin-bottom: 15px;

overflow: hidden; //超出的文本隐藏

text-overflow: ellipsis; //溢出用省略号显示

white-space: nowrap; //溢出不换行

}

.control-row {

display: flex;

align-items: flex-end;

}

.row-right {

flex-grow: 100;

}

.time-row {

position: relative;

color: #B0AFAD;

font-size: 12px;

margin-bottom: 3px;

}

.audio-time {

position: absolute;

right: 0;

}

}

@timeupdate.stop="updateProgress(index)" @ended.stop="audioEnded(index)">

{{ item.audio_name }}

{{ audioArr[index].currentTime }}

{{ audioArr[index].duration }}

export default {

data() {

return {

audioNodes: [], // 用于存放所有 audio 的 DOM 节点

audioArr: [], // 用于维护 audio 的总时长和当前播放时间的数组

content: [ // 存放audio的地址信息,一般是接口返回

{"audio_name":"20210112_1056.m4a","audio_url":"https://xxx.com/android1610420451567524.m4a"},

{"audio_name":"20210112_1056.m4a","audio_url":"https://xxx.com/android1610420451567524.m4a"},

]

}

},

mounted() {

this.audioNodes = document.getElementsByClassName('list-box')

this.content.forEach((item, index) => {

this.$set(this.audioArr, index, { duration: '', currentTime: '00:00' })

})

},

methods: {

// 切换播放,暂停按钮的事件

playAudio(index) {

let audio = this.audioNodes[index].firstChild

if (audio.paused) {

audio.play()

} else {

audio.pause()

}

},

// 获取音频的总时长

fillTime(event, index) {

this.audioArr[index].duration = this.transTime(event.target.duration)

},

// 将秒数转化成(分:秒)格式

transTime(time) {

let duration = parseInt(time)

let minute = parseInt(duration / 60).toString().padStart(2, '0')

let sec = (duration % 60).toString().padStart(2, '0')

return `${minute}:${sec}`

},

// 进度条播放的事件

updateProgress(index) {

let audio = this.audioNodes[index].firstChild

let value = Math.round((Math.floor(audio.currentTime) / Math.floor(audio.duration)) * 100, 0)

this.audioArr[index].currentTime = this.transTime(audio.currentTime)

let progressTag = this.audioNodes[index].getElementsByClassName('pgs-play')[0]

progressTag.style.left = `${value}%`

},

// 播放结束的处理动作

audioEnded(index) {

let audio = this.audioNodes[index].firstChild

audio.currentTime = 0

audio.pause()

},

}

}

文章到这里就结束了,如果对你有帮助,欢迎点赞,收藏,谢谢~

html控制多个音频audio css,vue中audio自定义样式(页面中包含多个audio)相关推荐

  1. vue中组件在不同页面中渲染出错

    vue中组件在不同页面中渲染出错 封装了一个组件 封装了一个组件 遇到问题了,特向各位大佬请教 比如我封装了一个 child 组件 我在father 页面中引入 可以正常使用 而且css 样式不会出现 ...

  2. 从Chrome中的css自定义样式按钮中删除蓝色边框

    本文翻译自:Remove blue border from css custom-styled button in Chrome I'm working on a web page, and I wa ...

  3. html消除按钮边框,在Chrome中从css自定义样式按钮中删除蓝色边框

    在Chrome中从css自定义样式按钮中删除蓝色边框 我正在做一个网页,我想要定制样式标签.因此,对于CSS,我说:border: none..现在它在Safari中运行得很好,但是在Chrome中, ...

  4. audio 上一首 下一首 自定义样式_HTML5中 audio标签的样式修改

    2017年8月23日,台风"天鸽"的到来,带来了些风雨,也给平淡的上班生活带来了些乐趣. 今天做的一个小需求,就是要在网页上添加音频,html5属性 可以实现,用这个标签可以实现音 ...

  5. 配置 postCSS 自动添加 css 的兼容前缀||打包样式表中的图片和字体文件||打包处理 js 文件中的高级语法

    配置 postCSS 自动添加 css 的兼容前缀 ① 运行 npm i postcss-loader autoprefixer -D 命令 ② 在项目根目录中创建 postcss 的配置文件 pos ...

  6. vue项目html引入css,vue项目引入自定义.css的样式文件

    ES6的引入方式: .vue文件中 css文件引入 @import "../assets/common/common.css";//自定义.css的样式路径 js文件的引入 在ma ...

  7. vue 方法里面修改样式_vue中修改swiper样式

    问题 vue单文件组件中无法修改swiper样式. 解决 1,单文件组件中:新增一个style 不加scoped 让它最终成为全局样式.只在其中操作swiper的样式. .swiper-contain ...

  8. html中的面板样式,Dreamweaver中CSS样式面板

    核心提示:本教程为大家介绍一下Dreamweaver中CSS样式面板,希望对大家有帮助. 本教程为大家介绍一下Dreamweaver中CSS样式面板,希望对大家有帮助. 一.打开CSS样式面板 使用& ...

  9. 《风尚坐火箭学习vue》-- 第二章:页面中输出hello Vue

    前言:前端框架千千万,独有vue占一半 我是风尚,让我们一起坐火箭去学习Vue 图片来自vue官网 第二章:hello Vue 上章回顾:风尚内心答应了跟白发老头学习vue 风尚内心答应了跟白发老头学 ...

最新文章

  1. 相机设置感兴趣区域(自带API)
  2. python requests 发送 上传 多个文件
  3. android 怎么初始化下拉框_第30讲:“二师兄”的成长历程之二,类属性的初始化...
  4. 查看/修改Linux时区和时间
  5. python 查询包_查找Python包的依赖包(语句)
  6. android播放html5视频,仅仅有声音没有图像视频
  7. kafka专题:kafka单机和集群安装详情,Spring Boot如何整合Kafka
  8. MySQLBackup 8.0.26 备份与恢复
  9. [20150304]唯一索引与阻塞.txt
  10. 运行python脚本时出现no module named cv2怎么解决
  11. Gradle之全局配置
  12. JavaScript 函数定义和调用
  13. 部署http+svn,yum安装svn 1.9版本
  14. 2023年厦门大学全日制会计专硕(MPAcc)考研上岸前辈备考经验
  15. 空间自相关—莫兰指数
  16. 【GlobalMapper精品教程】003:影像裁剪、批量影像分幅案例详解
  17. 雷电模拟器+proxifier
  18. 只因一段代码全公司200多人被捕,爬虫敲响警钟!
  19. C语言实现钢琴块小游戏(低仿拉胯版)
  20. Hazelcast Jet DAG原理

热门文章

  1. windows10下使用nginx -s reload重启nginx失败
  2. SQL单行注释和多行注释
  3. 中国号码注册谷歌账号_亲测有效
  4. 玩转 Springboot 2 | 不使用 parent 方式创建SpringBoot项目篇
  5. 新传要不要学计算机,大学新生入学要带电脑吗?学长给出建议,不知道的会很“吃亏”...
  6. iphone 禁止系统屏保锁屏
  7. 芝麻信用 在阿里金融云上向P2P行业开放
  8. Linux系统时间修改方法
  9. MATLAB 函数句柄
  10. java p2p文件传输_P2P文件传输软件的设计与实现