前端页面简单添加进度条

前端添加进度条

具体步骤如下:

  • 前端代码:在页面添加下面代码
<div class="processcontainer" title="正在处理,请耐心等候...">
	<div id="processbar" style="width:0%"></div>
</div>
  • 进度条样式:如果认为样式不太好,可以自己调试改动
<style type="text/css">
			.processcontainer{
				width:350px;
				border:1px solid #999a9c;
				height:22px;
				/* background:transparent; */
				background:rgba(0,0,0,0.2);
				position:fixed;
				margin:auto;
				left:0;
				right:0;
				top:0;
				bottom:0;
				z-index:1;
				display:none;
			}
			.processcontainer::before{
				content:'';
				position:fixed;
				width:100%;
				height:100%;
				left:0;
				top:0;
				background:rgba(0,0,0,0.2);
			}
			#processbar{
				background:#2d8cf0;
				float:left;
				height:100%;
				text-align:center;
				line-height:150%;
			}
	</style>
  • 请求后台接口的js方法:此处需要单独写相应的后台接口来实现
/**
 * 获取当前进度
 * @returns
 */
function setProcess(){
	$.ajax({
		url : "xx/getSession",
		type : "post",
		data : [],
		success : function(result) {
			var processbar = document.getElementById("processbar");
			processbar.style.width = result.obj + "%";
			processbar.innerHTML = parseInt(processbar.style.width) + "%";
			/*if(processbar.style.width == "100%"){}*/
		},
		error : function(errInfo) {
			layer.alert("获取进度失败!");
		}
	})
}
  • 关闭进度条js方法:
/**
 * 关闭进度条
 * @returns
 */
function closeProcess(){
	window.clearInterval(bartimer);
    $(".processcontainer").hide();
    $("#processbar").css("width","0%");
    $("#processbar").html("");
    return;
}
  • 页面调用获取进度条的方法,添加定时器,重复执行setProcess方法:
 	//定时请求后台获取进度session值
	bartimer = window.setInterval(function(){setProcess();},2000);//注意bartimer 为全局变量
 	$(".processcontainer").show();
  • 后台接口代码很简单,获取session中的进度数字值:
/**
	 * 获取实时进度
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/getSession", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getSession(HttpSession session){
		try {
			String count = (String) session.getAttribute(Constants.SESSION_PLRXBK);
			return new JsonResult(true,"请求成功!",count); 
		} catch (Exception e) {
			return new JsonResult(false,"获取进度失败!");
		}
	}
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值