You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/3-event-details/4-mouse-drag-and-drop/article.md
+17-17
Original file line number
Diff line number
Diff line change
@@ -4,51 +4,51 @@ Drag'n'Drop is a great interface solution. Taking something and dragging and dro
4
4
5
5
In the modern HTML standard there's a [section about Drag and Drop](https://html.spec.whatwg.org/multipage/interaction.html#dnd) with special events such as `dragstart`, `dragend`, and so on.
6
6
7
-
These events are useful in that they allow us to solve simple tasks easily. For instance, they allow us to handle the drag'n'drop of "external" files into the browser, so we can take a file in the OS file-manager and drop it into the browser window, thereby giving JavaScript access to its contents.
7
+
These events allow us to support special kinds of drag'n'drop, such as handling dragging a file from OS file-manager and dropping it into the browser window. Then JavaScript can access the contents of such files.
8
8
9
-
But native Drag Events also have limitations. For instance, we can't limit dragging by a certain area. Also we can't make it "horizontal" or "vertical" only. And there are other drag'n'drop tasks that can't be done using that API. Also, mobile device support for such events is almost non-existant.
9
+
But native Drag Events also have limitations. For instance, we can't prevent dragging from a certain area. Also we can't make the dragging "horizontal" or "vertical" only. And there are many other drag'n'drop tasks that can't be done using them. Also, mobile device support for such events is very weak.
10
10
11
11
So here we'll see how to implement Drag'n'Drop using mouse events.
12
12
13
13
## Drag'n'Drop algorithm
14
14
15
15
The basic Drag'n'Drop algorithm looks like this:
16
16
17
-
1. On `mousedown` - prepare the element for moving, if needed (maybe create a copy of it).
18
-
2. Then on `mousemove` move it by changing `left/top`and`position:absolute`.
19
-
3. On `mouseup` - perform all actions related to a finished Drag'n'Drop.
17
+
1. On `mousedown` - prepare the element for moving, if needed (maybe create a clone of it, add a class to it or whatever).
18
+
2. Then on `mousemove` move it by changing `left/top`with`position:absolute`.
19
+
3. On `mouseup` - perform all actions related to finishing the drag'n'drop.
20
20
21
-
These are the basics. Later we can extend it, for instance, by highlighting droppable (available for the drop) elements when hovering over them.
21
+
These are the basics. Later we'll see how to other features, such as highlighting current underlying elements while we drag over them.
22
22
23
-
Here's the algorithm for drag'n'drop of a ball:
23
+
Here's the implementation of dragging a ball:
24
24
25
25
```js
26
-
ball.onmousedown=function(event) { // (1) start the process
27
-
28
-
// (2) prepare to moving: make absolute and on top by z-index
26
+
ball.onmousedown=function(event) {
27
+
// (1) prepare to moving: make absolute and on top by z-index
29
28
ball.style.position='absolute';
30
29
ball.style.zIndex=1000;
30
+
31
31
// move it out of any current parents directly into body
32
32
// to make it positioned relative to the body
33
33
document.body.append(ball);
34
-
// ...and put that absolutely positioned ball under the pointer
35
-
36
-
moveAt(event.pageX, event.pageY);
37
34
38
35
// centers the ball at (pageX, pageY) coordinates
39
36
functionmoveAt(pageX, pageY) {
40
37
ball.style.left= pageX -ball.offsetWidth/2+'px';
41
38
ball.style.top= pageY -ball.offsetHeight/2+'px';
42
39
}
43
40
41
+
// move our absolutely positioned ball under the pointer
0 commit comments