您的位置:首页 > Web前端 > JavaScript

重寫一下js的alert函數

2015-07-20 15:52 495 查看
$.Alert={
	verticalOffset:-75,horizontalOffset:0,
	alert:function(msg,callback){
		$.Alert._show(msg,'alert',function(result){if(callback)callback(result)})
	}
	,_show:function(msg,type,callback){
		$.Alert._hide();
		$.Alert._overlay('show');
		$("BODY").append('<div id="popup_container" style="font-family:Arial,sans-serif;line-height:25px;font-size:12px;min-width:300px;max-width:600px;border:1px solid #ddd;color:#000;-moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;background-color:#fff"><div id="popup_content" style="margin:0;padding:10px"><div id="popup_message"></div></div></div>');
		//var pos=($.browser.msie&&parseInt($.browser.version)<=6)?'absolute':'fixed';
		var pos='fixed';
		$("#popup_container").css({position:pos,zIndex:99999,padding:0,margin:0});
		$("#popup_message").html(msg.replace(/\n/g,'<br>'));
		//$("#popup_message").html($("#popup_message").text().replace(/\n/g,'<br />'));
		$("#popup_container").css({minWidth:$("#popup_container").outerWidth(),maxWidth:$("#popup_container").outerWidth()});
		$.Alert._creat();
		$(window).bind('resize',function(){$.Alert._creat()});
		if(type=='alert'){
			$("#popup_message").after('<div style="text-align:center;margin-top:20px"><button id="popup_ok" style="width:70px;border:1px solid #444;color:#000;-moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px">確認</button></div>');
			$("#popup_ok").click(function(){$.Alert._hide();callback(true)});
			$("#popup_ok").focus().keypress(function(e){if(e.keyCode==13||e.keyCode==27)$("#popup_ok").trigger('click')});
		}
	}
	,_hide:function(){
		$("#popup_container").remove();
		$.Alert._overlay('hide');
		$(window).unbind('resize');
	}
	,_overlay:function(status){
		switch(status){
			case'show':$.Alert._overlay('hide');$("BODY").append('<div id="popup_overlay"></div>');$("#popup_overlay").css({position:'absolute',zIndex:99998,top:'0px',left:'0px',width:'100%',height:$(document).height(),backgroundColor:'#000',opacity:0.7});break;
			case'hide':$("#popup_overlay").remove();break
		}
	}
	,_creat:function(){
		var top=(($(window).height()/2) - ($("#popup_container").outerHeight()/2))+$.Alert.verticalOffset;
		var left=(($(window).width()/2) - ($("#popup_container").outerWidth()/2))+$.Alert.horizontalOffset;
		if(top<0)top=0;
		if(left<0)left=0;
		//if($.browser.msie&&parseInt($.browser.version)<=6)top=top+$(window).scrollTop();
		$("#popup_container").css({top:top+'px',left:left+'px'});
		$("#popup_overlay").height($(document).height())
	}
};window.alert=function(msg,callback){$.Alert.alert(msg,callback)};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: