Web Front-End Development Learning----11. JQuery to achieve simple drag-and-drop effect

Source: Internet
Author: User

Drag-and-drop effects are common in Web pages. It is not difficult to achieve. Record the simple effect of today's implementation.

Drag and Drop Demo

When you drag quickly, there is a problem and you can modify it later.

To be blunt, there are 3 click events.

1. Press the left mouse button, triggering the Click event, the record can get the mouse relative to the position of the drag control (current mouse position-control position);

2. Drag the mouse, trigger the move event, you can calculate the distance of the mouse movement (the current mouse position-calculated relative position), that is, drag the control to move the distance;

3. Mouse lift, end drag.


In JQ, Event.pagex Event.pagey can get the location of the mouse, relative to the upper-left corner of the document.

As shown in figure:


Note that when JQ gets the location of the control:

X=event.pagex-parseint ($ ("#box"). CSS ("left");
Y=event.pagey-parseint ($ ("#box"). CSS ("top");

To remove the unit px.


However, when you modify the control position, do not add units, do not add quotes, and do not add unit px.

Dx=event.pagex-x;
Dy=event.pagey-y;
Do not put quotes ...
$ ("#box"). CSS ({
' Top ':d y, ' left ':d x
})


Complete code:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  





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.