Skip to content

Commit a9e5ed5

Browse files
committed
minor rephrasings
1 parent 681ef7b commit a9e5ed5

File tree

1 file changed

+25
-27
lines changed
  • 2-ui/3-event-details/1-mouse-events-basics

1 file changed

+25
-27
lines changed

2-ui/3-event-details/1-mouse-events-basics/article.md

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
# Mouse events basics
1+
# Mouse events
22

33
In this chapter we'll get into more details about mouse events and their properties.
44

55
Please note: such events may come not only from "mouse devices", but are also from other devices, such as phones and tablets, where they are emulated for compatibility.
66

77
## Mouse event types
88

9-
We can split mouse events into two categories: "simple" and "complex"
10-
11-
### Simple events
12-
13-
The most used simple events are:
9+
We've already seen some of these events:
1410

1511
`mousedown/mouseup`
1612
: Mouse button is clicked/released over an element.
@@ -21,28 +17,24 @@ The most used simple events are:
2117
`mousemove`
2218
: Every mouse move over an element triggers that event.
2319

24-
`contextmenu`
25-
: Triggers when opening a context menu is attempted. In the most common case, that happens when the right mouse button is pressed. Although, there are other ways to open a context menu, e.g. using a special keyboard key, so it's not exactly the mouse event.
26-
27-
...There are several other event types too, we'll cover them later.
28-
29-
### Complex events
30-
3120
`click`
3221
: Triggers after `mousedown` and then `mouseup` over the same element if the left mouse button was used.
3322

3423
`dblclick`
35-
: Triggers after a double click over an element.
24+
: Triggers after two clicks on the same element within a short timeframe. Rarely used nowadays.
3625

37-
Complex events are made of simple ones, so in theory we could live without them. But they exist, and that's good, because they are convenient.
26+
`contextmenu`
27+
: Triggers when when the right mouse button is pressed. There are other ways to open a context menu, e.g. using a special keyboard key, it triggers in that case also, so it's not exactly the mouse event.
3828

39-
### Events order
29+
...There are several other events too, we'll cover them later.
4030

41-
An action may trigger multiple events.
31+
## Events order
4232

43-
For instance, a click first triggers `mousedown`, when the button is pressed, then `mouseup` and `click` when it's released.
33+
As you can see from the list above, a user action may trigger multiple events.
4434

45-
In cases when a single action initiates multiple events, their order is fixed. That is, the handlers are called in the order `mousedown` -> `mouseup` -> `click`.
35+
For instance, a left-button click first triggers `mousedown`, when the button is pressed, then `mouseup` and `click` when it's released.
36+
37+
In cases when a single action initiates multiple events, their order is fixed. That is, the handlers are called in the order `mousedown` -> `mouseup` -> `click`.
4638

4739
```online
4840
Click the button below and you'll see the events. Try double-click too.
@@ -54,7 +46,7 @@ Also we can see the `which` property that allows to detect the mouse button.
5446
<input onmousedown="return logMouse(event)" onmouseup="return logMouse(event)" onclick="return logMouse(event)" oncontextmenu="return logMouse(event)" ondblclick="return logMouse(event)" value="Click me with the right or the left mouse button" type="button"> <input onclick="logClear('test')" value="Clear" type="button"> <form id="testform" name="testform"> <textarea style="font-size:12px;height:150px;width:360px;"></textarea></form>
5547
```
5648

57-
## Getting the button: which
49+
## Mouse button: "which"
5850

5951
Click-related events always have the `which` property, which allows to get the exact mouse button.
6052

@@ -116,17 +108,25 @@ For JS-code it means that we should check `if (event.ctrlKey || event.metaKey)`.
116108
```
117109
118110
```warn header="There are also mobile devices"
119-
Keyboard combinations are good as an addition to the workflow. So that if the visitor has a keyboard -- it works. And if their device doesn't have it -- then there should be another way to do the same.
111+
Keyboard combinations are good as an addition to the workflow. So that if the visitor uses a keyboard -- they work.
112+
113+
But if their device doesn't have it -- then there should be a way to live without modifier keys.
120114
```
121115

122116
## Coordinates: clientX/Y, pageX/Y
123117

124-
All mouse events have coordinates in two flavours:
118+
All mouse events provide coordinates in two flavours:
125119

126120
1. Window-relative: `clientX` and `clientY`.
127121
2. Document-relative: `pageX` and `pageY`.
128122

129-
For instance, if we have a window of the size 500x500, and the mouse is in the left-upper corner, then `clientX` and `clientY` are `0`. And if the mouse is in the center, then `clientX` and `clientY` are `250`, no matter what place in the document it is, how far the document was scrolled. They are similar to `position:fixed`.
123+
We already covered the difference between them in the chapter <info:coordinates>.
124+
125+
In short, document-relative coordinates `pageX/Y` are counted from the left-upper corner of the document, and do not change when the page is scrolled, while `clientX/Y` are counted from the current window left-upper corner. When the page is scrolled, they change.
126+
127+
For instance, if we have a window of the size 500x500, and the mouse is in the left-upper corner, then `clientX` and `clientY` are `0`, no matter how the page is scrolled.
128+
129+
And if the mouse is in the center, then `clientX` and `clientY` are `250`, no matter what place in the document it is. They are similar to `position:fixed` in that aspect.
130130

131131
````online
132132
Move the mouse over the input field to see `clientX/clientY` (the example is in the `iframe`, so coordinates are relative to that `iframe`):
@@ -136,11 +136,9 @@ Move the mouse over the input field to see `clientX/clientY` (the example is in
136136
```
137137
````
138138

139-
Document-relative coordinates `pageX`, `pageY` are counted from the left-upper corner of the document, not the window. You can read more about coordinates in the chapter <info:coordinates>.
140-
141-
## Disabling selection
139+
## Preventing selection on mousedown
142140

143-
Double mouse click has a side-effect that may be disturbing in some interfaces: it selects the text.
141+
Double mouse click has a side-effect that may be disturbing in some interfaces: it selects text.
144142

145143
For instance, a double-click on the text below selects it in addition to our handler:
146144

0 commit comments

Comments
 (0)