Vue自定义进度条组件

在这里插入图片描述

进度条组件
使用Vue自定义进度条 Progress 组件。基本实现思路:用两个嵌套的div,外层div给蓝色背景,内层div给黄色背景;动态地改变黄色div的宽度,动态地计算出色彩(宽度)占比。

1、核心代码如下

<body>
    <div id="app">
        <my-progreess :rate="rate"></my-progreess>
    </div>
     <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script>
        Vue.component('my-progreess', {
            props: {
                rate: {
                    type: Number,
                    required: false,
                    default: 0
                }
            },
            template: `<div>
                <div class="progress">
                    <div class="rate" :style='{"width":rate+"px"}'></div>
                </div>
                <div class='nums'>
                    <div><span v-text='Math.round(rate/2)'></span>%</div>
                    <div><span v-text='100-Math.round(rate/2)'></span>%</div>
                </div>            
            </div>`
        })
        const vm = new Vue({
            el: '#app',
            data: {
                rate: 0
            },
            mounted: function() {
                this.timer = setInterval(()=>{
                    // 动态改变黄色div的宽度
                    this.rate++
                    // 当然进度满格时,关闭定时器
                    if (this.rate >= 200) {
                        clearInterval(this.timer)
                    }
                }, 150)
            }
        })
    </script>
</body>

css样式如下:

 <style>
         #app {
            padding: 50px;
        }
        .progress {
            width: 200px;
            height: 30px;
            <style>
         #app {
            padding: 50px;
        }
        .progress {
            width: 200px;
            height: 30px;
            background-color: rgba(87, 80, 30, 1);
            border-radius: 15px;
            overflow: hidden;
            position: relative;
        }
        .rate {
            position: absolute;
            left: 0;
            top: 0;
            bottom: 0;
            background-color: rgb(240, 145, 37);
            width: 0;  
        }
        .nums {
            font-size: 16px;
            width: 200px;
            height: 30px;
            line-height: 30px;
            overflow: hidden;
            box-sizing: border-box;
            padding: 0 5px;
        }
        .nums div:first-child {
            float: left;
        }
        .nums div:last-child {
            float: right;
        }
    </style>

本篇结束

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值