ModelDialog JavaScript模态对话框类代码


Posted in Javascript onApril 17, 2011

/**
* JavaScript ModelDialog v0.1
*
* new ModelDialog({
* caption 标题 '对话框标题'(默认)
* template 主体内容 ''(默认)
* dialogCls 对话框className 'md-dialog'(默认)
* headCls 头部className 'md-head'(默认)
* btnCloseCls 关闭按钮className 'md-close'(默认)
* bodyCls 主体className 'md-body'(默认)
* shadowBg 遮盖层背景色 'gray'(默认)
* shadowOpy 遮盖层透明的 0.2(默认)
* dragable 是否可拖拽 true(默认)
* dragInWin 是否仅在窗口内拖动 (true)默认 与area互斥
* area [minX,maxX,minY,maxY] 与dragInWin互斥
* });
*/

ModelDialog JavaScript模态对话框类代码


核心代码:
/** 
* JavaScript ModelDialog v0.4 
* Copyright (c) 2010 snandy 
* Blog: http://snandy.javaeye.com/ 
* QQ群: 34580561 
* Date: 2010-09-08 
* 
* 
* new ModelDialog({ 
* caption 标题 '对话框标题'(默认) 
* template 主体内容 ''(默认) 
* dialogCls 对话框className 'md-dialog'(默认) 
* headCls 头部className 'md-head'(默认) 
* btnCloseCls 关闭按钮className 'md-close'(默认) 
* bodyCls 主体className 'md-body'(默认) 
* shadowBg 遮盖层背景色 'gray'(默认) 
* shadowOpy 遮盖层透明的 0.2(默认) 
* dragable 是否可拖拽 true(默认) 
* dragInWin 是否仅在窗口内拖动 (true)默认 与area互斥 
* area [minX,maxX,minY,maxY] 与dragInWin互斥 
* }); 
*/ 
ModelDialog = 
function(){ 
var px = 'px'; 
var isIE = /msie/.test(navigator.userAgent.toLowerCase()); function getViewSize(){ 
return {w: window['innerWidth'] || document.documentElement.clientWidth, 
h: window['innerHeight'] || document.documentElement.clientHeight} 
} 
function getFullSize(){ 
var w = Math.max(document.documentElement.clientWidth ,document.body.clientWidth) + Math.max(document.documentElement.scrollLeft, document.body.scrollLeft); 
var h = Math.max(document.documentElement.clientHeight,document.body.clientHeight) + Math.max(document.documentElement.scrollTop, document.body.scrollTop); 
w = Math.max(document.documentElement.scrollWidth,w); 
h = Math.max(document.documentElement.scrollHeight,h); 
return {w:w,h:h}; 
} 
function $(tag){ 
return new $.prototype.init(tag); 
} 
$.prototype = { 
init : function(tag){ 
this[0] = document.createElement(tag); 
return this; 
}, 
setCls : function(cls){ 
this[0].className = cls; 
return this; 
}, 
setSty : function(name,val){ 
name=='opacity' ? 
isIE ? 
this[0].style.filter = 'Alpha(Opacity=' + val*100 + ')' : 
this[0].style.opacity = val : 
this[0].style[name] = val; 
return this; 
}, 
css : function(str){ 
this[0].style.cssText = str; 
return this; 
}, 
html : function(str){ 
this[0].innerHTML = str; 
return this; 
} 
} 
$.prototype.init.prototype = $.prototype; 
function ModelDialog(opt){ 
this.dialogCls = opt.dialogCls || 'md-dialog'; 
this.headCls = opt.headCls || 'md-head'; 
this.btnCloseCls = opt.btnCloseCls || 'md-close'; 
this.bodyCls = opt.bodyCls || 'md-body'; 
this.shadowBg = opt.shadowBg || 'gray'; 
this.shadowOpy = opt.shadowOpy || '0.2'; 
this.caption = opt.caption || "对话框标题"; 
this.template = opt.template || ''; 
this.dragable = opt.dragable != false; 
this.dragInWin = opt.dragInWin != false; 
this.area = opt.area; 
this.dialog = null; 
this.head = null; 
this.label = null; 
this.btnClose = null; 
this.body = null; 
this.shadow = null; 
this.init(); 
} 
ModelDialog.prototype = { 
init : function(){ 
var _this = this; 
this.dialog = $('div').setCls(this.dialogCls).css('position:absolute;z-index:100;')[0]; 
this.head = $('div').setCls(this.headCls)[0]; 
this.label = $('label').html(this.caption)[0]; 
this.btnClose = $('div').setCls(this.btnCloseCls)[0]; 
this.on(this.btnClose,'click',function(){ 
_this.onClose(); 
}); 
this.head.appendChild(this.label); 
this.head.appendChild(this.btnClose); 
this.body = $('div').setCls(this.bodyCls)[0]; 
this.setContent(this.template); 
this.dialog.appendChild(this.head); 
this.dialog.appendChild(this.body); 
this.createShadow(); 
document.body.appendChild(this.shadow); 
document.body.appendChild(this.dialog); 
this.moveToCenter(); 
// 计算拖拽范围 
// 标准模式下:clientWidth=width+padding;offsetWidth=width+padding+border 
if(this.dragable){ 
if(this.dragInWin){ 
var maxX = getViewSize().w - this.dialog.offsetWidth; 
var maxY = getViewSize().h - this.dialog.offsetHeight; 
this.dragdrop(this.dialog,{ 
bridge : this.head, 
area : [0,maxX,0,maxY] 
}); 
return; 
} 
if(this.area){ 
this.dragdrop(this.dialog,{ 
bridge : this.head, 
area : this.area 
}); 
return; 
} 
this.dragdrop(this.dialog,{ 
bridge : this.head 
}); 
} 
}, 
destroy : function(){ 
this.dialog = null; 
this.head = null; 
this.label = null; 
this.btnClose = null; 
this.body = null; 
this.shadow = null; 
}, 
createShadow : function(){ 
var str = 'position:absolute;left:0px;top:0px;z-index:1' + 
';width:' + getFullSize().w + px + 
';height:' + getFullSize().h + px + 
';background:' + this.shadowBg + 
';opacity:' + this.shadowOpy + 
';filter:Alpha(Opacity=' + this.shadowOpy*100 + ');'; 
var _this = this; 
this.shadow = $("div").setCls('md-shadow').css(str)[0]; 
this.on(window,'resize',function(){ 
_this.shadow.style.width = getFullSize().w + px; 
_this.shadow.style.height = getFullSize().h + px; 
_this.moveToCenter(); 
}); 
}, 
moveTo : function(x, y){ 
this.dialog.style.left = x + px; 
this.dialog.style.top = y + px; 
}, 
moveToCenter : function(){ 
var size = getViewSize(); 
var x = (size.w-50)/2 - (this.dialog.clientWidth-50)/2; 
var y = (size.h- 50)/2 - (this.dialog.clientHeight-50)/2 + document.documentElement.scrollTop; 
this.moveTo(x, y); 
}, 
setCaption : function(v){ 
this.caption = v; 
this.label.innerHTML = v; 
}, 
setContent : function(str){ 
this.template = str; 
this.body.innerHTML = str; 
}, 
onClose : function(){ 
document.body.removeChild(this.dialog); 
document.body.removeChild(this.shadow); 
if(this['onbi']){ 
this.onbi(); 
} 
this.destroy(); 
}, 
on : function(el, type, fn){ 
el.addEventListener ? 
el.addEventListener(type, fn, false) : 
el.attachEvent ? 
el.attachEvent("on" + type, fn) : 
el['on'+type] = fn; 
}, 
un : function(el,type,fn){ 
el.removeEventListener ? 
el.removeEventListener(type, fn, false) : 
el.detachEvent ? 
el.detachEvent("on" + type, fn) : 
el['on'+type] = null; 
}, 
dragdrop : function(){ 
return function(el,opt){ 
var _this=this, ele, diffX, diffY, dragX=true,dragY=true, minX, maxX, minY, maxY, bridge; 
ele = el; 
opt && opt.dragX===false && (dragX=false); 
opt && opt.dragY===false && (dragY=false); 
opt && opt.area && typeof opt.area[0]==='number' && (minX=opt.area[0]); 
opt && opt.area && typeof opt.area[1]==='number' && (maxX=opt.area[1]); 
opt && opt.area && typeof opt.area[2]==='number' && (minY=opt.area[2]); 
opt && opt.area && typeof opt.area[3]==='number' && (maxY=opt.area[3]); 
opt && opt.bridge && (bridge=opt.bridge); 
ele.style.position = 'absolute'; 
bridge ? 
this.on(bridge,'mousedown',mousedown) : 
this.on(ele,'mousedown',mousedown); 
function mousedown(e){ 
e = e || window.event; 
ele.style.cursor = 'pointer'; 
if(ele.setCapture){//IE 
_this.on(ele, "losecapture", mouseup); 
ele.setCapture(); 
e.cancelBubble = true; //IE 
}else if(window.captureEvents){//标准DOM 
e.stopPropagation(); 
_this.on(window, "blur", mouseup); 
e.preventDefault(); 
} 
_x = e.clientX; 
_y = e.clientY; 
diffX = e.clientX - ele.offsetLeft; 
diffY = e.clientY - ele.offsetTop; 
_this.on(document,'mousemove',mousemove); 
_this.on(document,'mouseup',mouseup); 
} 
function mousemove(e){ 
e = e || window.event; 
var moveX = e.clientX - diffX, 
moveY = e.clientY - diffY; 
moveX < minX && (moveX = minX); // left 最小值 
moveX > maxX && (moveX = maxX); // left 最大值 
moveY < minY && (moveY = minY); // top 最小值 
moveY > maxY && (moveY = maxY); // top 最大值 
dragX && (ele.style.left = moveX + 'px'); 
dragY && (ele.style.top = moveY + 'px'); 
} 
function mouseup(e){ 
ele.style.cursor = 'default'; 
_this.un(document,'mousemove',mousemove); 
_this.un(document,'mouseup',mouseup); 
if(ele.releaseCapture){//IE 
_this.un(ele, "losecapture", mouseup); 
ele.releaseCapture(); 
} 
if(window.releaseEvents){//标准DOM 
_this.un(window, "blur", mouseup); 
} 
} 
} 
}() 
} 
return ModelDialog; 
}();

