现在的位置: 首页 > 综合 > 正文

javascript.async异步加载页面。

2012年03月13日 ⁄ 综合 ⁄ 共 840字 ⁄ 字号 评论关闭
/*
 *异步js 扩展加载标识
 *@author 
 */
(function($){
	if($.fn.async) {
		return;
	}
	if(!document.body) {
		document.write("<body/>");
	}
	$.fn.extend({
		async : function(url,obj,callback) {//异步路径,指定异步返回数据填充的div,回调函数
			if(!this) {
				alert("对象未找到...");
				return;
			}
			var height;
			if(obj) {
				var _obj = typeof(obj);
				if( _obj == "function")  {
					callback = obj;
				}else if(_obj == "number") {
					height = obj;
				}
			}
			obj = this.wrap("<div style='position:relative;width:100%;'/>");
			var o = obj.parent();
			var img = $("<img src='/common/images/loading.gif' style='position:absolute;z-index:9999;display:none;'/>").appendTo(o);
			var h = img.height();
			var oh = obj.height();
			var top = height ? height : oh < h ? 0 : (oh-h)/4;
			img.css({"left":(obj.width()-img.width())/2,"top":top < 20 ? 20 : top,"display":""});
			$.ajax({
				url  : url,
				timeout : 30000,
				complete : function() {
					o.replaceWith(obj);
					if($.isFunction(callback)) {
						callback();
					}
				},
				success : function(data) {
					obj.html(data);
				}
			})
		}
	});
})(jQuery);

加载图片:

抱歉!评论已关闭.