Skip to content

Commit 0564de0

Browse files
authored
Merge pull request #353 from odsantos/update-en-drag-n-drop
Update "Drag'n'Drop" files
2 parents 7bcc94e + 6ed8a95 commit 0564de0

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed
Lines changed: 1 addition & 1 deletion
Loading

2-ui/3-event-details/4-mouse-drag-and-drop/article.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ The basic Drag'n'Drop algorithm looks like this:
1818
2. Then on `mousemove` move it by changing `left/top` with `position:absolute`.
1919
3. On `mouseup` - perform all actions related to finishing the drag'n'drop.
2020

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.
2222

2323
Here's the implementation of dragging a ball:
2424

2525
```js
26-
ball.onmousedown = function(event) {
26+
ball.onmousedown = function(event) {
2727
// (1) prepare to moving: make absolute and on top by z-index
2828
ball.style.position = 'absolute';
2929
ball.style.zIndex = 1000;
3030

3131
// move it out of any current parents directly into body
3232
// to make it positioned relative to the body
33-
document.body.append(ball);
33+
document.body.append(ball);
3434

3535
// centers the ball at (pageX, pageY) coordinates
3636
function moveAt(pageX, pageY) {
@@ -93,14 +93,14 @@ So we should listen on `document` to catch it.
9393

9494
## Correct positioning
9595

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:
9797

9898
```js
9999
ball.style.left = pageX - ball.offsetWidth / 2 + 'px';
100100
ball.style.top = pageY - ball.offsetHeight / 2 + 'px';
101101
```
102102

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 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.
104104

105105
It would be better if we keep the initial shift of the element relative to the pointer.
106106

@@ -219,7 +219,7 @@ That's why the initial idea to put handlers on potential droppables doesn't work
219219
220220
So, what to do?
221221
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.
223223

224224
We can use it in any of our mouse event handlers to detect the potential droppable under the pointer, like this:
225225

0 commit comments

Comments
 (0)