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 相关文章推荐
jquery 跨域访问问题解决方法(笔记)
Jun 08 Javascript
简单易用的倒计时js代码
Aug 04 Javascript
Vue监听数据对象变化源码
Mar 09 Javascript
基于Vue的文字跑马灯组件(npm 组件包)
May 24 Javascript
JavaScript 通过Ajax 动态加载CheckBox复选框
Aug 31 Javascript
vue slots 组件的组合/分发实例
Sep 06 Javascript
vue-cli项目修改文件热重载失效的解决方法
Sep 19 Javascript
JS实现求5的阶乘示例
Jan 21 Javascript
原生js代码能实现call和bind吗
Jul 31 Javascript
Vue实现将数据库中带html标签的内容输出(原始HTML(Raw HTML))
Oct 28 Javascript
VUE+elementui面包屑实现动态路由详解
Nov 04 Javascript
Node.js API详解之 assert模块用法实例分析
May 26 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面向对象全攻略 (六)__set() __get() __isset() __unset()的用法
2009/09/30 PHP
php获取用户浏览器版本的方法
2015/01/03 PHP
php版微信自动登录并获取昵称的方法
2016/09/23 PHP
php把文件设置为插件的技巧方法
2020/02/03 PHP
让广告代码不再影响你的网页加载速度
2006/07/07 Javascript
javascript学习网址备忘
2007/05/29 Javascript
教你如何解密js/vbs/vbscript加密的编码异处理小结
2008/06/25 Javascript
JavaScript Event学习第十章 一些可替换的事件对
2010/02/10 Javascript
js中parseFloat(参数1,参数2)定义和用法及注意事项
2013/01/27 Javascript
解析window.open的使用方法总结
2013/06/19 Javascript
jQuery动态添加
2016/04/07 Javascript
jquery实现跳到底部,回到顶部效果的简单实例(类似锚)
2016/07/10 Javascript
js实现颜色阶梯渐变效果(Gradient算法)
2017/03/21 Javascript
Node.js利用js-xlsx处理Excel文件的方法详解
2017/07/05 Javascript
jquery更改元素属性attr()方法操作示例
2020/05/22 jQuery
[00:52]DOTA2齐天大圣预告片
2016/08/13 DOTA
python网络编程学习笔记(三):socket网络服务器
2014/06/09 Python
详解Python中dict与set的使用
2015/08/10 Python
通过实例浅析Python对比C语言的编程思想差异
2015/08/30 Python
Python如何判断数独是否合法
2016/09/08 Python
python学习--使用QQ邮箱发送邮件代码实例
2019/04/16 Python
Python的互斥锁与信号量详解
2019/09/12 Python
使用Pyhton集合set()实现成果查漏的例子
2019/11/24 Python
Python流程控制语句的深入讲解
2020/06/15 Python
关于 HTML5 的七个传说小结
2012/04/12 HTML / CSS
HTML5到底会有什么发展?HTML5的前景展望
2015/07/07 HTML / CSS
Omio波兰:全欧洲低价大巴、火车和航班搜索和比价
2018/02/16 全球购物
即时搜索数百万张门票:SeatsForEveryone.com
2018/08/26 全球购物
澳大利亚墨尔本的在线时装店:LORETA
2018/09/14 全球购物
写给女朋友的道歉信
2014/01/08 职场文书
邮政员工辞职信
2014/01/16 职场文书
劳动竞赛活动总结
2014/05/05 职场文书
科级干部群众路线教育实践活动个人对照检查材料
2014/09/19 职场文书
幼儿教师自我剖析材料
2014/09/29 职场文书
2016年社区文体活动总结
2016/04/06 职场文书
Python开发简易五子棋小游戏
2022/05/02 Python