封装的原生javascript弹出层代码


Posted in Javascript onSeptember 24, 2010
<script type="text/javascript">// <![CDATA[ 
/* @author: hongru.chen 
** @date: 2010-09-15 
** @vision: 1.1 
*/ 
var Hongru = {}; 
function $(id){return document.getElementById(id)} 
Object.prototype.extend = function(target, /*optional*/source, /*optional*/deep) { 
target = target || {}; 
var sType = typeof source, i = 1, options; 
if( sType === 'undefined' || sType === 'boolean' ) { 
deep = sType === 'boolean' ? source : false; 
source = target; 
target = this; 
} 
if( typeof source !== 'object' && Object.prototype.toString.call(source).call(source) !== '[object Function]' ) 
source = {}; while(i <= 2) { 
options = i === 1 ? target : source; 
if( options != null ) { 
for( var name in options ) { 
var src = target[name], copy = options[name]; 
if(target === copy) 
continue; 
if(deep && copy && typeof copy === 'object' && !copy.nodeType) 
target[name] = this.extend(src || 
(copy.length != null ? [] : {}), copy, deep); 
else if(copy !== undefined) 
target[name] = copy; 
} 
} 
i++; 
} 
return target; 
}; 
isFunction = function( fn ) { 
return !!fn && typeof fn != "string" && !fn.nodeName && 
fn.constructor != Array && /^[\s[]?function/.test( fn + "" ); 
} 
Hongru.box = function(){ 
var box,mask,content,_content,_height,_width,isPreload,flag = false; 
return{ 
open:function(con,options){ 
//default options 
var defaultOptions = { 
width:300, 
height:200, 
isPre:true, 
time:0, 
title:'对话框', 
isBar:true, 
isShut:true 
}; 
options = options?options:{}; 
options = Object.extend(defaultOptions,options); 
if(!flag){ 
box = document.createElement('div');box.id = "popup-box"; 
box.style.cssText = "position:absolute; display:none; background:#fff url(preload.gif) no-repeat 50% 50%; border:5px solid #ccc; z-index:2000"; 
mask = document.createElement('div');mask.id = "popup-mask"; 
mask.style.cssText = "position:absolute; display:none; top:0; left:0; height:100%; width:100%; background:#000; z-index:1500"; 
content = document.createElement('div');content.id = "popup-content"; 
content.style.cssText = "background:#fff"; 
bar = document.createElement('div'); bar.id = "popup-bar"; 
bar.style.cssText = "background:none repeat scroll 0 0 #F7F7F7;border-bottom:4px solid #EEEEEE;border-top:3px solid #F9F9F9;margin-top:2px;position:relative"; 
wrapTit = document.createElement('div'); wrapTit.id = "wrap-tit"; 
wrapTit.style.cssText = "background:none repeat scroll 0 0 #F3F3F3;border-bottom:5px solid #F1F1F1;border-top:4px solid #F5F5F5;line-height:5px;margin-top:3px;"; 
tit = document.createElement('span'); tit.id = "popup-tit"; 
tit.style.cssText = "cursor:text;margin-left:10px;position:relative;color:#333;font-size:84%" 
shut = document.createElement('a'); shut.id = "popup-shut"; 
shut.innerHTML = '×'; 
shut.style.cssText = "color:#34538B;cursor:pointer;font-family:Tahoma;font-weight:bold;position:absolute;top:0px;right:10px;text-decoration:none;"; 
document.body.appendChild(mask); document.body.appendChild(box); box.appendChild(bar); box.appendChild(content); bar.appendChild(wrapTit); bar.appendChild(shut); wrapTit.appendChild(tit); 
mask.onclick = shut.onclick = Hongru.box.hide; 
//bar.onclick = function(){alert($('sure').id)} 
window.onresize = Hongru.box.resize; 
flag = true; 
}tit.innerHTML = options.title; 
options.isShut?shut.style.display = '':shut.style.display = 'none'; 
options.isBar?bar.style.display = '':bar.style.display = 'none'; 
if(!options.isPre){ 
box.style.width = options.width?options.width+'px':'auto'; 
box.style.height = options.height?options.height+'px':'auto'; 
box.style.backgroundImage = 'none'; 
content.innerHTML = con; 
} 
else{ 
content.style.display = 'none'; 
box.style.width = box.style.height = '100px'; 
} 
this.mask(); 
this.alpha(mask,1,50); 
_content = con; _height = options.height; _width = options.width; isPreload = options.isPre; 
if(options.time){ 
setTimeout(function(){Hongru.box.hide()},1000*options.time); 
} 
}, 
fill:function(con,options){ 
if(options.isPre){ 
if(!options.width || !options.height){ 
var autoWidth = box.style.width, autoHeight = box.style.height; 
content.innerHTML = con; 
box.style.width = options.width?options.width+'px':''; 
box.style.height = options.height?options.height+'px':''; 
content.style.display = ''; 
options.width = parseInt(box.offsetWidth-10); 
options.height = parseInt(box.offsetHeight-10); 
content.style.display = 'none'; 
box.style.width = autoWidth; 
box.style.height = autoHeight; 
} 
else{ 
content.innerHTML = con; 
} 
this.size(box,options.width,options.height); 
} 
else{ 
box.style.backgroundImage = 'none'; 
} 
}, 
hide:function(){ 
Hongru.box.alpha(box,-1,0); 
}, 
resize:function(){ 
Hongru.box.pos(); 
Hongru.box.mask(); 
}, 
mask:function(){ 
mask.style.height = Hongru.page.total(1)+'px'; 
mask.style.width=''; mask.style.width = Hongru.page.total(0)+'px'; 
}, 
pos:function(){ 
var minTop = (Hongru.page.height()/2)-(box.offsetHeight/2); minTop = minTop<10?10:minTop; 
box.style.top=(minTop+Hongru.page.top())+'px'; 
box.style.left=(Hongru.page.width()/2)-(box.offsetWidth/2)+'px'; 
}, 
alpha:function(obj,direction,destination){ 
clearInterval(obj.animing); 
if(direction == 1){ 
obj.style.opacity=0; obj.style.filter='alpha(opacity=0)'; 
obj.style.display='block'; this.pos(); 
} 
obj.animing = setInterval(function(){Hongru.box.alphaAnim(obj,destination,direction)},50); 
}, 
alphaAnim:function(obj,destination,direction){ 
var opacity = Math.round(obj.style.opacity*100); 
if(opacity == destination){ 
clearInterval(obj.animing); 
if(direction == -1){ 
obj.style.display='none'; 
obj == box?Hongru.box.alpha(mask,-1,0):content.innerHTML=box.style.backgroundImage=''; 
}else{ 
curOptions = {width:_width,height:_height,isPre:isPreload} 
obj == mask?this.alpha(box,1,100):Hongru.box.fill(_content,curOptions); 
} 
}else{ 
var n=Math.ceil((opacity+((destination-opacity)*.5))); n=n==1?0:n; 
obj.style.opacity=n/100; 
obj.style.filter='alpha(opacity='+n+')'; 
} 
}, 
size:function(obj,width,height){ 
obj = typeof obj == 'object' ? obj : $(obj); 
clearInterval(obj.sizing); 
var offsetW = obj.offsetWidth, offsetH = obj.offsetHeight, 
otherW = offsetW-parseInt(obj.style.width), otherH = offsetH-parseInt(obj.style.height); 
var wFlag = (offsetW-otherW>width)?0:1, hFlag = (offsetH-otherH>height)?0:1; 
obj.sizing = setInterval(function(){Hongru.box.sizeAnim(obj,width,otherW,wFlag,height,otherH,hFlag)},20); 
}, 
sizeAnim:function(obj,width,otherW,wFlag,height,otherH,hFlag){ 
var objW = obj.offsetWidth-otherW, objH = obj.offsetHeight-otherH; 
if(objW == width && objH == height){ 
clearInterval(obj.sizing); 
box.style.backgroundImage='none'; 
content.style.display='block'; 
}else{ 
if(objW!=width){ 
var n = objW+((width-objW)*.5); 
obj.style.width = wFlag?Math.ceil(n)+'px':Math.floor(n)+'px'; 
} 
if(objH!=height){ 
var n = objH+((height-objH)*.5); 
obj.style.height = hFlag?Math.ceil(n)+'px':Math.floor(n)+'px'; 
} 
this.pos(); 
} 
}, 
ask:function(message,options,sureCall,cancelCall){ 
var elements = '<div class="wrap-remind" style="text-align:center">'+message+'<p><button id="sure-btn" class="sure-btn">确认</button>  <button id="cancel-btn" class="cancel-btn">取消</button></p></div>'; 
Hongru.box.open(elements,options); 
function delay(){//回调 
if(($('sure-btn') && $('cancel-btn')) != null){ 
clearInterval(checkComplete); 
//alert('yes'); 
$('sure-btn').onclick = function(){ 
if(isFunction(sureCall)){sureCall.call(this);} 
} 
$('cancel-btn').onclick = function(){ 
if(isFunction(cancelCall)){cancelCall.call(this);} 
Hongru.box.hide(); 
} 
}} 
var checkComplete = setInterval(delay,100); 
} 
} 
}(); 
Hongru.page=function(){ 
return{ 
top:function(){return document.documentElement.scrollTop||document.body.scrollTop}, 
width:function(){return self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth}, 
height:function(){return self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}, 
total:function(d){ 
var b=document.body, e=document.documentElement; 
return d?Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight)): 
Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth)) 
} 
} 
}(); 
// ]]></script>

