美化js系统函数alert,confirm,prompt,并实现lightbox效果

注意:1)alert,confirm及prompt并不同于系统的,这个是用层模仿的,并不能挂起程序的执行

2)所以如果需要在确认后执行相关的操作,需要在配置文件中传递回调函数fn
3)按钮只提供yes和no两个

点击查看示例效果

示例代码下载

msg.js

var Showbo={author:'showbo',homepage:'http://www.w3dev.cn'}; //是否为ie浏览器 Showbo.IsIE=!!document.all; //ie浏览器版本 Showbo.IEVersion=(function(){if(!Showbo.IsIE)return -1;try{return parseFloat(/msie ([/d/.]+)/i.exec(navigator.userAgent)[1]);}catch(e){return -1;}})(); //按id获取对象 Showbo.$=function(Id,isFrame){var o;if("string"==typeof(Id))o= document.getElementById(Id);else if("object"==typeof(Id))o= Id;else return null;return isFrame?(Showbo.IsIE?frames[Id]:o.contentWindow):o;} //按标签名称获取对象 //页面的高和宽****************************** Showbo.isStrict=document.compatMode == "CSS1Compat"; Showbo.BodyScale={x:0,y:0,tx:0,ty:0};//(x,y):当前的浏览器容器大小 (tx,ty):总的页面滚动宽度和高度 Showbo.getClientHeight=function(){/*if(Showbo.IsIE)*/return Showbo.isStrict ? document.documentElement.clientHeight :document.body.clientHeight;/*else return self.innerHeight;*/} Showbo.getScrollHeight=function(){var h=!Showbo.isStrict?document.body.scrollHeight:document.documentElement.scrollHeight;return Math.max(h,this.getClientHeight());} Showbo.getHeight=function(full){return full?this.getScrollHeight():this.getClientHeight();} Showbo.getClientWidth=function(){/*if(Showbo.IsIE)*/return Showbo.isStrict?document.documentElement.clientWidth:document.body.clientWidth;/*else return self.innerWidth;*/} Showbo.getScrollWidth=function(){var w=!Showbo.isStrict?document.body.scrollWidth:document.documentElement.scrollWidth;return Math.max(w,this.getClientWidth());} Showbo.getWidth=function(full){return full?this.getScrollWidth():this.getClientWidth();} Showbo.initBodyScale=function(){Showbo.BodyScale.x=Showbo.getWidth(false);Showbo.BodyScale.y=Showbo.getHeight(false);Showbo.BodyScale.tx=Showbo.getWidth(true);Showbo.BodyScale.ty=Showbo.getHeight(true);} //页面的高和宽****************************** Showbo.Msg={ INFO:'info', ERROR:'error', WARNING:'warning', IsInit:false, timer:null, dvTitle:null, dvCT:null, dvBottom:null, dvBtns:null, lightBox:null, dvMsgBox:null, defaultWidth:300, moveProcessbar:function(){ var o=Showbo.$('dvProcessbar'),w=o.style.width; if(w=='')w=20; else{ w=parseInt(w)+20; if(w>100)w=0; } o.style.width=w+'%'; }, InitMsg:function(width){ //ie下不按照添加事件的循序来执行,所以要注意在调用alert等方法时要检测是否已经初始化IsInit=true var ifStr='<iframe src="javascript:false" mce_src="javascript:false" style="position:absolute; visibility:inherit; top:0px;left:0px;width:100%; height:100%; z-index:-1;' +'filter=/'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)/';"></iframe>', html='<div class="top"><div class="right"><div class="title" id="dvMsgTitle"></div></div></div>'+ '<div class="body"><div class="right"><div class="ct" id="dvMsgCT"></div></div></div>'+ '<div class="bottom" id="dvMsgBottom"><div class="right"><div class="btn" id="dvMsgBtns"></div></div></div>'; this.dvMsgBox=document.createElement("div"); this.dvMsgBox.id="dvMsgBox"; this.dvMsgBox.innerHTML+=html; document.body.appendChild(this.dvMsgBox); this.lightBox=document.createElement("div"); this.lightBox.id="ShowBolightBox"; document.body.appendChild(this.lightBox); if(Showbo.IsIE&&Showbo.IEVersion<7){//加iframe层修正ie6下无法遮盖住select的问题 this.lightBox.innerHTML+=ifStr; this.dvMsgBox.innerHTML+=ifStr; } this.dvBottom=Showbo.$('dvMsgBottom'); this.dvBtns=Showbo.$('dvMsgBtns'); this.dvCT=Showbo.$('dvMsgCT'); this.dvTitle=Showbo.$('dvMsgTitle'); this.IsInit=true; }, checkDOMLast:function(){//此方法非常关键,要不无法显示弹出窗口。两个对象dvMsgBox和lightBox必须处在body的最后两个节点内 if(document.body.lastChild!=this.lightBox){ document.body.appendChild(this.dvMsgBox); document.body.appendChild(this.lightBox); } }, createBtn:function(p,v,fn){ var btn=document.createElement("input"); btn.type="button"; btn.className='btn'; btn.value=v; btn.οnmοuseοver=function(){this.className='btnfocus';} btn.οnmοuseοut=function(){this.className='btn';} btn.οnclick=function(){ Showbo.Msg.hide(); if(fn)fn(p); } return btn; }, alert:function(msg){ this.show({buttons:{yes:'确认'},msg:msg}); }, confirm:function(msg,fn){ //fn为回调函数,参数和show方法的一致 this.show({buttons:{yes:'确认',no:'取消'},msg:msg,title:'提示',fn:fn}); }, prompt:function(labelWord,defaultValue,txtId,fn){ if(!labelWord)labelWord='请输入:'; if(!defaultValue)defaultValue=""; if(!txtId)txtId="msg_txtInput"; this.show({title:'输入提示',msg:labelWord+'<input type="text" id="'+txtId+'" style="width:200px" value="'+defaultValue+'"/>',buttons:{yes:'确认',no:'取消'},fn:fn}); }, wait:function(msg,title){ if(!msg)msg='正在处理..'; this.show({title:title,msg:msg,wait:true}); }, show:function(cfg){ //cfg:{title:'',msg:'',wait:true,icon:'默认为信息',buttons:{yes:'',no:''},fn:function(btn){回调函数,btn为点击的按钮,可以为yes,no},width:显示层的宽} //如果是等待则wait后面的配置不需要了。。 if(!cfg)throw("没有指定配置文件!"); //添加窗体大小改变监听 if(Showbo.IsIE)window.attachEvent("onresize",this.onResize); else window.addEventListener("resize",this.onResize,false); if(!this.IsInit)this.InitMsg();//初始化dom对象 else this.checkDOMLast();//检查是否在最后 //检查是否要指定宽,默认为300 if(cfg.width)this.defaultWidth=cfg.width; this.dvMsgBox.style.width=this.defaultWidth+'px'; //可以直接使用show方法停止为进度条的窗口 if(this.timer){clearInterval(this.timer);this.timer=null;} this.dvTitle.innerHTML=''; if(cfg.title)this.dvTitle.innerHTML=cfg.title; this.dvCT.innerHTML=''; if(cfg.wait){ if(cfg.msg)this.dvCT.innerHTML=cfg.msg; this.dvCT.innerHTML+='<div class="pro"><div class="bg" id="dvProcessbar"></div></div>'; this.dvBtns.innerHTML=''; this.dvBottom.style.height='10px'; this.timer=setInterval(function(){Showbo.Msg.moveProcessbar();},1000); } else{ //if(!cfg.icon)cfg.icon=Showbo.Msg.INFO; if(!cfg.buttons||(!cfg.buttons.yes&&!cfg.buttons.no)){ cfg.buttons={yes:'确定'}; } if(cfg.icon)this.dvCT.innerHTML='<div class="icon '+cfg.icon+'"></div>'; if(cfg.msg)this.dvCT.innerHTML+=cfg.msg+'<div class="clear"></div>'; this.dvBottom.style.height='45px'; this.dvBtns.innerHTML='<div class="height"></div>'; if(cfg.buttons.yes){ this.dvBtns.appendChild(this.createBtn('yes',cfg.buttons.yes,cfg.fn)); if(cfg.buttons.no)this.dvBtns.appendChild(document.createTextNode(' ')); } if(cfg.buttons.no)this.dvBtns.appendChild(this.createBtn('no',cfg.buttons.no,cfg.fn)); } Showbo.initBodyScale(); this.dvMsgBox.style.display='block'; this.lightBox.style.display='block'; this.onResize(false); }, hide:function(){ this.dvMsgBox.style.display='none'; this.lightBox.style.display='none'; if(this.timer){clearInterval(this.timer);this.timer=null;} if(Showbo.IsIE)window.detachEvent('onresize',this.onResize); else window.removeEventListener('resize',this.onResize,false); }, onResize:function(isResize){ if(isResize)Showbo.initBodyScale(); Showbo.Msg.lightBox.style.width=Showbo.BodyScale.tx+'px'; Showbo.Msg.lightBox.style.height=Showbo.BodyScale.ty+'px'; Showbo.Msg.dvMsgBox.style.top=document.documentElement.scrollTop+Math.floor((Showbo.BodyScale.y-Showbo.Msg.dvMsgBox.offsetHeight)/2)+'px'; Showbo.Msg.dvMsgBox.style.left=Math.floor((Showbo.BodyScale.x-Showbo.Msg.dvMsgBox.offsetWidth)/2)+'px'; } }

