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
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,19 +18,19 @@ The basic Drag'n'Drop algorithm looks like this:
18
18
2. Then on `mousemove` move it by changing `left/top` with `position:absolute`.
19
19
3. On `mouseup` - perform all actions related to finishing the drag'n'drop.
20
20
21
-
These are the basics. Later we'll see how to other features, such as highlighting current underlying elements while we drag over them.
21
+
These are the basics. Later we'll see how to add other features, such as highlighting current underlying elements while we drag over them.
22
22
23
23
Here's the implementation of dragging a ball:
24
24
25
25
```js
26
-
ball.onmousedown=function(event) {
26
+
ball.onmousedown=function(event) {
27
27
// (1) prepare to moving: make absolute and on top by z-index
28
28
ball.style.position='absolute';
29
29
ball.style.zIndex=1000;
30
30
31
31
// move it out of any current parents directly into body
32
32
// to make it positioned relative to the body
33
-
document.body.append(ball);
33
+
document.body.append(ball);
34
34
35
35
// centers the ball at (pageX, pageY) coordinates
36
36
functionmoveAt(pageX, pageY) {
@@ -93,14 +93,14 @@ So we should listen on `document` to catch it.
93
93
94
94
## Correct positioning
95
95
96
-
In the examples above the ball is always moved so, that it's center is under the pointer:
96
+
In the examples above the ball is always moved so that its center is under the pointer:
97
97
98
98
```js
99
99
ball.style.left= pageX -ball.offsetWidth/2+'px';
100
100
ball.style.top= pageY -ball.offsetHeight/2+'px';
101
101
```
102
102
103
-
Not bad, but there's a side-effect. To initiate the drag'n'drop, we can `mousedown` anywhere on the ball. But if "take" it from its edge, then the ball suddenly "jumps" to become centered under the mouse pointer.
103
+
Not bad, but there's a sideeffect. To initiate the drag'n'drop, we can `mousedown` anywhere on the ball. But if "take" it from its edge, then the ball suddenly "jumps" to become centered under the mouse pointer.
104
104
105
105
It would be better if we keep the initial shift of the element relative to the pointer.
106
106
@@ -219,7 +219,7 @@ That's why the initial idea to put handlers on potential droppables doesn't work
219
219
220
220
So, what to do?
221
221
222
-
There's a method called `document.elementFromPoint(clientX, clientY)`. It returns the most nested element on given window-relative coordinates (or `null`if given coordinates are out of the window).
222
+
There's a method called `document.elementFromPoint(clientX, clientY)`. It returns the most nested element on given window-relative coordinates (or `null`if given coordinates are out of the window).If there are multiple overlapping elements on the same coordinates, then the topmost one is returned.
223
223
224
224
We can use it in any of our mouse event handlers to detect the potential droppable under the pointer, like this:
0 commit comments