模態對話框是網頁應用中常見的互動元件。以下是一個名為 ModelDialog 的 JavaScript 類別,支援自訂標題、內容、樣式、遮罩層、拖曳與範圍限制等功能,並附完整核心程式碼。
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 是否僅能在視窗內拖動(與 area 互斥);area 為 [minX, maxX, minY, maxY] 的拖曳範圍(與 dragInWin 互斥)。

/**
* JavaScript ModelDialog v0.4
* Copyright (c) 2010 snandy
* Blog: https://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();
// 計算拖拽範圍
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;
}else if(window.captureEvents){
e.stopPropagation();
_this.on(window, "blur", mouseup);
e.preventDefault();
}
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);
moveX > maxX && (moveX = maxX);
moveY < minY && (moveY = minY);
moveY > maxY && (moveY = maxY);
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){
_this.un(ele, "losecapture", mouseup);
ele.releaseCapture();
}
if(window.releaseEvents){
_this.un(window, "blur", mouseup);
}
}
}
}()
}
return ModelDialog;
}();
演示地址 https://demo.jb51.net/js/2011/ModelDialog/index.html
打包下載地址 https://www.jb51.net/jiaoben/35245.html