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/1-mouse-events-basics/article.md
+25-27Lines changed: 25 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,12 @@
1
-
# Mouse events basics
1
+
# Mouse events
2
2
3
3
In this chapter we'll get into more details about mouse events and their properties.
4
4
5
5
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.
6
6
7
7
## Mouse event types
8
8
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:
14
10
15
11
`mousedown/mouseup`
16
12
: Mouse button is clicked/released over an element.
@@ -21,28 +17,24 @@ The most used simple events are:
21
17
`mousemove`
22
18
: Every mouse move over an element triggers that event.
23
19
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
-
31
20
`click`
32
21
: Triggers after `mousedown` and then `mouseup` over the same element if the left mouse button was used.
33
22
34
23
`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.
36
25
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.
38
28
39
-
### Events order
29
+
...There are several other events too, we'll cover them later.
40
30
41
-
An action may trigger multiple events.
31
+
## Events order
42
32
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.
44
34
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`.
46
38
47
39
```online
48
40
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.
54
46
<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>
55
47
```
56
48
57
-
## Getting the button: which
49
+
## Mouse button: "which"
58
50
59
51
Click-related events always have the `which` property, which allows to get the exact mouse button.
60
52
@@ -116,17 +108,25 @@ For JS-code it means that we should check `if (event.ctrlKey || event.metaKey)`.
116
108
```
117
109
118
110
```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.
120
114
```
121
115
122
116
## Coordinates: clientX/Y, pageX/Y
123
117
124
-
All mouse events have coordinates in two flavours:
118
+
All mouse events provide coordinates in two flavours:
125
119
126
120
1. Window-relative: `clientX` and `clientY`.
127
121
2. Document-relative: `pageX` and `pageY`.
128
122
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.
130
130
131
131
````online
132
132
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
136
136
```
137
137
````
138
138
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
142
140
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.
144
142
145
143
For instance, a double-click on the text below selects it in addition to our handler:
0 commit comments