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
-49Lines changed: 0 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -124,14 +124,9 @@ For JS-code it means that we should check `if (event.ctrlKey || event.metaKey)`.
124
124
```
125
125
126
126
```warn header="There are also mobile devices"
127
-
<<<<<<< HEAD
128
-
Keyboard combinations are good as an addition to the workflow. So that if the visitor has a
129
-
keyboard -- it works. And if your device doesn't have it -- then there's another way to do the same.
130
-
=======
131
127
Keyboard combinations are good as an addition to the workflow. So that if the visitor uses a keyboard -- they work.
132
128
133
129
But if their device doesn't have it -- then there should be a way to live without modifier keys.
134
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
135
130
```
136
131
137
132
## Coordinates: clientX/Y, pageX/Y
@@ -141,17 +136,13 @@ All mouse events provide coordinates in two flavours:
141
136
1. Window-relative: `clientX` and `clientY`.
142
137
2. Document-relative: `pageX` and `pageY`.
143
138
144
-
<<<<<<< HEAD
145
-
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. They are similar to `position:fixed`.
146
-
=======
147
139
We already covered the difference between them in the chapter <info:coordinates>.
148
140
149
141
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.
150
142
151
143
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.
152
144
153
145
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.
154
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
155
146
156
147
````online
157
148
Move the mouse over the input field to see `clientX/clientY` (the example is in the `iframe`, so coordinates are relative to that `iframe`):
@@ -163,19 +154,7 @@ Move the mouse over the input field to see `clientX/clientY` (the example is in
163
154
164
155
## Preventing selection on mousedown
165
156
166
-
<<<<<<< HEAD
167
-
<<<<<<< HEAD
168
-
## No selection on mousedown
169
-
170
-
Mouse clicks have a side-effect that may be disturbing. A double click selects the text.
171
-
172
-
If we want to handle click events ourselves, then the "extra" selection doesn't look good.
173
-
=======
174
-
Double mouse click has a side-effect that may be disturbing in some interfaces: it selects the text.
175
-
>>>>>>> 852ee189170d9022f67ab6d387aeae76810b5923
176
-
=======
177
157
Double mouse click has a side-effect that may be disturbing in some interfaces: it selects text.
178
-
>>>>>>> e074a5f825a3d10b0c1e5e82561162f75516d7e3
179
158
180
159
For instance, double-clicking on the text below selects it in addition to our handler:
181
160
@@ -197,26 +176,7 @@ Before...
197
176
...After
198
177
```
199
178
200
-
<<<<<<< HEAD
201
-
Now the bold element is not selected on double clicks.
202
-
203
-
The text inside it is still selectable. However, the selection should start not on the text itself, but before or after it. Usually that's fine though.
204
-
205
-
````smart header="Canceling the selection"
206
-
Instead of *preventing* the selection, we can cancel it "post-factum" in the event handler.
Now the bold element is not selected on double clicks, and pressing the left button on it won't start the selection.
219
-
>>>>>>> 852ee189170d9022f67ab6d387aeae76810b5923
220
180
221
181
Please note: the text inside it is still selectable. However, the selection should start not on the text itself, but before or after it. Usually that's fine for users.
222
182
@@ -246,15 +206,6 @@ Mouse events have the following properties:
246
206
- Window-relative coordinates: `clientX/clientY`.
247
207
- Document-relative coordinates: `pageX/pageY`.
248
208
249
-
<<<<<<< HEAD
250
-
It's also important to deal with text selection as an unwanted side-effect of clicks.
251
-
252
-
There are several ways to do this, for instance:
253
-
1. The CSS-property `user-select:none` (with browser prefixes) completely disables text-selection.
254
-
2. Cancel the selection post-factum using `getSelection().removeAllRanges()`.
255
-
3. Handle `mousedown` and prevent the default action (usually the best).
256
-
=======
257
209
The default browser action of `mousedown` is text selection, if it's not good for the interface, then it should be prevented.
258
210
259
211
In the next chapter we'll see more details about events that follow pointer movement and how to track element changes under it.
0 commit comments