Jquery 点击页面按钮,显示loading样式

点击页面按钮,显示loading样式

HTML代码

<div id="loading-bg">
	<div id="loading"><img src="/common/images/common/loading.svg" width="24" height="30"/></div>
	<div id="loading-message"></div>
</div>

JS代码

loading页面显示/隐藏函数。
页面文字屏幕中央显示,文字内容由参数message设置。

function loading(mode, message) {
	let $loadingBg = $("#loading-bg");
	$("#loading-message").html("");

	if (!mode || mode == "show") {
		$loadingBg.css("height", document.documentElement.clientWidth + "px");
		$loadingBg.show();
		let h = $("#loading").css("height");
		h = parseInt(h, 10) / 2;
		let wh = $(window).height();
		wh = wh / 2;
		h = (wh - h);
		$("#loading").css("top", h +"px");

		$("#loading-message").css("top", (h + 35) +"px");
		if (message) {
			$("#loading-message").html(message);
		}

	} else if (mode == "hide") {
		$loadingBg.hide();
	}
}
$.ajax({
	"url":...,
	// ajax请求参数
	"beforeSend":function() {
		loading("show", "数据加载中...");
	},
}).done(function() {
	loading("hide");
	// ajax请求成功后处理
}).fail(function() {
	loading("hide");
	// ajax请求失败后处理
});

css样式

#loading-bg {
    background: rgba(0, 0, 0, 0.3);
    position: absolute;
    width: 100%;
    text-align: center;

    z-index: 2000;

    left: 0px;
    top: 0px;
    display: none;
}

#loading {
    width: 100%;
    position: fixed;
    top: 50%;
}

#loading-message {
    width: 100%;
    position: fixed;
    top: 50%;
    color: #fff;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值