Home  >  Article  >  Web Front-end  >  Jquery writes a mouse drag effect implementation principle and code_jquery

Jquery writes a mouse drag effect implementation principle and code_jquery

WBOY
WBOYOriginal
2016-05-16 17:45:53955browse

In a recent project, I wanted to create a mouse-drag layer effect, so I manually made one using Jquery. I sent it to share with everyone, and I hope to get some advice from experts. If any heroes think that my ideas and codes are incorrect. Or if there is any need for improvement, I hope you can give me some pointers, and I would be very grateful.

My idea is this:
1. Capture the current position of the mouse when the mouse is pressed;

2. Get the desired The current position information of the moving object;

3. When the mouse moves, calculate the distance of the mouse movement, and update this distance to the position of the object. In my code, I use absolute positioning to represent the position of the object;

4. When the mouse moves out of the object or the mouse bounces up, the object is considered to be in an immovable state. This is represented by a bool type variable isMouseDown in my code. When this variable is true, it means that the object is in a movable state. If it is false, it means that the object is in an immovable state. When the mouse moves out of the object or pops up, set isMouseDown to false.

Okay, this is the idea. I will post the code below. If my idea is not clear, you can see it in the code:

Copy code The code is as follows:


< ;head>
Use the mouse to drag the layer code






Put the mouse here and drag me


Layers that can be dragged with the mouse





Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn