Skip to content

Commit db789a0

Browse files
committed
minor
1 parent a9e5ed5 commit db789a0

File tree

3 files changed

+21
-31
lines changed

3 files changed

+21
-31
lines changed

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

+17-17
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,51 @@ Drag'n'Drop is a great interface solution. Taking something and dragging and dro
44

55
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.
66

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

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

1111
So here we'll see how to implement Drag'n'Drop using mouse events.
1212

1313
## Drag'n'Drop algorithm
1414

1515
The basic Drag'n'Drop algorithm looks like this:
1616

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

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

23-
Here's the algorithm for drag'n'drop of a ball:
23+
Here's the implementation of dragging a ball:
2424

2525
```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
2928
ball.style.position = 'absolute';
3029
ball.style.zIndex = 1000;
30+
3131
// move it out of any current parents directly into body
3232
// to make it positioned relative to the body
3333
document.body.append(ball);
34-
// ...and put that absolutely positioned ball under the pointer
35-
36-
moveAt(event.pageX, event.pageY);
3734

3835
// centers the ball at (pageX, pageY) coordinates
3936
function moveAt(pageX, pageY) {
4037
ball.style.left = pageX - ball.offsetWidth / 2 + 'px';
4138
ball.style.top = pageY - ball.offsetHeight / 2 + 'px';
4239
}
4340

41+
// move our absolutely positioned ball under the pointer
42+
moveAt(event.pageX, event.pageY);
43+
4444
function onMouseMove(event) {
4545
moveAt(event.pageX, event.pageY);
4646
}
4747

48-
// (3) move the ball on mousemove
48+
// (2) move the ball on mousemove
4949
document.addEventListener('mousemove', onMouseMove);
5050

51-
// (4) drop the ball, remove unneeded handlers
51+
// (3) drop the ball, remove unneeded handlers
5252
ball.onmouseup = function() {
5353
document.removeEventListener('mousemove', onMouseMove);
5454
ball.onmouseup = null;
@@ -64,10 +64,10 @@ Here's an example in action:
6464
6565
[iframe src="ball" height=230]
6666
67-
Try to drag'n'drop the mouse and you'll see such behavior.
67+
Try to drag'n'drop with the mouse and you'll see such behavior.
6868
```
6969

70-
That's because the browser has its own Drag'n'Drop for images and some other elements that runs automatically and conflicts with ours.
70+
That's because the browser has its own drag'n'drop support for images and some other elements. It runs automatically and conflicts with ours.
7171

7272
To disable it:
7373

Diff for: 2-ui/3-event-details/4-mouse-drag-and-drop/ball.view/index.html

+2-7
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313

1414

1515
<script>
16-
ball.onmousedown = function(event) { // (1) start the process
17-
18-
// (2) prepare to moving: make absolute and top by z-index
16+
ball.onmousedown = function(event) {
1917
ball.style.position = 'absolute';
2018
ball.style.zIndex = 1000;
2119
document.body.appendChild(ball);
22-
// ...and put that absolutely positioned ball under the cursor
20+
2321
moveAt(event.pageX, event.pageY);
2422

25-
// centers the ball at (pageX, pageY) coordinates
2623
function moveAt(pageX, pageY) {
2724
ball.style.left = pageX - ball.offsetWidth / 2 + 'px';
2825
ball.style.top = pageY - ball.offsetHeight / 2 + 'px';
@@ -32,10 +29,8 @@
3229
moveAt(event.pageX, event.pageY);
3330
}
3431

35-
// (3) move the ball on mousemove
3632
document.addEventListener('mousemove', onMouseMove);
3733

38-
// (4) drop the ball, remove unneeded handlers
3934
ball.onmouseup = function() {
4035
document.removeEventListener('mousemove', onMouseMove);
4136
ball.onmouseup = null;

Diff for: 2-ui/3-event-details/4-mouse-drag-and-drop/ball2.view/index.html

+2-7
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@
1313

1414

1515
<script>
16-
ball.onmousedown = function(event) { // (1) start the process
17-
18-
// (2) prepare to moving: make absolute and top by z-index
16+
ball.onmousedown = function(event) {s
1917
ball.style.position = 'absolute';
2018
ball.style.zIndex = 1000;
2119
document.body.appendChild(ball);
22-
// ...and put that absolutely positioned ball under the cursor
20+
2321
moveAt(event.pageX, event.pageY);
2422

25-
// centers the ball at (pageX, pageY) coordinates
2623
function moveAt(pageX, pageY) {
2724
ball.style.left = pageX - ball.offsetWidth / 2 + 'px';
2825
ball.style.top = pageY - ball.offsetHeight / 2 + 'px';
@@ -32,10 +29,8 @@
3229
moveAt(event.pageX, event.pageY);
3330
}
3431

35-
// (3) move the ball on mousemove
3632
document.addEventListener('mousemove', onMouseMove);
3733

38-
// (4) drop the ball, remove unneeded handlers
3934
ball.onmouseup = function() {
4035
document.removeEventListener('mousemove', onMouseMove);
4136
ball.onmouseup = null;

0 commit comments

Comments
 (0)