Html代码如下:
<a href="#" id="load">Load</a>
<div id="tb">
<div id="result">
</div>
</div>
“body” 或是 “Div” 加上Loading遮罩!
/******重写Ajax操作,做成通用Loading操作*******/
$.ajaxLoading = function (options, aimDiv) {
var img = $("<img id=\"progressImgage\" src=\"/Images/ajax-loader.gif\" />"); //Loading小图标
var mask = $("<div id=\"maskOfProgressImage\"></div>").addClass("mask").addClass("hide"); //Div遮罩
var PositionStyle = "fixed";
//是否将Loading固定在aimDiv中操作,否则默认为全屏Loading遮罩
if (aimDiv != null && aimDiv != "" && aimDiv != undefined) {
$(aimDiv).css("position", "relative").append(img).append(mask);
PositionStyle = "absolute";
}
else {
$("body").append(img).append(mask);
}
img.css({
"z-index": "2000",
"display": "none"
})
mask.css({
"position": PositionStyle,
"top": "0",
"right": "0",
"bottom": "0",
"left": "0",
"z-index": "1000",
"background-color": "#000000",
"display": "none"
});
var complete = options.complete;
options.complete = function (httpRequest, status) {
img.hide();
mask.hide();
if (complete) {
complete(httpRequest, status);
}
};
options.async = true;
img.show().css({
"position": PositionStyle,
"top": "40%",
"left": "50%",
"margin-top": function () { return -1 * img.height() / 2; },
"margin-left": function () { return -1 * img.width() / 2; }
});
mask.show().css("opacity", "0.1");
$.ajax(options);
};
调用如下:
$("#load").click(function() {
$.ajaxLoading({
url:’@Url.Action(“Index”)?’ + new Date().toTimeString(),
success: function (result) {
$("#result").html(result);
}
}, “#tb”);
});