Skip to content

Commit f5fcfbb

Browse files
committed
Moving the mouse: mouseover/out, mouseenter/leave
1 parent ca203b3 commit f5fcfbb

File tree

7 files changed

+162
-162
lines changed

7 files changed

+162
-162
lines changed

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/1-behavior-nested-tooltip/task.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@ importance: 5
22

33
---
44

5-
# Improved tooltip behavior
5+
# Comportamento del tooltip migliorato
66

7-
Write JavaScript that shows a tooltip over an element with the attribute `data-tooltip`. The value of this attribute should become the tooltip text.
7+
Scrivere del codice JavaScript che mostri un tooltip su un elemento con un attributo `data-tooltip`. Il valore di questo attributo dovrebbe rappresentare il testo del tooltip.
88

9-
That's like the task <info:task/behavior-tooltip>, but here the annotated elements can be nested. The most deeply nested tooltip is shown.
9+
Questo compito è come quello di <info:task/behavior-tooltip>, con la differenza che qui gli elementi delle annotazioni possono essere annidati. Deve essere mostrato il tooltip più annidato.
1010

11-
Only one tooltip may show up at the same time.
11+
Può essere mostrato solo un tooltip alla volta.
1212

13-
For instance:
13+
Per esempio:
1414

1515
```html
16-
<div data-tooltip="Hereis the house interior" id="house">
17-
<div data-tooltip="Hereis the roof" id="roof"></div>
16+
<div data-tooltip="Quil'interno della casa" id="house">
17+
<div data-tooltip="Quiil tetto" id="roof"></div>
1818
...
19-
<a href="https://en.wikipedia.org/wiki/The_Three_Little_Pigs" data-tooltip="Read on">Hover over me</a>
19+
<a href="https://en.wikipedia.org/wiki/The_Three_Little_Pigs" data-tooltip="Leggi">Hover su di me</a>
2020
</div>
2121
```
2222

23-
The result in iframe:
23+
Il risultato dell'iframe:
2424

2525
[iframe src="solution" height=300 border=1]
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11

2-
The algorithm looks simple:
3-
1. Put `onmouseover/out` handlers on the element. Also can use `onmouseenter/leave` here, but they are less universal, won't work if we introduce delegation.
4-
2. When a mouse cursor entered the element, start measuring the speed on `mousemove`.
5-
3. If the speed is slow, then run `over`.
6-
4. When we're going out of the element, and `over` was executed, run `out`.
2+
L'algoritmo è semplice:
3+
1. Impostare dei gestori `onmouseover/out` sull'elemento. Qui si possono anche usare `onmouseenter/leave`, però sono meno universali, e non funzionerebbero se introducessimo l'uso della delegation.
4+
2. Quando il puntatore è entrato dentro l'elemento, si comincia a misurare la velocità al `mousemove`.
5+
3. Se la velocità è lenta, eseguire `over`.
6+
4. Quando si esce fuori dall'elemento, ed è stato eseguito `over`, eseguire `out`.
77

8-
But how to measure the speed?
8+
Ma come misurare la velocità?
99

10-
The first idea can be: run a function every `100ms` and measure the distance between previous and new coordinates. If it's small, then the speed is small.
10+
La prima strategia potrebbe essere: eseguire una funzione ogni `100ms` e misurare la distanza tra le vecchie e nuove coordinate. Se fosse piccola, anche la velocità lo sarebbe.
1111

12-
Unfortunately, there's no way to get "current mouse coordinates" in JavaScript. There's no function like `getCurrentMouseCoordinates()`.
12+
Sfortunatamente, non c'è modo di ricavare "le coordinate attuali del mouse" in JavaScript. Non esistono funzioni come `getCurrentMouseCoordinates()`.
1313

14-
The only way to get coordinates is to listen for mouse events, like `mousemove`, and take coordinates from the event object.
14+
L'unico modo è di mettersi in ascolto sugli eventi del mouse, come `mousemove`, e prendere le coordinate dall'oggetto evento.
1515

16-
So let's set a handler on `mousemove` to track coordinates and remember them. And then compare them, once per `100ms`.
16+
Quindi impostiamo un gestore su `mousemove` per tenere traccia delle coordinate e memorizzarle, per poi confrontarle ogni `100ms`.
1717

