js进度条—模拟加载—画板

**

js通过canvas来画图模拟进度条加载动画

**
html:

<div class="wai">
    <div>模拟加载—>加载进度:<span id="num"></span></div>
    <span id="dtnum"></span>
    <canvas width="200" height="22" style="border-radius: 10px;" id="can">您的浏览器不支持canvas</canvas>
    <br>
    <button id="btnLogin">开始</button>
    <button id="btnZt">暂停</button>
</div>

css:

.wai{
	 height: 80px;
	 width: 100%;
	 position: relative;
}
#dtnum{
	 position: absolute;
	 top: 23px;
	 z-index: 99999;
	 color: white;
	 font-size: 12px;
	 display: inline-block;
	 width: 40px;
}
#btnLogin{
	 width: 100px;
	 height: 30px;
	 font-size: 14px;
	 color: white;
	 background: #1568ff;
	 border: none;
	 border-radius: 15px;
	 cursor: pointer;
	 outline: none;
}
#btnZt{
	 width: 100px;
	 height: 30px;
	 font-size: 14px;
	 color: white;
	 background: #1568ff;
	 border: none;
	 border-radius: 15px;
	 cursor: pointer;
	 outline: none;
}

js:

(function(){
        let canvas;
        let ctx;
        let num = document.getElementById('num');
        let dtnum = document.getElementById('dtnum');
        let btnLogin = document.getElementById('btnLogin');
        let btnZt = document.getElementById('btnZt');
        let setInt;
        let k;
        let bfb;
        let col;

        // 建立画布
        canvas = document.getElementById('can');
        ctx = canvas.getContext('2d');
        //设置画布样式
        canvas.width = 1024;
        canvas.height = 20;
        canvas.style.border = '1px solid #009688';
        canvas.style.borderRadius = '10px';
        // 初始值
        k = 24;
        bfb = 0;
        col ='#009688';

        // 开始
        btnLogin.addEventListener('click',function () {
            setInt = setInterval(function (a,b) {
                jsq();
                arc();
            },1000);
        });
        // 暂停
        btnZt.addEventListener('click',function () {
            clearInterval(setInt);//清除计时器
        });
        //计时器运行函数
        let jsq = function(){
            let x = Math.ceil(Math.random()*40);//0-40之间的随机整数
            k = k+x;
            // console.log(k);
            // 显示文字、样式
            if(k>=1024){
                clearInterval(setInt);
                k = 1024;
                col = '#ff0000';
                bfb = (k-24)/10;
                dtnum.style.left = k-24 + 'px';
                num.innerText = bfb + '%';
                dtnum.innerText = bfb + '%';
                canvas.style.border = '1px solid #ff0000';
            }else {
                bfb = (k-24)/10;
                dtnum.style.left = k-24 + 'px';
                num.innerText = bfb + '%';
                dtnum.innerText = bfb + '%';
            }
        };
        //绘制进度条
        let arc = function () {
            ctx.strokeStyle = col;
            ctx.lineWidth = 5;//画笔大小
            ctx.beginPath();//开始作画
            ctx.moveTo(0,0);//起始点
            ctx.lineTo(0,20);
            ctx.lineTo(k,20);
            ctx.arc(k,10,10,Math.PI/2,-Math.PI/2,true);//画圆——默认逆时针false ,顺时针为true
            ctx.lineTo(k,0);
            ctx.lineTo(0,0);
            ctx.closePath();//结束
            ctx.stroke();//描边
            ctx.fillStyle = col;
            ctx.fill();
        };
    })();

效果演示:
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DevilAngelia

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值