jquery.modal.js

/**
 * Date:2016-09-19
 * $.modal.alert(msg,function(){});
 * $.modal.confirm(msg,function(boolean){});
 * $.modal.prompt(msg,function(false|value){});
 * $.modal.msg(msg,function(){});
 * $.modal.toast(msg,function(){});
 * $.modal.html(html,function(){});
 * $.modal.load(url,function(){});
 * $.modal.open({type:,title:,content:,time:,callback:,url:,style:});
 */
(function($){
	$.modal = {
		config: {
			init: false, //初始化标志
			type: "msg",
			title: "消息提示",
			content: "",
			time: 1000,
			callback: false,
			url: "",
			style: null
		},
		
		alert: function(str,callfun){
			$.modal._open({type:"alert",content:str,callback:callfun});
		},
		confirm: function(str,callfun){
			$.modal._open({type:"confirm",content:str,callback:callfun});
		},
		prompt: function(str,callfun){
			$.modal._open({type:"prompt",content:str,callback:callfun});
		},
		msg: function(str,callfun){
			$.modal._open({type:"msg",content:str,callback:callfun});
		},
		toast: function(str,callfun){
			$.modal._open({type:"toast",content:str,callback:callfun});
		},
		html: function(str,callfun){
			$.modal._open({type:"html",content:str,callback:callfun});
		},
		load: function(str,callfun){
			$.modal._open({type:"load",url:str,callback:callfun});
		},
		open: function(opt){
			$.modal._open(opt);
		},
		
		_open: function(options){
			var cfg = $.extend({},$.modal.config,options);
			
			$.modal._init(); //仅初始化一次
			$.modal._hide(); //先销毁
			$.modal._creat(); //再创建
			$.modal._init(cfg.style); //自定义样式
			
			switch(cfg.type){
				case "alert":
					$("#m_cover .body").html(cfg.content);
					$("#m_cover,#m_cover .close,#m_cover .yes").click(function(){$.modal._hide();if(cfg.callback) cfg.callback();});
					$("#m_cover .modal").click(function(e){e.stopPropagation();});
					$("#m_cover").addClass("alert");
					$.modal._show();
				break;
				case "confirm":
					$("#m_cover .body").html(cfg.content);
					$("#m_cover .yes").click(function(){$.modal._hide();if(cfg.callback) cfg.callback(true);});
					$("#m_cover,#m_cover .close,#m_cover .no").click(function(){$.modal._hide();if(cfg.callback) cfg.callback(false);});
					$("#m_cover .modal").click(function(e){e.stopPropagation();});
					$("#m_cover").addClass("confirm");
					$.modal._show();
				break;
				case "prompt":
					$("#m_cover .body").html(cfg.content+':<br><input type="text" class="m_input" name="m_input" />');
					$("#m_cover .yes").click(function(){var value = $(".m_input").val();$.modal._hide();if(cfg.callback) cfg.callback(value);});
					$("#m_cover,#m_cover .close,#m_cover .no").click(function(){$.modal._hide();if(cfg.callback) cfg.callback(false);});
					$("#m_cover .modal").click(function(e){e.stopPropagation();});
					$("#m_cover").addClass("prompt");
					$.modal._show();
				break;
				case "msg":
					$("#m_cover .body").html(cfg.content);
					$("#m_cover,#m_cover .close").click(function(){$.modal._hide();if(cfg.callback) cfg.callback();});
					$("#m_cover .modal").click(function(e){e.stopPropagation();});
					$("#m_cover").addClass("msg");
					$.modal._show();
					$("#m_cover").delay(cfg.time).fadeOut(500,function(){$.modal._hide();if(cfg.callback) cfg.callback();});
				break;
				case "toast":
					$("#m_cover .body").html(cfg.content);
					$("#m_cover").addClass("toast");
					$.modal._show();
					$("#m_cover").delay(cfg.time).fadeOut(500,function(){$.modal._hide();if(cfg.callback) cfg.callback();});
				break;
				case "html":
					if(cfg.title) $("#m_cover .title").html(cfg.title);
					$("#m_cover .body").html(cfg.content);
					$("#m_cover,#m_cover .close").click(function(){$.modal._hide();if(cfg.callback) cfg.callback();});
					$("#m_cover .modal").click(function(e){e.stopPropagation();});
					$("#m_cover").addClass("html");
					$.modal._show();
				break;
				case "load":
					$("#m_cover .body").html('<iframe id="m_frame" width="100%" height="100%" frameborder="0" scrolling="auto"></iframe>');
					$("#m_cover .body #m_frame").attr("src",cfg.url);
					$("#m_cover,#m_cover .close").click(function(){$.modal._hide();if(cfg.callback) cfg.callback();});
					$("#m_cover .modal").click(function(e){e.stopPropagation();});
					$("#m_cover").addClass("load");
					$.modal._show();
				break;
			}
		},
		_show: function(){
			$("#m_cover").show();
			$("#m_cover .modal").css("top",(($("#m_cover").height()-$("#m_cover .modal").height())/2)+"px");
			$("#m_cover .modal").css("left",(($("#m_cover").width()-$("#m_cover .modal").width())/2)+"px");
		},
		_hide: function(){
			$("#m_cover").remove();
		},
		_creat: function(){
			var str = '<div id="m_cover">'+
					'<div class="modal">'+
						'<span class="close">×</span>'+
						'<dt class="title">消息提示</dt>'+
						'<dd class="body"></dd>'+
						'<ul class="foot">'+
							'<li class="yes"><span>确定</span></li><li class="no"><span>取消</span></li>'+
						'</ul>'+
					'</div></div>';
			$(str).appendTo("body");
		},
		_init: function(css){
			if(!$.modal.config.init){
				$('<style type="text/css">'+
				'#m_cover{position:fixed;top:0px;left:0px;width:100%;height:100%;background:rgba(0, 0, 0, 0.6);z-index:1000000;display:none;}'+
				'#m_cover .modal{width:80%;max-width:400px;height:auto;background:#fff;position:absolute;top:0;z-index:7;border-radius:5px;overflow:hidden;}'+
				'#m_cover .modal>.close{position:absolute;top:0px;right:0px;width:45px;height:45px;line-height:45px;color:#fff;font-weight:bold;font-size:25px;text-align:center;cursor:pointer;display:none;}'+
				'#m_cover .modal>.title{height:45px;line-height:45px;color:#fff;text-indent:20px;background:#169673;font-size:16px;}'+
				'#m_cover .modal>.body{min-height:70px;margin:0px;padding:15px;line-height:24px;word-wrap:break-word;word-break:break-all;font-size:14px;}'+
				'#m_cover .modal>.foot{display:table;border-top:1px solid #ccc;width:100%;height:43px;line-height:43px;margin:0px;padding:1px 0px;display:none;}'+
				'#m_cover .modal>.foot>li{display:table-cell;width:50%;text-align:center;}'+
				'#m_cover .modal>.foot>li.no{border-left:1px solid #eee;display:none;}'+
				
				'#m_cover.alert .modal>.foot,#m_cover.confirm .modal>.foot,#m_cover.prompt .modal>.foot{display:table}'+
				
				'#m_cover.confirm .modal>.foot>li.no,#m_cover.prompt .modal ul.foot>li.no{display:table-cell}'+
				
				'#m_cover.prompt .modal>.body>input{width:100%;height:26px;text-indent:3px;margin-top:7px;box-sizing:border-box;}'+
				
				'#m_cover.toast{width:80%;height:auto;top:auto;left:10%;bottom:60px;background:transparent;}'+
				'#m_cover.toast .modal{color:#fff;width:100%;left:0px;top:0px;margin-left:0px;margin-top:0px;background:rgba(0, 0, 0, 0.7);}'+
				'#m_cover.toast .modal>.title{display:none;}'+
				'#m_cover.toast .modal>.body{min-height:auto;}'+
				
				'#m_cover.html .modal>.close{display:block;}'+
				
				'#m_cover.load .modal>.title{display:none;}'+
				'#m_cover.load .modal>.body{height:100%;padding:0px;}'+
				'</style>').appendTo("body");
				$.modal.config.init = true;
			}else{
				if(typeof(css) == "string") $('<style type="text/css">'+css+'</style>').appendTo("#m_cover");
			}
		}
	}
	
	//兼容旧版消息提示
	window.tips = function(msg,url){
		var callfun = null;
		if(typeof(url) == "function") callfun = url;
		else if(typeof(url) == "string") callfun = function(){window.location.href=url;};
		$.modal.msg(msg,callfun);
	}
})(jQuery);

调用只需【一个或2个参数】

tips("登录成功", function() {
window.location.href = "?pfid=1&" + Date.parse(new Date());
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值