18-
P.S. Please note: the solution tests use `dispatchEvent` to see if the tooltip works right.
18+
P.S.: Nota bene: i test della soluzione fanno uso di `dispatchEvent` per vedere se il tooltip funziona bene.

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/2-hoverintent/solution.view/hoverIntent.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
class HoverIntent {
44

55
constructor({
6-
sensitivity = 0.1, // speed less than 0.1px/ms means "hovering over an element"
7-
interval = 100, // measure mouse speed once per 100ms
6+
sensitivity = 0.1, // velocità inferiore a 0.1px/ms indica "hovering su un elemento"
7+
interval = 100, // misura la velocità del mouse ogni 100ms
88
elem,
99
over,
1010
out
@@ -15,12 +15,12 @@ class HoverIntent {
1515
this.over = over;
1616
this.out = out;
1717

18-
// make sure "this" is the object in event handlers.
18+
// si assicura che "this" sia l'oggetto nei gestori evento.
1919
this.onMouseMove = this.onMouseMove.bind(this);
2020
this.onMouseOver = this.onMouseOver.bind(this);
2121
this.onMouseOut = this.onMouseOut.bind(this);
2222

23-
// and in time-measuring function (called from setInterval)
23+
// e che nella funzione che misura il tempo (chiamata da setInterval)
2424
this.trackSpeed = this.trackSpeed.bind(this);
2525

2626
elem.addEventListener("mouseover", this.onMouseOver);
@@ -32,16 +32,16 @@ class HoverIntent {
3232
onMouseOver(event) {
3333

3434
if (this.isOverElement) {
35-
// if we're over the element, then ignore the event
36-
// we are already measuring the speed
35+
// se siamo sull'elemento, ignoriamo l'evento
36+
// dato che stiamo già misurando la velocità
3737
return;
3838
}
3939

4040
this.isOverElement = true;
4141

42-
// after every mousemove we'll be check the distance
43-
// between the previous and the current mouse coordinates
44-
// if it's less than sensivity, then the speed is slow
42+
// dopo ogni mousemove controlliamo la distanza
43+
// tra le coordinate attuali e le precedenti
44+
// se e' minore della "sensibilità", allora la velocità sarà bassa
4545

4646
this.prevX = event.pageX;
4747
this.prevY = event.pageY;
@@ -52,13 +52,13 @@ class HoverIntent {
5252
}
5353

5454
onMouseOut(event) {
55-
// if left the element
55+
// se ha abbandonato l'elemento
5656
if (!event.relatedTarget || !elem.contains(event.relatedTarget)) {
5757
this.isOverElement = false;
5858
this.elem.removeEventListener('mousemove', this.onMouseMove);
5959
clearInterval(this.checkSpeedInterval);
6060
if (this.isHover) {
61-
// if there was a stop over the element
61+
// se c'è stato uno stop sull'elemento
6262
this.out.call(this.elem, event);
6363
this.isHover = false;
6464
}
@@ -76,7 +76,7 @@ class HoverIntent {
7676
let speed;
7777

7878
if (!this.lastTime || this.lastTime == this.prevTime) {
79-
// cursor didn't move
79+
// il cursore non si è mosso
8080
speed = 0;
8181
} else {
8282
speed = Math.sqrt(
@@ -90,7 +90,7 @@ class HoverIntent {
9090
this.isHover = true;
9191
this.over.call(this.elem, event);
9292
} else {
93-
// speed fast, remember new coordinates as the previous ones
93+
// alta velocità, memorizza le nuove coordinate come quelle precedenti
9494
this.prevX = this.lastX;
9595
this.prevY = this.lastY;
9696
this.prevTime = this.lastTime;

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/2-hoverintent/source.view/hoverIntent.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

3-
// Here's a brief sketch of the class
4-
// with things that you'll need anyway
3+
// Ecco una bozza
4+
// con le cose di cui avrete bisogno
55
class HoverIntent {
66

77
constructor({
8-
sensitivity = 0.1, // speed less than 0.1px/ms means "hovering over an element"
9-
interval = 100, // measure mouse speed once per 100ms: calculate the distance between previous and next points
8+
sensitivity = 0.1, // una velocità inferiore a 0.1px/ms indica "hovering su un elemento"
9+
interval = 100, // misura la velocità del mouse ogni 100ms
1010
elem,
1111
over,
1212
out
@@ -17,16 +17,16 @@ class HoverIntent {
1717
this.over = over;
1818
this.out = out;
1919

20-
// make sure "this" is the object in event handlers.
20+
// si assicura che "this" sia l'oggetto nei gestori evento.
2121
this.onMouseMove = this.onMouseMove.bind(this);
2222
this.onMouseOver = this.onMouseOver.bind(this);
2323
this.onMouseOut = this.onMouseOut.bind(this);
2424

25-
// assign the handlers
25+
// assegna i gestori
2626
elem.addEventListener("mouseover", this.onMouseOver);
2727
elem.addEventListener("mouseout", this.onMouseOut);
2828

29-
// continue from this point
29+
// continua da questo punto
3030

3131
}
3232

@@ -44,8 +44,8 @@ class HoverIntent {
4444

4545

4646
destroy() {
47-
/* your code to "disable" the functionality, remove all handlers */
48-
/* it's needed for the tests to work */
47+
/* il tuo codice per "disattivare" la funzionalità, che rimuove tutti i gestori */
48+
/* è necessario per fare funzionare i test */
4949
}
5050

5151
}

2-ui/3-event-details/3-mousemove-mouseover-mouseout-mouseenter-mouseleave/2-hoverintent/task.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@ importance: 5
44

55
# "Smart" tooltip
66

7-
Write a function that shows a tooltip over an element only if the visitor moves the mouse *to it*, but not *through it*.
7+
Scrivere una funzione che mostri un tooltip su un elemento solo se l'utente sposta il mouse *su di esso*, e non *attraverso di esso*.
88

9-
In other words, if the visitor moves the mouse to the element and stops there -- show the tooltip. And if they just moved the mouse through, then no need, who wants extra blinking?
9+
In altre parole, se il visitatore sposta il mouse su questo elemento e si ferma lì -- mostra il tooltip. Se invece ha solo spostato il mouse passandoci sopra, non ce n'è bisogno, d'altronde chi mai vorrebbe altri elementi lampeggianti non desiderati?
1010

11-
Technically, we can measure the mouse speed over the element, and if it's slow then we assume that it comes "over the element" and show the tooltip, if it's fast -- then we ignore it.
11+
Tecnicamente, possiamo misurare la velocità del mouse su un elemento, e se è abbastanza lento possiamo supporre che sta arrivando proprio "sull'elemento", mostrando il tooltip, se è troppo veloce -- lo ignoriamo.
1212

13-
Make a universal object `new HoverIntent(options)` for it.
13+
Creare un oggetto universale `new HoverIntent(options)` utile allo scopo.
1414

15-
Its `options`:
16-
- `elem` -- element to track.
17-
- `over` -- a function to call if the mouse came to the element: that is, it moves slowly or stopped over it.
18-
- `out` -- a function to call when the mouse leaves the element (if `over` was called).
15+
Le opzioni possibili `options`:
16+
- `elem` -- elemento da tracciare.
17+
- `over` -- una funzione da chiamare se il mouse arriva sull'elemento: ossia, se si muove lentamente o se si ferma sull'elemento.
18+
- `out` -- una funzione da chiamare quando il mouse abbandona l'elemento (se è stato chiamato `over`).
1919

20-
An example of using such object for the tooltip:
20+
Ecco un esempio dell'uso di questo oggetto per il tooltip:
2121

2222
```js
23-
// a sample tooltip
23+
// un tooltip di esempio
2424
let tooltip = document.createElement('div');
2525
tooltip.className = "tooltip";
2626
tooltip.innerHTML = "Tooltip";
2727

28-
// the object will track mouse and call over/out
28+
// l'oggetto tiene traccia del mouse e chiama over/out
2929
new HoverIntent({
3030
elem,
3131
over() {
@@ -39,10 +39,10 @@ new HoverIntent({
3939
});
4040
```
4141

42-
The demo:
42+
La demo:
4343

4444
[iframe src="solution" height=140]
4545

46-
If you move the mouse over the "clock" fast then nothing happens, and if you do it slow or stop on them, then there will be a tooltip.
46+
Muovendo il mouse oltre la velocità di "clock" non succede nulla, facendolo lentamente o fermandocisi sopra, viene mostrato il tooltip.
4747

48-
Please note: the tooltip doesn't "blink" when the cursor moves between the clock subelements.
48+
Nota bene: il tooltip non "lampeggia" quando il cursore si muove tra i sottoelementi dell'orologio.

0 commit comments

Comments
 (0)