前端公用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('添加成功!')
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值