jquery actual combat--popup box Drag effect

Source: Internet
Author: User
Tags jquery library

Today the main record of the pop-up box drag effect:

First, the steps to move the popup window is: Press the left mouse button-Move the mouse-release the left mouse button to stop moving

Second, the main ideas:

Press the left mouse button: $ (' div '). MouseDown (function (e) {})

Move the mouse to get the current coordinate value of the mouse: $ (document). MouseMove (function (e) {})

Release the left mouse button to stop moving: $ (' div '). MouseUp (function (e) {

$ (document). Unbind (' MouseMove '); That is, the MouseMove event is lifted when the left mouse button is released

})

It is important to note that when the mouse is pressed relative to the upper left corner of the div position is unchanged, so in the time of movement, the div will have to do with the leftmost and top value of the corresponding processing (see code description)

Third, finally, the div in the process of moving to prevent it from moving out of the visual window, so should do the following processing:

To remove from the left:

if (x<0) {

x=0;

}

To remove from the right:

if (x>$ (document). Width ()-$ (' div '). Outerwidth (True))

{

x=$ (document). Width ()-$ (' div '). Outerwidth (True);

}

To disallow removal from the top:

if (y<0) {

y=0;

}

To disallow removal from the bottom:

if (y>$ (document). Height ()-$ (' div '). Outerheight (True)) {

y=$ (document). Height ()-$ (' div '). Outerheight (True);

}

Detailed code to participate in the following:

<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>jquery Drag and drop function</title><Scriptsrc= "Js/jquery-1.8.3.min.js"type= "Text/javascript"></Script><styletype= "Text/css">*{margin:0;padding:0;}Div{width:100px;Height:100px;background:#F00;cursor:Move; Left:0;Top:0;position:Absolute;}</style></Head><Body><!--Drag-and-drop function to change the left and top values move the window: Press the mouse button-Move the mouse-release the mouse to stop moving -<Div></Div></Body><Scripttype= "Text/javascript">$(function(){    //first get the mouse position x coordinate: E.pagex and y coordinate: E.pagey    $('Div'). MouseDown (function(e) {//The left and top values of the mouse are unchanged relative to the upper-right corner of the div while dragging    //Black segment Required: Green Segment-Red segment    varPositiondiv= $( This). offset (); varDistencex=E.pagex-Positiondiv.left; varDistencey=E.pagey-Positiondiv.top; $ (document). MouseMove (function(e) {varx=E.pagex-Distencex; vary=E.pagey-Distencey; //Drag the div inside the visual window          if(x<0){ //prevent the left side from moving out of the visual windowx=0; }Else if(x>$ (document). Width ()-$('Div'). Outerwidth (true)){ //prevent the visible window from being moved to the rightx=$ (document). Width ()-$('Div'). Outerwidth (true); }           if(y<0) //prevent the top from moving out of the visual window{y=0; }Else if(y>$ (document). Height ()-$('Div'). Outerheight (true)){ //prevent the bottom from moving out of the visual windowy=$ (document). Height ()-$('Div'). Outerheight (true); }          //Step2: Assigns x and y coordinates to DIV          $('Div'). css ({' Left': x+'px',            'Top': Y+'px'              });    });    }); $('Div'). MouseUp (function(){//When the left mouse button is released$ (document). Unbind ('MouseMove');//de-MouseMove event (unbind specifies that one or more event handlers are removed from the specified element.) If no parameters are specified, the Unbind () method removes all event handlers for the specified element. )        //in addition to unbind (). Off () can also be used to dismiss the event-high version of the jquery library in a way that is incompatible with the lower version of the jquery library    }); });//step3: Move the window: Press the left mouse button--Move the mouse</Script></HTML>
View Code

jquery actual combat--popup box Drag effect

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.