CSS+Javascript实现的一个粗糙的圆形进度条,可以当小插件用

<html>
    <head>
        <meta charset="utf-8">
        <title>circle progress bar</title>
    </head>
    <body>
        <style type="text/css">
            .circle_container{width: 200px; height: 200px; border: 1px dotted #aaa; margin: 100px auto; position: relative;}
            .cover{position: absolute; top: 0; width: 100px; height: 200px; overflow: hidden ;}
            .left{left:0;}
            .right{right:0;}
            .circleprogress{width:180px; height: 180px; border: 10px solid transparent; border-radius: 50%; position: absolute; top:0; transition: all 1s;}
            .rightcircle{border-top: 10px solid green; border-right: 10px solid green; right: 0; transform: rotate(45deg);}
            .leftcircle{border-left: 10px solid green; border-top: 10px solid green; transform: rotate(45deg);}
            
        </style>
        <div id="progress1" class="circle_container">
            <div class="cover left">
                <div class="circleprogress leftcircle"></div>
            </div>
            <div class="cover right">
                <div class="circleprogress rightcircle"></div>
            </div>
        </div>
        <script type="text/javascript" language="javascript">
            function circleProgressbar(objElement,intNum){
                this._objElement = objElement;
                this._percentage = parseInt(intNum);
                if(isNaN(this._percentage)) this._percentage = 0;
                if(circleProgressbar.prototype._isinitial == undefined){
                    circleProgressbar.prototype._transdeg = 100 / 360;
                    circleProgressbar.prototype.get = function(){
                        return this._percentage;
                    }
                    circleProgressbar.prototype.set = function(intValue){
                        this._percentage = intValue;
                        this._percentage <= 0 ? 0 : this._percentage;
                        this._percentage >= 100 ? 100 : this._percentage;
                        //计算数值对应的角度
                        this._deg = intValue / circleProgressbar.prototype._transdeg;//能算出度数
                        if(this._percentage <= 50){
                            this._deg -= 135;//角度差
                            //左边隐藏
                            this._objElement.firstElementChild.firstElementChild.style.transform = "rotate(135deg)";
                            //旋转右边
                            this._objElement.lastElementChild.firstElementChild.style.transform = "rotate(" + this._deg + "deg)";
                        }else{
                            //旋转右边
                            this._objElement.lastElementChild.firstElementChild.style.transform = "rotate(45deg)";
                            if(this._percentage == 100){
                                this._objElement.firstElementChild.firstElementChild.style.transform = "rotate(" + 315 + "deg)";
                            }else{
                                this._deg -= 45;//角度差
                                this._objElement.firstElementChild.firstElementChild.style.transform = "rotate(" + this._deg + "deg)";
                                
                            }
                            
                        }
                        return this._deg;
                    }
                    this.set(this._percentage);//创建对象的时候,指示默认进度
                    circleProgressbar.prototype._isinitial = true;
                }
            }

            //创建滚动条对象,传入滚动条id和默认进度
            per = 0;
            var pg = new circleProgressbar(document.getElementById("progress1"),per);
            //展示用,每秒加10%
            setInterval(() => {
                pg.set(per);
                per = per+10;
                per = per > 100 ? 0 : per;
            }, 1000);
        </script>
    </body>
</html>

新建文件,复制粘贴就能用,有点糙,后续完善。也是边学边实践吧,有点不伦不类,插件不插件的,真要做成插件要考虑的就要比现在多太多了,原生不原生的,写的时候只想着够我自己用的了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值