演示地址 http://demo.3water.com/js/2011/ModelDialog/index.html
打包下载地址 https://3water.com/jiaoben/35245.html
Javascript 相关文章推荐
在你的网页中嵌入外部网页的方法
Apr 02 Javascript
javascript实现的鼠标链接提示效果生成器代码
Jun 28 Javascript
jquery 查找select ,并触发事件的实现代码
Mar 30 Javascript
JS/jQuery实现默认显示部分文字点击按钮显示全部内容
May 13 Javascript
JQuery限制复选框checkbox可选中个数的方法
Apr 20 Javascript
jQuery实现鼠标选文字发新浪微博的方法
Apr 02 Javascript
javaScript知识点总结(必看篇)
Jun 10 Javascript
微信小程序  TLS 版本必须大于等于1.2问题解决
Feb 22 Javascript
使用 Node.js 开发资讯爬虫流程
Jan 07 Javascript
详解Vue 动态组件与全局事件绑定总结
Nov 11 Javascript
javascript实现前端input密码输入强度验证
Jun 24 Javascript
Openlayers3实现车辆轨迹回放功能
Sep 29 Javascript
JavaScript中的isXX系列是否继续使用的分析
Apr 16 #Javascript
EXTJS FORM HIDDEN TEXTFIELD 赋值 使用value不好用的问题
Apr 16 #Javascript
表单JS弹出填写提示效果代码
Apr 16 #Javascript
纯JAVASCRIPT图表动画插件Highcharts Examples
Apr 16 #Javascript
分享10篇优秀的jQuery幻灯片制作教程及应用案例
Apr 16 #Javascript
javascript 弹出窗口中是否显示地址栏的实现代码
Apr 14 #Javascript
js网页侧边随页面滚动广告效果实现
Apr 14 #Javascript
You might like
php数组中删除元素的实现代码
2012/06/22 PHP
php导出csv数据在浏览器中输出提供下载或保存到文件的示例
2014/04/24 PHP
php筛选不存在的图片资源
2015/04/28 PHP
PHP实现cookie跨域session共享的方法分析
2019/08/23 PHP
javascript整除实现代码
2010/11/23 Javascript
javascript实现分栏显示小技巧附图
2014/10/13 Javascript
node.js中的buffer.copy方法使用说明
2014/12/14 Javascript
javascript实现给定半径求出圆的面积
2015/06/26 Javascript
js滚动条平滑移动示例代码
2016/03/29 Javascript
JavaScript_ECMA5数组新特性详解
2016/06/12 Javascript
浅谈JS中的bind方法与函数柯里化
2016/08/10 Javascript
jQuery实现全选、反选和不选功能
2017/08/16 jQuery
浅谈Vue.js 组件中的v-on绑定自定义事件理解
2017/11/17 Javascript
weebox弹出窗口不居中显示的解决方法
2017/11/27 Javascript
基于Node.js实现压缩和解压缩的方法
2018/02/13 Javascript
新版vue-cli模板下本地开发环境使用node服务器跨域的方法
2018/04/03 Javascript
浅谈vue项目如何打包扔向服务器
2018/05/08 Javascript
深入理解Vue 组件之间传值
2018/08/16 Javascript
JavaScript模拟实现自由落体效果
2018/08/28 Javascript
详谈js的变量提升以及使用方法
2018/10/06 Javascript
JavaScript数据结构与算法之二叉树遍历算法详解【先序、中序、后序】
2019/02/21 Javascript
javascript设计模式 ? 中介者模式原理与用法实例分析
2020/04/20 Javascript
jQuery弹框插件使用方法详解
2020/05/26 jQuery
Python语言实现获取主机名根据端口杀死进程
2016/03/31 Python
python 读写文件,按行修改文件的方法
2018/07/12 Python
详解Python 装饰器执行顺序迷思
2018/08/08 Python
Python实现的微信支付方式总结【三种方式】
2019/04/13 Python
pytorch 获取层权重,对特定层注入hook, 提取中间层输出的方法
2019/08/17 Python
快速解决docker-py api版本不兼容的问题
2019/08/30 Python
python GUI库图形界面开发之PyQt5打印控件QPrinter详细使用方法与实例
2020/02/28 Python
Python自定义聚合函数merge与transform区别详解
2020/05/26 Python
Python Selenium模块安装使用教程详解
2020/07/09 Python
html5实现滑块功能之type=&quot;range&quot;属性
2020/02/18 HTML / CSS
食品安全标语
2014/06/07 职场文书
教师一帮一活动总结
2014/07/08 职场文书
社区党的群众路线教育实践活动领导班子对照检查材料
2014/09/25 职场文书