1.先写一个遮罩层和动态图片
loading = {
show: function () {
var html = '<div class="loading_image aiabase parbase section" tabindex="-1"' +
'style="overflow: hidden; position: fixed;top: 0;right: 0;bottom: 0;left: 0;min-width: 320px;z-index: 1050;outline: 0;background: rgba(0, 0, 0, 0.4);">' +
'<div class="landing" style="height: 100%;display: flex;align-items: center;flex-direction: column;justify-content: center;">' +
'<img src="src/img/moving-loading.gif" style="width: 30%;max-width: 150px;"/>' +
'</div></div>';
$('body').append(html);
$('body').addClass('loading_body');
},
hide: function () {
if ($('.loading_image')) {
$('.loading_image').remove();
$('body').removeClass('loading_body');
}
},
}
2.加在ajax 中
function ajaxForSth(data, successCallback, errorCallback, optionToken) {
if (!$('.loading_image').html()) {
loading.show();
}
var showLoading = false; // 标志
var options = {
type: 'POST',
dataType: 'json',
data: data,
headers: { 'Content-Type': 'application/json' },
url: url,
cache: false,
headers: { Authorization: "Bearer " + optionToken },
};
$.ajax(options).done(function (data) {
if (!showLoading) {
ctx.loading.hide();
}
if (typeof (successCallback) == 'function') {
successCallback(data);
}
}).fail(function (jqXHR, status, errorThrown) {
if (!showLoading) {
ctx.loading.hide();
}
if (jqXHR.status == 401 && typeof (errorCallbackFour) == 'function') {
errorCallbackF(jqXHR, status, errorThrown);
}
});
}
就实现了。