jQuery弹出框代码封装DialogHelper_jquery
程序员文章站
2022-04-01 13:30:33
...
看了jQueryUI Dialog的例子,效果还不错,就是用起来有点儿别扭,写出的代码有点拧巴,需要再封装一下!于是就有了下面这个简单的DialogHelper辅助类,因为这篇文章分享的重点是思路,所以目前版本的代码也还非常粗糙。思路对了,后续再封装成什么样都不过是形式而已,希望这个思路能给大家点启迪,同时欢迎大家开拓思维,提出更好的改进意见。
//require ScrollHelper.js
function DialogHelper() {
var _this = this;
var doc = window.document;
_this.maskDiv = null;
_this.contentDiv = null;
var options = {
opacity: 0.4
};
this.popup = function (contentdiv, optionArg) {
if (optionArg) {
for (var prop in optionArg) {
options[prop] = optionArg[prop];
}
}
_this.contentDiv = contentdiv || _this.contentDiv;
_this.maskDiv = $('
复制代码 代码如下:
//require ScrollHelper.js
function DialogHelper() {
var _this = this;
var doc = window.document;
_this.maskDiv = null;
_this.contentDiv = null;
var options = {
opacity: 0.4
};
this.popup = function (contentdiv, optionArg) {
if (optionArg) {
for (var prop in optionArg) {
options[prop] = optionArg[prop];
}
}
_this.contentDiv = contentdiv || _this.contentDiv;
_this.maskDiv = $('
');
_this.maskDiv.addClass('MaskDiv');
_this.maskDiv.css({
'filter': "Alpha(opacity=" + ( options.opacity - "0" ) * 100 + ");",
'opacity': options.opacity,
'display': 'block'
});
$(doc.body).append(_this.maskDiv);
if (_this.contentDiv) {
$(doc.body).append(_this.contentDiv);
_this.contentDiv.show();
_this.contentDiv.draggable({
containment: "document",
cursor: 'move',
handle: ".Dialog_Head"
});
$(_this.maskDiv).on("mousemove", function() {
$("body").preventScroll();
});
$(_this.maskDiv).on("mouseout", function() {
$("body").liveScroll();
});
if ($(".cke").length == 0 && $(".Dialog_Body").length > 0) {
$(".Dialog_Body").preventOuterScroll();
}
}
};
this.remove = function () {
if (_this.contentDiv) {
_this.contentDiv.remove();
}
if (_this.maskDiv) {
_this.maskDiv.remove();
}
$("body").liveScroll();
};
this.formatPercentNumber = function (value, whole) {
if (isNaN(value) && typeof value === "string") {
if (value.indexOf("%") !== -1) {
value = (value.replace("%", "") / 100) * whole;
} else if (value.indexOf("px") !== -1) {
value = value.replace("px", "");
}
}
return Math.ceil(value);
};
this.position = function (dialog, dialogBody, minusHeight) {
dialog = dialog || $(".ShowDialogDiv");
if (dialog[0]) {
var clientWidth = document.documentElement.clientWidth;
var clientHeight = document.documentElement.clientHeight;
var width = _this.formatPercentNumber(dialog.data("position").width, clientWidth) || dialog.width();
var height = _this.formatPercentNumber(dialog.data("position").height, clientHeight) || dialog.height();
width = width height = height $(dialog).css({
"width": width + "px",
"height": height + "px",
"top": Math.ceil((clientHeight - height) / 2) + "px",
"left": Math.ceil((clientWidth - width) / 2) + "px"
});
dialogBody = dialogBody || $(".Dialog_Body");
if (dialogBody[0]) {
minusHeight = minusHeight || ($(".Dialog_Head").outerHeight() + $(".Dialog_Foot").outerHeight());
var dialogBodyHeight = height - minusHeight;
dialogBody.height(dialogBodyHeight);
}
}
}
}
var createDialogTemplate = function (optionArg, contentHtml, saveBtnClickHandler) {
var options = {
"Action": "",
"Title": "",
"Width": "50%",
"Height": "50%"
};
if (optionArg) {
for (var prop in optionArg) {
options[prop] = optionArg[prop];
}
}
var newDialog = $("
_this.maskDiv.addClass('MaskDiv');
_this.maskDiv.css({
'filter': "Alpha(opacity=" + ( options.opacity - "0" ) * 100 + ");",
'opacity': options.opacity,
'display': 'block'
});
$(doc.body).append(_this.maskDiv);
if (_this.contentDiv) {
$(doc.body).append(_this.contentDiv);
_this.contentDiv.show();
_this.contentDiv.draggable({
containment: "document",
cursor: 'move',
handle: ".Dialog_Head"
});
$(_this.maskDiv).on("mousemove", function() {
$("body").preventScroll();
});
$(_this.maskDiv).on("mouseout", function() {
$("body").liveScroll();
});
if ($(".cke").length == 0 && $(".Dialog_Body").length > 0) {
$(".Dialog_Body").preventOuterScroll();
}
}
};
this.remove = function () {
if (_this.contentDiv) {
_this.contentDiv.remove();
}
if (_this.maskDiv) {
_this.maskDiv.remove();
}
$("body").liveScroll();
};
this.formatPercentNumber = function (value, whole) {
if (isNaN(value) && typeof value === "string") {
if (value.indexOf("%") !== -1) {
value = (value.replace("%", "") / 100) * whole;
} else if (value.indexOf("px") !== -1) {
value = value.replace("px", "");
}
}
return Math.ceil(value);
};
this.position = function (dialog, dialogBody, minusHeight) {
dialog = dialog || $(".ShowDialogDiv");
if (dialog[0]) {
var clientWidth = document.documentElement.clientWidth;
var clientHeight = document.documentElement.clientHeight;
var width = _this.formatPercentNumber(dialog.data("position").width, clientWidth) || dialog.width();
var height = _this.formatPercentNumber(dialog.data("position").height, clientHeight) || dialog.height();
width = width height = height $(dialog).css({
"width": width + "px",
"height": height + "px",
"top": Math.ceil((clientHeight - height) / 2) + "px",
"left": Math.ceil((clientWidth - width) / 2) + "px"
});
dialogBody = dialogBody || $(".Dialog_Body");
if (dialogBody[0]) {
minusHeight = minusHeight || ($(".Dialog_Head").outerHeight() + $(".Dialog_Foot").outerHeight());
var dialogBodyHeight = height - minusHeight;
dialogBody.height(dialogBodyHeight);
}
}
}
}
var createDialogTemplate = function (optionArg, contentHtml, saveBtnClickHandler) {
var options = {
"Action": "",
"Title": "",
"Width": "50%",
"Height": "50%"
};
if (optionArg) {
for (var prop in optionArg) {
options[prop] = optionArg[prop];
}
}
var newDialog = $("
");
var DialogHead = $("
var DialogHead = $("
").appendTo(newDialog);
$("").html(options.Action + " " + options.Title).appendTo(DialogHead);
var DialogClose = $("").appendTo(DialogHead);
var DialogBody = $("
$("").html(options.Action + " " + options.Title).appendTo(DialogHead);
var DialogClose = $("").appendTo(DialogHead);
var DialogBody = $("
").html(contentHtml).appendTo(newDialog);
var DialogFoot = $("
var DialogFoot = $("
").appendTo(newDialog);
var newDiv = $("
var newDiv = $("
").appendTo(DialogFoot);
var ActionCancelDiv = $("
var ActionCancelDiv = $("
").appendTo(newDiv);
DialogClose.on("click", function() {
dialogHelper.remove();
});
ActionCancelDiv.on("click", function() {
dialogHelper.remove();
});
var newA = $("
DialogClose.on("click", function() {
dialogHelper.remove();
});
ActionCancelDiv.on("click", function() {
dialogHelper.remove();
});
var newA = $("
").appendTo(ActionCancelDiv);
$("
$("
").appendTo(newA);
$("
$("
").html("Cancel").appendTo(newA);
var ActionSaveDiv = $("
var ActionSaveDiv = $("
").appendTo(newDiv);
var newB = $("
var newB = $("
").appendTo(ActionSaveDiv);
newB.on('click', function () {
if (typeof saveBtnClickHandler == "function") {
saveBtnClickHandler();
}
});
$("
newB.on('click', function () {
if (typeof saveBtnClickHandler == "function") {
saveBtnClickHandler();
}
});
$("
").appendTo(newB);
$("
$("
").html("Save").appendTo(newB);
var minusHeight = DialogHead.outerHeight() + DialogFoot.outerHeight();
newDialog.data("position", {
width: options.Width,
height: options.Height
});
dialogHelper.position(newDialog, DialogBody, minusHeight);
return newDialog;
};
var changeDialogLayout = function(optionArg, dialogObj) {
var options = {
"Width": "70%",
"Height": "90%"
};
if (optionArg) {
for (var prop in optionArg) {
options[prop] = optionArg[prop];
}
}
var DialogBody = $(dialogObj).find(".Dialog_Body");
var DialogHead = $(dialogObj).find(".Dialog_Head");
var DialogFoot = $(dialogObj).find(".Dialog_Foot");
var other = Math.round(DialogBody.css("padding-top").replace(/[a-z]/ig, "")) + Math.round(DialogBody.css("padding-bottom").replace(/[a-z]/ig, ""));
var minusHeight = DialogHead.outerHeight() + DialogFoot.outerHeight() + other;
dialogObj.data("position", {
width: options.Width,
height: options.Height
});
dialogHelper.position(dialogObj, DialogBody, minusHeight);
};
var minusHeight = DialogHead.outerHeight() + DialogFoot.outerHeight();
newDialog.data("position", {
width: options.Width,
height: options.Height
});
dialogHelper.position(newDialog, DialogBody, minusHeight);
return newDialog;
};
var changeDialogLayout = function(optionArg, dialogObj) {
var options = {
"Width": "70%",
"Height": "90%"
};
if (optionArg) {
for (var prop in optionArg) {
options[prop] = optionArg[prop];
}
}
var DialogBody = $(dialogObj).find(".Dialog_Body");
var DialogHead = $(dialogObj).find(".Dialog_Head");
var DialogFoot = $(dialogObj).find(".Dialog_Foot");
var other = Math.round(DialogBody.css("padding-top").replace(/[a-z]/ig, "")) + Math.round(DialogBody.css("padding-bottom").replace(/[a-z]/ig, ""));
var minusHeight = DialogHead.outerHeight() + DialogFoot.outerHeight() + other;
dialogObj.data("position", {
width: options.Width,
height: options.Height
});
dialogHelper.position(dialogObj, DialogBody, minusHeight);
};
以上就是本文所分享的全部内容了,希望大家能够喜欢。
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
相关文章
相关视频
专题推荐
-
独孤九贱-php全栈开发教程
全栈 170W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
-
玉女心经-web前端开发教程
入门 80W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
-
天龙八部-实战开发教程
实战 120W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习
- 最新文章
- 热门排行
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论