不设任何options参数的box(默认高200px,宽300px)
普通设定高宽的box
高宽自适应内容高宽的box
不带预载动画的box
延迟自动消失的box
title自定义的box
不带标题栏的的box
不带标题栏关闭按钮的box
带按钮和回调参数的box

调用 Hongru.box.open(con,options)函数,参数con为弹出层主要内容,可以是html代码。options为一个object,可选参数目前有

{ 
width:300 //自定义,弹出层主体(除边框和标题栏的宽度),为0时自适应。 
height:200//自定义,弹出层主体(除边框和标题栏的高度),为0时自适应。 
isPre:1 //true or false,是否有预加载背景图, 
time:0 //延迟自动关闭时间,秒为单位,0时不触发 
title:'' //自定义title 
isBar:1 //true or false,是否有标题栏 
isShut:1 //是否有关闭按钮 
}

另 Hongru.box.ask(con,options,surecall,cancelcall)为带按钮的提示框,带回调函数前两个参数同上,后两个参数分别为‘确认'和‘取消'的回调函数
其中加入了透明度渐变和大小渐变,透明度渐变主体函数如下:
show sourceview sourceprint?alphaAnim:function(obj,destination,direction){ 
var opacity = Math.round(obj.style.opacity*100); 
if(opacity == destination){ 
clearInterval(obj.animing); 
if(direction == -1){ 
obj.style.display='none'; 
obj == box?Hongru.box.alpha(mask,-1,0):content.innerHTML=box.style.backgroundImage=''; 
}else{ 
curOptions = {width:_width,height:_height,isPre:isPreload} 
obj == mask?this.alpha(box,1,100):Hongru.box.fill(_content,curOptions); 
} 
}else{ 
var n=Math.ceil((opacity+((destination-opacity)*.5))); n=n==1?0:n; 
obj.style.opacity=n/100; 
obj.style.filter='alpha(opacity='+n+')'; 
} 
},

