JQuery drag div implementation _ jquery-js tutorial

Source: Internet
Author: User
JQuery drag and drop the div, and use jquery's mousemove, mousedown, and mouseup events. If you need them, you can refer to the following ideas: Use jquery's mousemove, mousedown, and mouseup events, define two relative positions:

1. The relative position between the upper left corner of the widget and the upper left corner of the screen

2. The coordinates of the mouse and the relative position in the upper left corner of the widget.

The specific functions are as follows:

The Code is as follows:


. Drag {
Position: absolute;
Background: # 0000CC;
Top: 100px; left: 200px;
Padding: 0;
}


The Code is as follows:


$ (Document). ready (function (){
Var move = false; // move the tag
Var _ x, _ y; // the relative position of the cursor in the upper left corner of the control.
$ (". Drag"). mousedown (function (e ){
Move = true;
_ X = e. pageX-parseInt ($ (". drag" ).css ("left "));
_ Y = e. pageY-parseInt ($ (". drag" ).css ("top "));
});
$ (Document). mousemove (function (e ){
If (move ){
Var x = e. pageX-_ x; // relative position from the upper left corner of the control to the upper left corner of the screen
Var y = e. pageY-_ y;
$ (". Drag" ).css ({"top": y, "left": x });
}
}). Mouseup (function (){
Move = false;
});


E. pageX and e. pageY are the horizontal and vertical coordinates of the current mouse.

You can give it a try on your own to get a clearer idea ~
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.