上一期我们介绍了外观设计,这把我们聊聊让进度条跟随播放进度走
目录
预期效果
准备
- 在此之前,你需要有以下基础:
- HTML/CSS/JS
- 了解一些<audio>标签的属性和事件
- Vue(数据绑定和事件绑定)
实现
- 还是上次的那个Html/css,但我们这把加入了:
- 最外围用div包裹,用于创建vue
- 添加了<audio>
- 显示音乐标签面板 (controls)
- 默认静音 (muted)
- 绑定事件:播放时长更新事件 timeupdate
- 绑定数据:音频文件资源链接 src
- 两个vue数据绑定
- cmusic_control_loadProgress_style (加载进度条长度)
- cmusic_control_progress_style (播放进度条长度)
- 修改部分css样式
- HTML:
<div id="root_div" class="cmusic_displayFlex_class" style="height:150px;justify-content:space-between;align-items: center;">
<!-- 音频 -->
<audio id="cmusic_audio" ref="cmusic_audio" controls muted
@timeupdate="audio_lengthChange()" :src="url_audio">
</audio>
<span id="cmusic_control_span_progressBar">
<div class="cmusic_displayFlex_class cmusic_control_progress_div" style="flex-direction: row;">
<span id="cmusic_control_span_loadProgress" :style="cmusic_control_loadProgress_style"></span>
<span id="cmusic_control_span_progress" :style="cmusic_control_progress_style"></span>
</div>
</span>
<div>
- CSS:
<style>
/*外框*/
#cmusic_control_span_progressBar{
border-radius: 50px;
height: 20px;
width: 100%;
display: inline-flex;
align-items: center;
position: relative;
background: transparent;
box-shadow: inset 2px 2px 4px #bcc5d6,
inset -2px -2px 5px #feffff;
border: 2px solid #d0f4ff;
justify-content: center;
transition: all 0.8s ease;
}
/*布局类*/
.cmusic_displayFlex_class {
width: 100%;
display: flex;
flex-direction: column;
margin-left: auto;
margin-right: auto;
}
.cmusic_control_progress_div {
position:absolute;
width:94% !important;
height:100%;
align-items: center;
}
/*进度条*/
#cmusic_control_span_progress,
#cmusic_control_span_loadProgress{
background: linear-gradient(90deg, #c4f4fe, #66ccff);
border-radius: 50px;
position: absolute;
width:100%;
height: 60%;
pointer-events: none;
transition: width 0.5s ease;
box-shadow: 2px 2px 10px #ccd3ff,
-2px -2px 10px #ccd3ff;
}
/*加载进度条*/
#cmusic_control_span_loadProgress {
background: linear-gradient(90deg, #abecd6, #fff9d2);
}
</style>
- 并在script标签内写js/vue代码:
<!-- 引入vue -->
<script src="https://unpkg.com/vue@3.2.33"></script>
<script>
const { createApp } = Vue
var cmusic_json = {
//数据
data() {
return {
url_audio:"https://blog.coolight.cool/wp-content/uploads/2022/05/大喜_洛天依.m4a",
cmusic_control_loadProgress_style:"width:80%",
cmusic_control_progress_style:"width:50%"
}
},
//函数
methods:{
//监听播放时长,修改进度条长度
audio_lengthChange:function(){
let caudio = this.$refs.cmusic_audio;
//duration是音频总时长
//如果音频元数据加载完成,才能获得其总时长,后续才能进行计算
if (!isNaN(caudio.duration)){
this.cmusic_control_progress_style = "width:" + (parseInt(caudio.currentTime / caudio.duration * 100)).toString() + "%;";
}else{
this.cmusic_control_progress_style = "width:0%;";
}
}
}
};
var cmusic_app = Vue.createApp(cmusic_json);
var cmusic_vue = cmusic_app.mount("#root_div");
</script>
- 最终效果:
- 默认是静音的,可以点击播放试试
viagra canada: Sildenafil Citrate Tablets 100mg – Cheap Viagra 100mg
https://kamagra.win/# Kamagra 100mg
Levitra 20 mg for sale: Buy Vardenafil 20mg – Vardenafil price
cenforce.pro: cheapest cenforce – cheapest cenforce
Levitra 10 mg buy online Buy generic Levitra online Cheap Levitra online
Kamagra 100mg price buy kamagra online sildenafil oral jelly 100mg kamagra
http://kamagra.win/# sildenafil oral jelly 100mg kamagra
Generic Tadalafil 20mg price buy cialis overseas Generic Cialis price
Vardenafil price: Buy Vardenafil 20mg – Levitra 20 mg for sale
https://cenforce.pro/# Cenforce 100mg tablets for sale
http://kamagra.win/# buy kamagra online usa
http://kamagra.win/# super kamagra
cheap kamagra: kamagra oral jelly – sildenafil oral jelly 100mg kamagra
buy Viagra online: viagras.online – Generic Viagra for sale
http://levitrav.store/# Levitra tablet price
cheapest cenforce Cenforce 150 mg online cheapest cenforce
Kamagra 100mg price: kamagra pills – Kamagra 100mg
Purchase Cenforce Online cenforce.pro Purchase Cenforce Online
order cenforce cenforce for sale Cenforce 150 mg online
buy viagra here: Buy generic 100mg Viagra online – viagra canada
https://viagras.online/# viagra canada
https://levitrav.store/# Levitra 10 mg buy online
http://levitrav.store/# Buy generic Levitra online
Buy Cenforce 100mg Online: order cenforce – cenforce.pro
https://kamagra.win/# Kamagra tablets
Buy Cialis online buy cialis overseas Cialis 20mg price in USA
http://cialist.pro/# Cialis without a doctor prescription
Vardenafil online prescription buy Levitra over the counter Buy Vardenafil 20mg
Vardenafil online prescription: levitrav.store – Vardenafil buy online
sildenafil oral jelly 100mg kamagra: kamagra pills – super kamagra
viagra canada Cheapest place to buy Viagra Cheapest Sildenafil online
п»їkamagra: buy kamagra online – п»їkamagra
Cheap Sildenafil 100mg: viagras.online – cheapest viagra
https://cenforce.pro/# Buy Cenforce 100mg Online
buy kamagra online usa kamagra pills Kamagra 100mg price
Buy Tadalafil 10mg Cialis over the counter Tadalafil Tablet
buy Viagra online: Buy Viagra online – Order Viagra 50 mg online
http://viagras.online/# over the counter sildenafil
http://levitrav.store/# Buy Vardenafil 20mg
Buy Cenforce 100mg Online: Cenforce 150 mg online – cenforce for sale
http://levitrav.store/# Generic Levitra 20mg
order cenforce Cenforce 150 mg online buy cenforce
buy Viagra online: Buy generic 100mg Viagra online – Order Viagra 50 mg online
Buy Cenforce 100mg Online cenforce.pro order cenforce
buy kamagra online usa: kamagra – Kamagra Oral Jelly
http://kamagra.win/# buy kamagra online usa
https://levitrav.store/# Levitra tablet price
https://viagras.online/# cheapest viagra
https://viagras.online/# sildenafil 50 mg price
sildenafil 50 mg price: viagras.online – Generic Viagra online