msg.css

/*Msg*/ #dvMsgBox{display:none;position:absolute;font-size:12px;width:300px;background:transparent url(top-bottom.png) repeat-x;overflow:hidden;z-index:999;} #dvMsgBox .top{height:24px;background:transparent url(left-corners.png) no-repeat;padding-left:6px;} #dvMsgBox .top .right{height:100%;background:transparent url(right-corners.png) no-repeat right top;padding-right:6px;} #dvMsgBox .top .right .title{cursor:move;background:transparent url(top-bottom.png) repeat-x;height:100%;line-height:24px;color:#15428b;vertical-align:middle;font-weight:bold;overflow:hidden;} #dvMsgBox .body{background:transparent url(left-right.png) repeat-y;padding-left:10px;} #dvMsgBox .body .right{background:transparent url(left-right.png) repeat-y right;padding-right:2px;} #dvMsgBox .body .right .ct{background-color:#cddef3 !important;background-color:#cadaec;line-height:20px;vertical-align:middle;width:100%;} #dvMsgBox .body .right .ct .pro{width:280px;border:solid 1px #6593cf;height:25px;background:#ffffff;line-height:23px;overflow:hidden;} #dvMsgBox .body .right .ct .pro .bg{width:0%;height:100%;background:#c9dffc;} #dvMsgBox .bottom{background:transparent url(left-corners.png) no-repeat left bottom;padding-left:6px;} #dvMsgBox .bottom .right{height:100%;background:transparent url(right-corners.png) no-repeat right bottom;padding-right:6px;} #dvMsgBox .bottom .right .btn{height:100%;background:transparent url(top-bottom.png) repeat-x left bottom;text-align:center;} #dvMsgBox .bottom .right .btn input{border:0px;width:70px;height:22px;text-align:center;line-height:22px;vertical-align:middle;cursor:pointer;} #dvMsgBox .bottom .right .btn input.btn{background:url(btn.gif) no-repeat;} #dvMsgBox .bottom .right .btn input.btnfocus{background:url(btnfocus.gif) no-repeat;} #dvMsgBox .icon{width:32px;height:32px;float:left;margin-right:10px;} #dvMsgBox .error{background:url(icon-error.gif) no-repeat;} #dvMsgBox .info{background:url(icon-info.gif) no-repeat;} #dvMsgBox .warning{background:url(icon-warning.gif) no-repeat;} #dvMsgBox .clear{clear:both;} #dvMsgBox .height{height:10px;line-height:10px;} #ShowBolightBox{display:none;-moz-opacity:0.5;filter:alpha(opacity=50);opacity:0.5;background-color:#000000;z-index:100;position:absolute;left:0px;top:0px;}

