Some common pop-up window, drag-and-drop, asynchronous file upload, and other practical code _ javascript skills

Source: Internet
Author: User
Today, I want to write something I encountered at work [pop-up window] [drag-and-drop] [asynchronous file upload]. Everyone can learn and make progress together for a long time without technical articles, I forget that I am a programmer ...... today, I want to write a little bit about what I met at work. Everyone is learning together, and it is quite simple.

Pop-up windowDuring our work, we often encounter pop-up window applications, and sometimes we need a bit of coverage layer:

In fact, this type of corner pop-up box is still very widely used, and it can be very easy to use CSS3. However, considering browser compatibility problems, this type of corner pop-up box still needs to be implemented using images.

The main code is as follows::

The Code is as follows:


// Pop-up layer play
Function popup (popupName ){
Var _ scrollHeight = $ (document). scrollTop (); // obtain the height of the current window from the top of the page.
_ Optional wheight = $ (window). height (); // get the current window height
_ Required wwidth = $ (window). width (); // obtain the current window width.
_ PopupHeight = popupName. height (); // obtain the height of the pop-up layer
_ PopupWeight = popupName. width (); // obtain the pop-up layer width.
// _ PosiTop = (_ javaswheight-_ popupHeight)/2 + _ scrollHeight-50;
_ PosiTop = _ scrollHeight + 120;
_ PosiLeft = (_ Your wwidth-_ popupWeight)/2;
PopupName.css ({"left": _ posiLeft + "px", "top": _ posiTop + "px", "display": "block"}); // set position
}
Function dragFunc (dragDiv, dragBody ){
If (dragDiv [0] & dragBody [0]) {
Var dragAble = false;
Var x1 = 0;
Var y1 = 0;
Var l = 0;
Var t = 0;
Var pOffset = dragBody. offset ();
DragDiv. mousedown (function (e ){
Var ss = this;
// Var rootId =
DragDiv.css ("cursor", "move ");
DragAble = true;
// The distance between the current mouse and the p border
// The current mouse coordinate, minus the pixel on the left of p
L = parseInt(dragBody.css ("left "));
T = parseInt(dragBody.css ("top "));
X1 = e. clientX-l;
Y1 = e. clientY-t;
X1 = x1> 0? X1: 0;
Y1 = y1> 0? Y1: 0;
This. setCapture & this. setCapture ();
});
DragDiv. mousemove (function (e ){
If (! DragAble)
Return;
// Coordinates on the left of the current p
// The current mouse coordinate, minus the amount of mouse dragging
Var x2 = 0;
Var y2 = 0;
// You need to consider the scroll bar problem !!!
Var top = $ (document). scrollTop ()? $ (Document). scrollTop ()-15: 0;
Var left = $ (document). scrollLeft ()? $ (Document). scrollLeft ()-15: 0;
X2 = e. clientX-x1 + left;
Y2 = e. clientY-y1 + top;
X2 = x2> 0? X2: 0;
Y2 = y2> 0? Y2: 0;
// Move a certain number of objects
If (Math. abs (l-x2)> 10 | Math. abs (t-y2)> 10 ){
DragBody.css ("left", x2 + "px ");
DragBody.css ("top", y2 + "px ");
}
});
DragDiv. mouseup (function (event ){
If (! DragAble)
Return;
DragAble = false;
// DragDiv.css ("position", "relative ");
This. releaseCapture & this. releaseCapture ();
});
}
}
Var MyDialog = function (cfg ){
This. config = {
Id: (new Date (). getTime (). toString (),
El: null,
BodyId: null,
Cover: true,
BoxHtm :'

'+
'

















'+' '+' '+' '+' '+' '+' '+' '+' '+' '+' '+' '+' '+' '+' '+' '+'
'+
'
'+
'
'+
'
'+
'

'+
'
'+
'

'+
''+
'Enter the title' +
''+
'

'+
'

'+
'Enter the content' +
'

'+
'
'+
'
'+
'
'+
'

'+
'
'+
'
'+
'

'
};
Var scope = this;
If (cfg ){
$. Each (cfg, function (key, value ){
Scope. config [key] = value;
});
}
This. box = null;
This. cover = null;
This. tmpBody = null;
}
MyDialog. prototype. show = function (){
Var scope = this;
Var cover = null;
Var box = null;
If (this. config. cover ){
If (this. config. id & $ ('#' + this. config. id + '_ cover') [0]) {
Cover = $ ('#' + this. config. id + '_ cover ');
Cover. show ();
} Else {
Cover = $ ('

');
$ ('Body'). append (cover );
}
Scope. cover = cover;
}
If (! $ ('#' + This. config. id) [0]) {
Box = $ (this. config. boxHtm );
$ ('Body'). append (box );
Box. attr ('id', this. config. id );
If (this. config. title ){
Box.find('.title_text'{.html (this. config. title );
}
If (this. config. bodyId ){
Var body = $ ('#' + this. config. bodyId );
Var tmp = $ ('

'). Append (body );
Var initBody = tmp.html ();
Scope. tmpBody =$ (initBody );
Tmp = null;
If (body [0]) {
Var con = box. find ('. main. content ');
Body. show ();
Con.html ('');
Con. append (body );
}
}
If (this. config. el & this. config. el [0]) {
Var con = box. find ('. main. content ');
Con.html (this. config. el );
}
// Center
Popup (box );
// Disable dialog
Box. find ('. title. cls'). click (function (e ){
Scope. close ();
E. preventDefault ();
Return false;
});
DragFunc ($ ('#' + this. config. id + '. main. title'), $ (' # '+ this. config. id ));
Box. show ();
This. box = box;
}
}
MyDialog. prototype. close = function (){
// There is a problem
Var box = this. box;
Var tmpBody = this. tmpBody;
Var cover = this. cover;
If (tmpBody & tmpBody [0]) {
$ ('Body'). append (tmpBody );
}
If (box & box [0]) {
Box. remove ();
}
If (cover & cover [0]) {
Cover. hide ();
}
};


Call method:

The Code is as follows:


Var dia = new MyDialog ({
Title: title,
BodyId: id,
Id: id + '_ box'
});
Dia. show ();


Some Function callbacks may be required. You can encapsulate them yourself.

Drag and DropSome requirements for drag-and-drop effects often occur during work:

The Code is as follows::

The Code is as follows:






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.