[vue3]audio音乐进度条(二)跟随播放进度

/ 3,530评论 / 16206阅读 / 2点赞

上一期我们介绍了外观设计,这把我们聊聊让进度条跟随播放进度走

预期效果


准备


实现

<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>
<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>
<!-- 引入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>
  1. helpful site说道:

    Oh my goodness! Incredible article dude! Thank you, However I am encountering difficulties with your RSS. I don’t know why I can’t subscribe to it. Is there anybody getting identical RSS problems? Anyone that knows the solution can you kindly respond? Thanx!

  2. 789b1 com说道:

    There’s definately a great deal to find out about this issue. I really like all the points you made.

  3. 789bet home说道:

    That is a very good tip particularly to those new to the blogosphere. Brief but very accurate information… Many thanks for sharing this one. A must read post.

  4. YTMP3说道:

    After I initially commented I seem to have clicked the -Notify me when new comments are added- checkbox and from now on every time a comment is added I get four emails with the exact same comment. Perhaps there is an easy method you can remove me from that service? Thank you.

  5. Nice post. I learn something totally new and challenging on websites I stumbleupon every day. It will always be useful to read through content from other writers and use a little something from other sites.

  6. I used to be able to find good info from your blog posts.

  7. tubidy mp3说道:

    Excellent post. I’m dealing with a few of these issues as well..

  8. 78win说道:

    I really like reading through an article that will make men and women think. Also, thanks for allowing for me to comment.

  9. 78win说道:

    I needed to thank you for this excellent read!! I definitely loved every little bit of it. I’ve got you bookmarked to check out new stuff you post…

  10. Syble Figgs说道:

    Greetings! Very helpful advice in this particular article! It is the little changes that will make the most significant changes. Many thanks for sharing!

  11. Catherin Toppa说道:

    Very good info. Lucky me I found your website by accident (stumbleupon). I’ve saved as a favorite for later!

  12. Jamesjer说道:

    Paxlovid buy online: paxlovid pharmacy – paxlovid india

  13. Jamesjer说道:

    doxycycline 500mg price: doxycycline capsules 40 mg – doxycycline online uk

  14. Jamesjer说道:

    doxycycline tablet: doxycycline 100mg for sale – doxycycline 100mg online

  15. Jamesjer说道:

    cipro: buy cipro online – ciprofloxacin 500mg buy online

  16. Leanne Varaza说道:

    You need to be a part of a contest for one of the finest sites online. I’m going to highly recommend this website!

  17. Arlen Horkey说道:

    Hi, I do believe this is an excellent blog. I stumbledupon it 😉 I’m going to come back once again since i have book marked it. Money and freedom is the greatest way to change, may you be rich and continue to help other people.

  18. Jamesjer说道:

    cipro ciprofloxacin: ciprofloxacin – buy cipro

  19. Jamesjer说道:

    п»їpaxlovid: Paxlovid over the counter – paxlovid generic

  20. Jamesjer说道:

    buying cheap clomid no prescription: where can i get cheap clomid – order clomid

  21. description说道:

    Oh my goodness! Awesome article dude! Many thanks, However I am going through difficulties with your RSS. I don’t know why I can’t subscribe to it. Is there anybody getting similar RSS problems? Anyone that knows the solution can you kindly respond? Thanks.

  22. Jamesjer说道:

    paxlovid for sale: paxlovid cost without insurance – paxlovid covid

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注