The code in the jQuery pop-up box encapsulates DialogHelper and jquerydialoghelper.
After reading the jQueryUI Dialog example, the result is pretty good. It's a bit awkward to use. The code written is a bit unscrewed and needs to be encapsulated! So we have the following simple DialogHelper helper class, because the focus of this article is the idea, so the current version of the code is still very rough. The train of thought is correct. We hope that this train of thought will give you some inspiration. At the same time, we welcome everyone to think and propose better suggestions for improvement.
Copy codeThe Code is as follows:
// Require ScrollHelper. js
Function DialogHelper (){
Var _ this = this;
Var doc = Invalid invalid doc ument;
_ 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 ("%", "")/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.doc umentElement. clientWidth;
Var clientHeight = document.doc umentElement. 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 = 'alog _ head'>"). appendTo (newDialog );
$ ("<Span class = 'headlabel'>" example .html (options. Action + "" + options. Title). appendTo (DialogHead );
Var DialogClose = $ ("<span class = 'dialogclose'>"). appendTo (DialogHead );
Var DialogBody = $ ("<div class = 'alog _ body'>" ).html (contentHtml). appendTo (newDialog );
Var DialogFoot = $ ("<div class = 'alog _ foot'>"). appendTo (newDialog );
Var newDiv = $ ("<div class = 'right'>"). appendTo (DialogFoot );
Var ActionCancelDiv = $ ("<div class = 'actionbuttoniner iner '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'>" cmd.html ("Cancel"). appendTo (newA );
Var ActionSaveDiv = $ ("<div class = 'actionbuttoniner iner 'title = 'save'>"). appendTo (newDiv );
Var newB = $ ("<div class = 'actionbutton ActionButtonAttention 'id = 'actionbuttonsav'>"). appendTo (ActionSaveDiv );
NewB. on ('click', function (){
If (typeof saveBtnClickHandler = "function "){
SaveBtnClickHandler ();
}
});
$ ("<Div class = 'icon save'>"). appendTo (newB );
$ ("<Div class = 'title IconTitle SaveButton '>" example .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 all the content shared in this article. I hope you will like it.