测试页面及说明

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>Web开发网--美化alert,confirm,并实现lightbox效果</title> <meta id="metaKey" name="Keywords" content="美化alert,confirm,并实现lightbox效果" /> <meta id="metaDes" name="Description" content="美化alert,confirm,并实现lightbox效果" /> <link href="images/msg.css" mce_href="images/msg.css" type="text/css" rel="Stylesheet" /> <mce:script type="text/javascript" src="msg.js" mce_src="msg.js"></mce:script> <mce:style type="text/css"><!-- body{font-size:14px;} --></mce:style><style type="text/css" mce_bogus="1"> body{font-size:14px;} </style> </head> <body> <h4>注意alert,confirm及prompt并不同于系统的,这个是用层模仿的,并不能挂起程序的执行<br /> 所以如果需要在确认后执行相关的操作,需要在配置文件中传递回调函数fn<br /> 按钮只提供yes和no两个 </h4> Showbo.Msg.alert:参数(要显示的信息)<br />如 Showbo.Msg.alert('你好!')<hr /> Showbo.Msg.confirm:参数(提示信息<font color="red">,回调函数</font>)<br />如 Showbo.Msg.confirm('确认删除?!',function(btn){alert('你点击的按钮为'+btn);})<hr /> Showbo.Msg.prompt:参数(输入框前的文字,输入框默认值,输入框id<font color="red">,回调函数</font>), <br />如 Showbo.Msg.prompt(null,null,function(btn){if(btn=='yes')alert('输入的值为/t/t'+Showbo.$('msg_txtInput').value);});<br />如果未指定“输入框id”, 输入框的id默认为'msg_txtInput"<hr /> Showbo.Msg.show:参数(cfg)<br /> <b>cfg:{title:'标题'<br />,msg:'信息内容'<br />,wait:true<br />,icon:'提示图标'<br />,buttons:{yes:'yes按钮显示的文字',no:'no按钮显示的文字'}<br />,width:显示的层宽度<br /><font color="red">,fn:function(btn){回调函数,btn为点击的按钮,可以为yes,no}</font>}</b> <br /> icon的值为Showbo.Msg.ERROR,Showbo.Msg.INFO,Showbo.Msg.WARNING 这个3个<br /> 上面的3个方法其实调用的还是此方法,show方法可以 <hr /> <br /> 上面的<font color="red">回调函数中参数只有一个,那就是点了哪个按钮!<b>[yes或者no]</b></font> <br /><br /><br />测试代码<br /><br /> <input type="button" value="Showbo.Msg.alert" οnclick="Showbo.Msg.alert('你好!')"/><br/> <input type="button" value="Showbo.Msg.confirm" οnclick="Showbo.Msg.confirm('确认删除?!',function(btn){alert('你点击的按钮为'+btn);})"/><br/> <input type="button" value="Showbo.Msg.prompt" οnclick="popInput()"/><br/> <input type="button" value="Showbo.Msg.wait" οnclick="Showbo.Msg.wait('正在更新信息...','请等待')"/><br/> 1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/>1<br/> 1<br/>1<br/>1<br/>1<br/>21<br/>21 <mce:script type="text/javascript"><!-- function popInput(){ //输入框的id为msg_txtInput Showbo.Msg.prompt(null,null,null,function(btn){ if(btn=='yes')alert('输入的值为/t/t'+Showbo.$('msg_txtInput').value); }); } // --></mce:script><span style="display:none" mce_style="display:none"><mce:script type='text/javascript' src="http://xslt.alexa.com/site_stats/js/t/a?url=http://www.w3dev.cn" mce_src="http://xslt.alexa.com/site_stats/js/t/a?url=http://www.w3dev.cn"></mce:script></span> </body> </html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
===============组件使用简要介绍=============== 1、在页面中引入ymPrompt.js。如:<script type="text/javascript" src="ymPrompt.js"></script> 2、在页面中引入对应的皮肤文件的CSS,如:<link rel="stylesheet" type="text/css" href="skin/qq/ymPrompt.css" /> 3、自定义组件的默认配置信息(此步骤可选,该方法可以在任意时间调用) 页面的js中通过ymPrompt.setDefaultCfg(cfg)方法修改组件部分或全部的默认属性。 如:ymPrompt.setDefaultCfg({maskAlpha:0.2,maskAlphaColor:'#00f'}) 组件的默认配置(对于没有设定的项将采用该配置项的默认值): { titleBar:true, //显示标题栏 fixPosition:true, //随滚动条浮动 dragOut:false, //不允许拖出页面 autoClose:true, //点击按钮后自动关闭页面 maskAlphaColor:'#000', //遮罩透明色 maskAlpha:0.1, //遮罩透明度 winAlpha:0.8, //拖动窗体时 窗体的透明度,默认为0.8 title: '标题', //消息框标题 message: '内容', //消息框按钮 width: 300, //宽 height: 185, //高 iframe:false, btn:null, icoCls:'', handler: function(){} //回调事件 showMask: true, //是否显示遮罩 winPos: 'c', //弹出窗口默认位置 closeBtn:true, //是否显示关闭按钮 //按钮文本,可通过自定义这些属性实现本地化 closeTxt: '关闭', okTxt:' 确 定 ', cancelTxt:' 取 消 ' } 4、根据您的需要调用相应的消息函数(两种参数传入方式): ymPrompt.alert(参数) //消息提示类型 ymPrompt.succeedInfo(参数) //成功信息类型 ymPrompt.errorInfo(参数) //错误信息类型 ymPrompt.confirmInfo(参数) //询问消息类型 ymPrompt.win(参数) //自定义窗口类型 参数传入方式包含两种: 第一种即传统的参数传入,按照顺序传入相应的参数值即可(一定要按照顺序),对于不需要设定的值请传入null。如ymPrompt.alert('内容',null,null,'标题') 参数顺序:message,width,height,title,handler,maskAlphaColor,maskAlpha, iframe,icoCls,btn,autoClose,fixPosition,dragOut,titleBar,showMask,winPos,winAlpha (推荐)第二种即JSON的传入方式,需要指定字段名,没有顺序,根据需要设定相关属性。如ymPrompt.alert({title:'标题',message:'内容'}) 五个方法的参数意义完全相同(所有参数均为可选,不传入则使用默认参数值),具体含义如下: message:消息组件要显示的内容,默认为“内容”。 width:消息框的宽度,默认为300。 height:消息框的高度,默认为185。 title:消息组件标题,默认为“标题” handler:回调函数。当确定/取消/关闭按钮被点击时会触发该函数并传入点击的按钮标识。如ok代表确定,cancel代表取消,close代表关闭 maskAlphaColor:遮罩的颜色,默认为黑色。 maskAlpha:遮罩的透明度,默认为0.1。 fixPosition:设定是否弹出框随滚动条一起浮动,保持在屏幕的固定位置,默认为true dragOut:设定是否允许拖出屏幕范围,默认为false。 autoClose:设定用户点击窗口中按钮后自动关闭窗口,默认为true(设定为false后程序中可以通过调用close方法关闭)。 titleBar:是否显示标题栏,默认显示。注意,如果没有标题栏需要自己在程序中控制关闭。 showMask:是否显示遮罩层,默认为true winPos:弹出窗口的位置,支持8种内置位置(c,l,t,r,b,lt,rt,lb,rb)及自定义窗口坐标,默认为c。 各参数意义:c:页面中间,l:页面左侧,t:页面顶部,r:页面右侧,b:页面顶部,lt:左上角,rt:右上角,lb:左下角,rb:右下角 winAlpha:弹出窗体拖动时的透明度,默认为0.8 //以下三个参数主要用于win方法(当然你也可以通过设定这些覆盖前面四个消息类型的默认属性)。 iframe:是否使用iframe方法加载内容,该属性如果为true或者object,组件则尝试将message内容作为url进行加载(如果属性值为一个object,则将object的内容添加为iframe的属性,如iframe:{id:'myId',name:'myName',src:'http://www.baidu.com'}则iframe的id为myId,name为myName,src为http://www.baidu.com)。默认为false。 icoCls:图标类型。传入的内容为className,具体写法可以参考ymprompt.css中对图标的定义方式。默认为空。 btn:按钮定义。传入的是数组形式。每个按钮的格式为['按钮文本','按钮标识'], 如[['确定','ok'],['取消','cancel'],['关闭','close']]等。 注意单个按钮应该是这样的:[['确定','ok']] closeBtn:是否显示关闭按钮,默认为true(显示)。 //以下参数可用于对组件语言本地化,如用于英文等系统中 okTxt:确定按钮的文本描述,默认为“确定” cancelTxt:取消按钮的文本描述,默认为“取消” closeTxt:关闭按钮的文本描述(鼠标放在关闭按钮上时显示),默认为“关闭” 5、操作接口: 属性: version:当前版本号 如:alert(ymPrompt.version) pubDate:当前版本的发布日期 如:alert(ymPrompt.pubDate); cfg:组件的当前的默认配置 方法: setDefaultCfg(cfg):设定组件的默认属性,设定后的所有弹出均默认采用cfg中的设置。 如:ymPrompt.setDefaultCfg({maskAlpha:0.2,maskAlphaColor:'#00f'}); //设定遮罩层颜色为蓝色,透明度0.2 getPage():在iframe窗口模式下,获取到iframe的dom对象。 如:alert(ymPrompt.getPage().contentWindow.document.body.outerHTML); //获取iframe页面的html内容 resizeWin(w,h):通过程序动态修改窗口的大小。参数:w:宽度,h:高度 如:ymPrompt.resizeWin(400,300); //修改弹出框宽度为400px,高度为300px doHandler(sign,autoClose):模拟触发某个按钮的点击事件。参数sign:传给回调函数的标识,autoClose:是否自动关闭窗口(默认采用全局配置) 如:ymPrompt.doHandler('ok',false); //触发确定按钮的点击事件,并且执行完回调函数后不关闭窗口 getButtons():获取当前弹出窗口的所有按钮对象,返回结果是一个对象集合(数组)。 如:var btnID=ymPrompt.getButtons()[0].id; //获取第一个按钮的id close():关闭当前弹出的窗口 如:ymPrompt.close() 6、其他说明:如果觉得“对象.方法”的调用方式比较麻烦,可以采用如下方式简化调用: 在调用之前设定var Alert=ymPrompt.alert。之后就可以使用Alert()的方式进行
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Go 旅城通票

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值