box大小渐变函数体如下:
show sourceview sourceprint?sizeAnim:function(obj,width,otherW,wFlag,height,otherH,hFlag){ 
var objW = obj.offsetWidth-otherW, objH = obj.offsetHeight-otherH; 
if(objW == width && objH == height){ 
clearInterval(obj.sizing); 
box.style.backgroundImage='none'; 
content.style.display='block'; 
}else{ 
if(objW!=width){ 
var n = objW+((width-objW)*.5); 
obj.style.width = wFlag?Math.ceil(n)+'px':Math.floor(n)+'px'; 
} 
if(objH!=height){ 
var n = objH+((height-objH)*.5); 
obj.style.height = hFlag?Math.ceil(n)+'px':Math.floor(n)+'px'; 
} 
this.pos(); 
} 
},

好了,废话不多说了,附上源文件打包下载:狠狠点击这里
如果觉得不错,请移驾点下 下面 的推荐
Javascript 相关文章推荐
用函数式编程技术编写优美的 JavaScript
Nov 25 Javascript
web的各种前端打印方法之jquery打印插件PrintArea实现网页打印
Jan 09 Javascript
jquery 页面滚动到底部自动加载插件集合
Jan 31 Javascript
将form表单中的元素转换成对象的方法适用表单提交
May 02 Javascript
使用javascript实现Iframe自适应高度
Dec 24 Javascript
使用javascript插入样式
Mar 14 Javascript
原生js实现秒表计时器功能
Feb 16 Javascript
JS实现延迟隐藏功能的方法(类似QQ头像鼠标放上展示信息)
Dec 28 Javascript
JavaScript求一组数的最小公倍数和最大公约数常用算法详解【面向对象,回归迭代和循环】
May 07 Javascript
javascript中关于类型判断的一些疑惑小结
Oct 14 Javascript
使用JavaScript实现网页秒表功能(含开始、暂停、继续、重置功能)
Jun 05 Javascript
微信小程序之滑动页面隐藏和显示组件功能的实现代码
Jun 19 Javascript
js最简单的拖拽效果实现代码
Sep 24 #Javascript
JavaScript 设计模式 安全沙箱模式
Sep 24 #Javascript
IE无法设置短域名下Cookie
Sep 23 #Javascript
Javascript中获取出错代码所在文件及行数的代码
Sep 23 #Javascript
基于JQuery的一个简单的鼠标跟随提示效果
Sep 23 #Javascript
用js模拟JQuery的show与hide动画函数代码
Sep 20 #Javascript
通过DOM脚本去设置样式信息
Sep 19 #Javascript
You might like
PHP+Tidy-完美的XHTML纠错+过滤
2007/04/10 PHP
pdo中使用参数化查询sql
2011/08/11 PHP
学习php设计模式 php实现备忘录模式(Memento)
2015/12/09 PHP
解决PHP上传非标准格式的图片pjpeg失败的方法
2017/03/12 PHP
php使用pthreads v3多线程实现抓取新浪新闻信息操作示例
2020/02/21 PHP
javascript定义函数的方法
2010/12/06 Javascript
解决window.opener=null;window.close(),只支持IE6不支持IE7,IE8的问题
2014/01/14 Javascript
Jquery使用val方法读写value值
2015/05/18 Javascript
学习使用AngularJS文件上传控件
2016/02/16 Javascript
详解jQuery中的empty、remove和detach
2016/04/11 Javascript
微信JS-SDK坐标位置如何转换为百度地图坐标
2016/07/04 Javascript
JS中位置与大小的获取方法
2016/11/22 Javascript
ES6中Symbol类型用法实例详解
2017/04/06 Javascript
angularJS 发起$http.post和$http.get请求的实现方法
2017/05/18 Javascript
vue element项目引入icon图标的方法
2018/06/06 Javascript
React中嵌套组件与被嵌套组件的通信过程
2018/07/11 Javascript
深入了解JavaScript代码覆盖
2019/06/13 Javascript
vue 地区选择器v-distpicker的常用功能
2019/07/23 Javascript
vue实现Input输入框模糊查询方法
2021/01/29 Javascript
微信小程序实现点击图片放大预览
2019/10/21 Javascript
vue 解决数组赋值无法渲染在页面的问题
2019/10/28 Javascript
nodejs实现百度舆情接口应用示例
2020/02/07 NodeJs
tensorflow建立一个简单的神经网络的方法
2018/02/10 Python
Python操作MySQL模拟银行转账
2018/03/12 Python
python实现维吉尼亚加密法
2019/03/20 Python
python设置表格边框的具体方法
2020/07/17 Python
Python3.9最新版下载与安装图文教程详解(Windows系统为例)
2020/11/28 Python
Python 排序最长英文单词链(列表中前一个单词末字母是下一个单词的首字母)
2020/12/14 Python
Python操作PostgreSql数据库的方法(基本的增删改查)
2020/12/29 Python
英国时尚首饰品牌:Missoma
2020/06/29 全球购物
管理科学大学生求职信
2013/11/13 职场文书
2014年三万活动总结
2014/04/26 职场文书
经营理念口号
2014/06/21 职场文书
技术员个人工作总结
2015/03/03 职场文书
宿舍卫生管理制度
2015/08/05 职场文书
nginx对http请求处理的各个阶段详析
2021/03/31 Servers