The code in the jQuery pop-up box encapsulates DialogHelper_jquery.

Source: Internet
Author: User
This article mainly introduces how to encapsulate DialogHelper in the jQuery pop-up box code. If you need it, you can refer to the example of jQueryUI Dialog. The effect is good, but it is a bit awkward to use, the Code 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.

The 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 (contentp, optionArg ){
If (optionArg ){
For (var prop in optionArg ){
Options [prop] = optionArg [prop];
}
}
_ This. contentDiv = contentp | _ 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.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 = $ ("

");
Var DialogHead = $ ("

"). AppendTo (newDialog );
$ ("" Pai.html (options. Action + "" + options. Title). appendTo (DialogHead );
Var DialogClose = $ (""). appendTo (DialogHead );
Var DialogBody = $ ("

"Pai.html (contentHtml). appendTo (newDialog );
Var DialogFoot = $ ("

"). AppendTo (newDialog );
Var newDiv = $ ("

"). AppendTo (DialogFoot );
Var ActionCancelDiv = $ ("

"). AppendTo (newDiv );
DialogClose. on ("click", function (){
DialogHelper. remove ();
});
ActionCancelDiv. on ("click", function (){
DialogHelper. remove ();
});
Var newA = $ ("

"). AppendTo (ActionCancelDiv );
$ ("

"). AppendTo (newA );
$ ("

"Cmd.html (" Cancel "). appendTo (newA );
Var ActionSaveDiv = $ ("

"). AppendTo (newDiv );
Var newB = $ ("

"). AppendTo (ActionSaveDiv );
NewB. on ('click', function (){
If (typeof saveBtnClickHandler = "function "){
SaveBtnClickHandler ();
}
});
$ ("

"). AppendTo (newB );
$ ("

"Maid (" 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.