Look at the example of jQueryUI dialog, the effect is good, is to use up a bit awkward, write code a bit twisted bar, need to be packaged again! So there is the following simple Dialoghelper helper class, because this article shares the focus is the idea, so the current version of the code is also very rough. The idea is, the follow-up package into what is just a form, hope that this idea can give you some enlightenment, while welcome to open up thinking, and put forward better ideas for improvement.
Copy Code code as follows:
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 = $ (' <div> ');
_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 ("%", "")/MB) * 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 < 300? 300:width;
Height = height < 450? 450: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 = $ ("<div class= ' showdialogdiv ' id= ' dialog_" + options.) Title + "' >");
var Dialoghead = $ ("<div class= ' Dialog_head ' >"). Appendto (NewDialog);
$ ("<span class= ' Headlabel ' >"). html (options. Action + "" + options. Title). Appendto (Dialoghead);
var dialogclose = $ ("<span class= ' Dialogclose ' >"). Appendto (Dialoghead);
var dialogbody = $ ("<div class= ' dialog_body ' >"). html (contenthtml). Appendto (NewDialog);
var dialogfoot = $ ("<div class= ' Dialog_foot ' >"). Appendto (NewDialog);
var newdiv = $ ("<div class= ' right ' >"). Appendto (Dialogfoot);
var Actioncanceldiv = $ ("<div class= ' actionbuttoncontainer ' title= ' Cancel ' >"). Appendto (Newdiv);
Dialogclose.on ("click", Function () {
Dialoghelper.remove ();
});
Actioncanceldiv.on ("click", Function () {
Dialoghelper.remove ();
});
var Newa = $ ("<div class= ' Actionbutton ' id= ' Actionbuttoncancel ' >"). Appendto (Actioncanceldiv);
$ ("<div class= ' Icon Cancel ' >"). Appendto (Newa);
$ ("<div class= ' Title icontitle ' >"). html ("Cancel"). Appendto (Newa);
var Actionsavediv = $ ("<div class= ' actionbuttoncontainer ' title= ' Save ' >"). Appendto (Newdiv);
var newb = $ ("<div class= ' Actionbutton actionbuttonattention ' id= ' Actionbuttonsave ' >"). Appendto (ACTIONSAVEDIV) ;
Newb.on (' click ', function () {
if (typeof Savebtnclickhandler = = "function") {
Savebtnclickhandler ();
}
});
$ ("<div class= ' Icon Save ' >"). Appendto (NEWB);
$ ("<div class= ' Title icontitle Savebutton ' >"). 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);
};
The above is the entire content that this article shares, hope everybody can like.