js 弹出alert框

js code:

/**
 *   提示弹出框
 *   用法:$toast("网络超时,请重试!",1000); 自动消失
 *   //	$toast("加载中...");   手动消失方法。    $closeToast(1); 关闭方法。
 **/
var missshield= null ;
var missalertFram = null ;
function $toast(text,ms)
{
	if(isEmtp(ms)){
	missshield= document.createElement("DIV");
	missalertFram = document.createElement("DIV");
	missshield.style.position = "absolute";
	missshield.style.left = "0px";
	missshield.style.top = "0px";
	missshield.style.width = "100%";
	missshield.style.height = ((document.documentElement.clientHeight>document.documentElement.scrollHeight)?document.documentElement.clientHeight:document.documentElement.scrollHeight)+"px";
	missshield.style.background = "#333";
	missshield.style.textAlign = "center";
	missshield.style.zIndex = "10000";
	missshield.style.filter = "alpha(opacity=0)";
	missshield.style.opacity = 0.5;
	missalertFram.id="alertFram";
	missalertFram.style.position = "absolute";
	missalertFram.style.left = "50%";
	missalertFram.style.top = "50%";
	missalertFram.style.marginTop = -120+document.documentElement.scrollTop+"px";
	missalertFram.style.textAlign = "center";
	missalertFram.style.lineHeight = "-20px";
	missalertFram.style.zIndex = "10001";
	missshield.id = "shield";
	missalertFram.style.marginLeft = "-100px" ;
	missalertFram.style.width = "200px";
	missalertFram.style.height = "100px";
	strHtml   = "<ul style=\"list-style:none;margin:0px;padding:0px;width:100%;opacity: 1;\">\n";
	strHtml += " <li style=\"background:rgba(0,0,0,0.8);text-align:center;height:100px;line-height:30px;border-radius: 10px;font-style:inherit;color: white;\"><img src=\""+imageurl+"css/images/ajax-loader.gif\" width=\"35px;\" height=\"35px;\" style=\padding-top:10px;\"><p style=\"text-align:center;font-size:19px;height:50px;font-style:inherit;color: white;\"> "+text+"</p></li>\n";
	strHtml += "</ul>\n";
	missalertFram.innerHTML = strHtml;
	document.body.appendChild(missalertFram);
	document.body.appendChild(missshield);
	this.setOpacity = function(obj,opacity){
	   if(opacity>=1)opacity=opacity/100;
	   try{ obj.style.opacity=opacity; }catch(e){}
	   try{ 
	    if(obj.filters.length>0&&obj.filters("alpha")){
	     obj.filters("alpha").opacity=opacity*100;
	    }else{
	     obj.style.filter="alpha(opacity=\""+(opacity*100)+"\")";
	    }
	   }catch(e){}
	};
	var c = 0;
	this.doAlpha = function(){
	   if (++c > 20){clearInterval(ad);return 0;}
	   setOpacity("shield",c);
	};
	var ad = setInterval("doAlpha()",1);  
	 document.body.onselectstart = function(){return true;};
	 document.body.oncontextmenu = function(){return true;};
	}
	else{
	missshield= document.createElement("DIV");
	missalertFram = document.createElement("DIV");
	missshield.style.position = "absolute";
	missshield.style.left = "0px";
	missshield.style.top = "0px";
	missshield.style.width = "100%";
	missshield.style.height = ((document.documentElement.clientHeight>document.documentElement.scrollHeight)?document.documentElement.clientHeight:document.documentElement.scrollHeight)+20+"px";
	missshield.style.background = "#333";
	missshield.style.textAlign = "center";
	missshield.style.zIndex = "10000";
	missshield.style.filter = "alpha(opacity=0)";
	missshield.style.opacity = 0.5;
	missalertFram.id="alertFram";
	missalertFram.style.position = "absolute";
	missalertFram.style.left = "50%";
	missalertFram.style.top = "50%";
	missalertFram.style.marginTop = -120+document.documentElement.scrollTop+"px";
	missalertFram.style.textAlign = "center";
	missalertFram.style.lineHeight = "-20px";
	missalertFram.style.zIndex = "10001";
	missshield.id = "shield";
	missalertFram.style.marginLeft = "-120px" ;
	missalertFram.style.width = "240px";
	missalertFram.style.height = "100px";
		var px = 90;	
		if(text.length>13&&text.length<26){
			 px = 45;	
			 text = text.substring(0,text.length/2) +"<br>"+text.substring(text.length/2,text.length);
		}else if(text.length>26&&text.length<39){
			 px = 30;	
			 text = text.substring(0,text.length/3) +"<br>"+text.substring(text.length/3,text.length-text.length/3)+"<br>"+text.substring(text.length-text.length/3,text.length);
		}
	strHtml   = "<ul style=\"list-style:none;margin:0px;padding:0px;width:100%;opacity: 1;\">\n";
	strHtml += " <li style=\"background:rgba(0,0,0,0.8);text-align:center;font-size:17px;height:90px;line-height:"+px+"px;border-radius: 10px;font-style:inherit;color: white;\">"+text+"</li>\n";
	strHtml += "</ul>\n";
	missalertFram.innerHTML = strHtml;
	document.body.appendChild(missalertFram);
	document.body.appendChild(missshield);
	this.setOpacity = function(obj,opacity){
	   if(opacity>=1)opacity=opacity/100;
	   try{ obj.style.opacity=opacity; }catch(e){}
	   try{ 
	    if(obj.filters.length>0&&obj.filters("alpha")){
	     obj.filters("alpha").opacity=opacity*100;
	    }else{
	     obj.style.filter="alpha(opacity=\""+(opacity*100)+"\")";
	    }
	   }catch(e){}
	};
	var c = 0;
	this.doAlpha = function(){
	   if (++c > 20){clearInterval(ad);return 0;}
	   setOpacity("shield",c);
	};
	var ad = setInterval("doAlpha()",1);  
	 document.body.onselectstart = function(){return true;};
	 document.body.oncontextmenu = function(){return true;};
	}
	 if(!isEmtp(ms)){
		 setTimeout("$closeToast()",ms);
	}
};
/**
 *   关闭
 **/
function $closeToast(){
	try {
			
			document.body.removeChild(missalertFram);
			document.body.removeChild(missshield);
		    document.body.onselectstart = function(){return false;};
			document.body.oncontextmenu = function(){return false;};		
			missalertFram.style.display = "none";
			missshield.style.display = "none";
		} catch (e) {
			document.body.removeChild(document.getElementById("alertFram"));
			document.body.removeChild(document.getElementById("shield"));
		    document.body.onselectstart = function(){return false;};
			document.body.oncontextmenu = function(){return false;};		
			missalertFram.style.display = "none";
			missshield.style.display = "none";
		}
		
}

/**
 *   空验证
 **/
function isEmtp(val){
	if(val == null || val == "" || val == "undefined" || val == undefined || val == "null" || val == "(null)" || val == 'NULL' || typeof(val) == 'undefined')
    {
    	return false;
	}else {
		return false;
	}
}	

使用时,直接js调用:(弹出abc 2秒)
$toast("abc",2000)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值