前端公用Toast弹框

var Toast = function(config){
	this.context = config.context==null?$('body'):config.context;//上下文
	this.message = config;//显示内容.message
	this.time = config.time==null?3000:config.time;//持续时间
	this.left = config.left;//距容器左边的距离
	this.top = config.top;//距容器上方的距离
	Toast.fn.init(this.context,this.message,this.left,this.top);
	Toast.fn.show(this.time);
}
var msgEntity;
Toast.fn=Toast.prototype = {
	//初始化显示的位置内容等
	init : function(context,message){
		$("#toastMessage").remove();
		//设置消息体
		var msgDIV = new Array();
		msgDIV.push('<div id="toastMessage" style="-moz-opacity:0.6;opacity:0.6;">');
		msgDIV.push('<span>'+message+'</span>');
		msgDIV.push('</div>');
		msgEntity = $(msgDIV.join('')).appendTo(context);
		//设置消息样式
        // var left = left == null ? context.width()/2-msgEntity.find('span').width()/2-20 : left;
		var top = top == null ? '30%' : top;
		msgEntity.css({position:'fixed',bottom:top,'z-index':'99999',left:'50%','background-color':'black',color:'white','font-size':'15px',padding:'10px 20px',transform :'translate(-50%)'});
		msgEntity.hide();
	},
	//显示动画
	show :function(time){
		msgEntity.fadeIn(time/5);
		msgEntity.fadeOut(time/2);
	}

}
Toast.fn.init.prototype=Toast.fn;

效果
在这里插入图片描述
弱提示弹框 居中显示 3秒后自动消失
该方法可放到公共文件中 所有页面可调用该方法

//调用该方法
Toast('添加成功!')

猜你喜欢

转载自blog.csdn.net/j244233138/article/details/106527225
今日推荐