js实现进度条

效果图

在这里插入图片描述

实现代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
		<style type="text/css">
			#progressComp {
				display: none;
				height: 26px;
				text-align: left;
				margin: 16px;
				width: 80%;
			}
			#progressComp .inlineBlock {
				display: inline-block;
				vertical-align: middle;
			}
			#progressComp #progressContainer {
				width: 80%;
				border: 1px solid #0159e3;
				border-radius: 10px;
				height: 10px;
			}
			#progressComp #progressShow{
				background-color: #0159E3;
				border-top-left-radius: 10px;
				border-bottom-left-radius: 10px;
				height: 100%;
				margin-top: -15px;
			}
		</style>
	</head>
	<body>
		<div id="">
			<button type="button" id="btn">显示进度条</button>
		</div>
		<div id="progressComp" class="inlineBlock">
			<div id="progressText" class="inlineBlock">
				<label id="progressTitle" class="inlineBlock">操作名称:</label>
				<div id="valContainer" class="inlineBlock">
					<span class="progressVal">0</span>
					<span class="progressUnit">%</span>
				</div>
			</div>
			<div id="progressContainer" class="inlineBlock">
				<div id="progressShow" class="inlineBlock"></div>
			</div>
		</div>
		<script type="text/javascript">
			class Progress {
				static timer; // 定时器
				static currentProgress; // 当前进度
				static closeFn; // 关闭之后需要执行的函数
				/** 显示进度条
				 * @param {Object} title 操作名称
				 * @param {Object} progress 显示进度
				 * @param {Object} closeFn 关闭后需要执行的函数
				 */
				static show(title,progress,closeFn = () => {}){
					const $progressComp = $('#progressComp'),
					$progressTitle = $('#progressTitle'),
					$progressShow = $('#progressShow'),
					$progressVal = $('#valContainer .progressVal');
					if(progress === 0){
						// 初始化显示进度条
						if($progressComp.css('display') !== 'none'){
							$progressComp.css('display','none')
						}
						$progressTitle.html(title);
						$progressVal.html(0);
						$progressShow.css('width',0);
						$progressComp.css('display','inline-block');
						
						// 记录closeFn
						Progress.closeFn = closeFn;
						// 监听ESC
						document.addEventListener('keydown',(e) => {
							if(e.keyCode === 27){
								console.log('esc....');
								if($progressComp.css('display') !== 'none'){
									// 执行中断机制
									Progress.closeFn();
								}
							}
						})
					}else{
						// 更新进度条
						Progress.timer && clearInterval(Progress.timer);
						let nowVal = Number($progressVal.html());
						Progress.currentProgress = Math.round(Number(progress) * 100);
						if(Progress.currentProgress < 0){
							Progress.currentProgress = 0;
						}
						if(Progress.currentProgress > 100){
							Progress.currentProgress = 0;
						}
						
						Progress.timer = setInterval(() => {
							console.log('progress...',nowVal);
							nowVal++;
							if(nowVal > Progress.currentProgress){
								Progress.timer && clearInterval(Progress.timer);
								return;
							}
							$progressVal.html(nowVal);
							$progressShow.css('width',nowVal + '%');
							if(nowVal >= 100){
								clearInterval(Progress.timer);
								$progressComp.css('display','none');
							}
						},17);
					}
				}
			}
			
			$('#btn').click(() => {
				const title = 'nameABC';
				let progress = 0;
				const timer = setInterval(() => {
					if(progress >= 1){
						clearInterval(timer);
					}
					Progress.show(title,progress);
					progress += 0.25;
				},100);
				
			});
		</script>
	</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值