Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 60
Moving the mouse: mouseover/out, mouseenter/leave#232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
ce0f88cfa4d924b7106d62e2be58b1e0401771e1390b8b60f6df0d3ba1f1b1a022b0da2a7552bcb283b608b80d5365dab5d65aec028d56577ee33833b43de998ffe813894b473a268d1d1efa835831974c48bc3e35f5df367e790dcdb9182e1e0ce13407a604eea7d4ba8ee305daca2d276d01abb18984c73d129a2848e8088e747a0926490ce6c7cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -2,24 +2,24 @@ importance: 5 | ||
| --- | ||
| # Improved tooltip behavior | ||
| # Comportamento del tooltip migliorato | ||
| Write JavaScript that shows a tooltip over an element with the attribute `data-tooltip`. The value of this attribute should become the tooltip text. | ||
| 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. | ||
| That's like the task <info:task/behavior-tooltip>, but here the annotated elements can be nested. The most deeply nested tooltip is shown. | ||
| 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. | ||
pierangelomiceli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Only one tooltip may show up at the same time. | ||
| Può essere mostrato solo un tooltip alla volta. | ||
pierangelomiceli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| For instance: | ||
| Per esempio: | ||
| ```html | ||
| <div data-tooltip="Here – is the house interior" id="house"> | ||
| <div data-tooltip="Here – is the roof" id="roof"></div> | ||
| <div data-tooltip="Qui – l'interno della casa" id="house"> | ||
| <div data-tooltip="Qui – il tetto" id="roof"></div> | ||
| ... | ||
| <a href="https://en.wikipedia.org/wiki/The_Three_Little_Pigs" data-tooltip="Read on…">Hover over me</a> | ||
| <a href="https://en.wikipedia.org/wiki/The_Three_Little_Pigs" data-tooltip="Leggi…">Hover su di me</a> | ||
| </div> | ||
| ``` | ||
| The result in iframe: | ||
| Il risultato dell'iframe: | ||
| [iframe src="solution" height=300 border=1] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,18 @@ | ||
| The algorithm looks simple: | ||
| 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. | ||
| 2. When a mouse cursor entered the element, start measuring the speed on `mousemove`. | ||
| 3. If the speed is slow, then run `over`. | ||
| 4. When we're going out of the element, and `over` was executed, run `out`. | ||
| L'algoritmo è semplice: | ||
| 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. | ||
| 2. Quando il puntatore è entrato dentro l'elemento, si comincia a misurare la velocità al `mousemove`. | ||
| 3. Se la velocità è lenta, eseguire `over`. | ||
| 4. Quando si esce fuori dall'elemento, ed è stato eseguito `over`, eseguire `out`. | ||
| But how to measure the speed? | ||
| Ma come misurare la velocità? | ||
| 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. | ||
| 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. | ||
| Unfortunately, there's no way to get "current mouse coordinates" in JavaScript. There's no function like `getCurrentMouseCoordinates()`. | ||
| Sfortunatamente, non c'è modo di ricavare "le coordinate attuali del mouse" in JavaScript. Non esistono funzioni come `getCurrentMouseCoordinates()`. | ||
| The only way to get coordinates is to listen for mouse events, like `mousemove`, and take coordinates from the event object. | ||
| L'unico modo è di mettersi in ascolto sugli eventi del mouse, come `mousemove`, e prendere le coordinate dall'oggetto evento. | ||
| So let's set a handler on `mousemove` to track coordinates and remember them. And then compare them, once per `100ms`. | ||
| Quindi impostiamo un gestore su `mousemove` per tenere traccia delle coordinate e memorizzarle, per poi confrontarle ogni `100ms`. | ||
| P.S. Please note: the solution tests use `dispatchEvent` to see if the tooltip works right. | ||
| P.S.: Nota bene: i test della soluzione fanno uso di `dispatchEvent` per vedere se il tooltip funziona bene. | ||
pierangelomiceli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. pierangelomiceli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4,28 +4,28 @@ importance: 5 | ||
| # "Smart" tooltip | ||
| Write a function that shows a tooltip over an element only if the visitor moves the mouse *to it*, but not *through it*. | ||
| 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*. | ||
| 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? | ||
| 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? | ||
pierangelomiceli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| 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. | ||
| 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. | ||
pierangelomiceli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Make a universal object `new HoverIntent(options)` for it. | ||
| Creare un oggetto universale `new HoverIntent(options)` utile allo scopo. | ||
| Its `options`: | ||
| - `elem` -- element to track. | ||
| - `over` -- a function to call if the mouse came to the element: that is, it moves slowly or stopped over it. | ||
| - `out` -- a function to call when the mouse leaves the element (if `over` was called). | ||
| Le opzioni possibili `options`: | ||
| - `elem` -- elemento da tracciare. | ||
| - `over` -- una funzione da chiamare se il mouse arriva sull'elemento: ossia, se si muove lentamente o se si ferma sull'elemento. | ||
| - `out` -- una funzione da chiamare quando il mouse abbandona l'elemento (se è stato chiamato `over`). | ||
| An example of using such object for the tooltip: | ||
| Ecco un esempio dell'uso di questo oggetto per il tooltip: | ||
pierangelomiceli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ```js | ||
| // a sample tooltip | ||
| // un tooltip di esempio | ||
pierangelomiceli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| let tooltip = document.createElement('div'); | ||
| tooltip.className = "tooltip"; | ||
| tooltip.innerHTML = "Tooltip"; | ||
| // the object will track mouse and call over/out | ||
| // l'oggetto tiene traccia del mouse e chiama over/out | ||
| new HoverIntent({ | ||
| elem, | ||
| over() { | ||
| @@ -39,10 +39,10 @@ new HoverIntent({ | ||
| }); | ||
| ``` | ||
| The demo: | ||
| La demo: | ||
| [iframe src="solution" height=140] | ||
| 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. | ||
| Muovendo il mouse oltre la velocità di "clock" non succede nulla, facendolo lentamente o fermandocisi sopra, viene mostrato il tooltip. | ||
| Please note: the tooltip doesn't "blink" when the cursor moves between the clock subelements. | ||
| Nota bene: il tooltip non "lampeggia" quando il cursore si muove tra i sottoelementi dell'orologio. | ||
pierangelomiceli marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.