diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/solution.md b/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/solution.md index 6d8878d4a..271b97355 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/solution.md +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/solution.md @@ -1,5 +1,5 @@ -As we can see from HTML/CSS, the slider is a `
` with a colored background, that contains a runner -- another `
` with `position:relative`. +Come possiamo notare dall'HTML/CSS, lo slider è un `
` con sfondo colorato, che contiene un altro `
` con `position:relative`. -To position the runner we use `position:relative`, to provide the coordinates relative to its parent, here it's more convenient here than `position:absolute`. +Per posizionare il cursore useremo `position:relative`, per fornire le coordinate relative al genitore, ed in questo caso è meglio usarlo al posto di `position:absolute`. -Then we implement horizontal-only Drag'n'Drop with limitation by width. +Quindi andremo a implementare il Drag'n'Drop esclusivamente orizzontale con il vincolo sulla larghezza. diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/solution.view/index.html b/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/solution.view/index.html index c364b42d9..90b638600 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/solution.view/index.html +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/solution.view/index.html @@ -16,10 +16,10 @@ let thumb = slider.querySelector('.thumb'); thumb.onmousedown = function(event) { - event.preventDefault(); // prevent selection start (browser action) + event.preventDefault(); // previene dal cominciare la selezione (browser action) let shiftX = event.clientX - thumb.getBoundingClientRect().left; - // shiftY not needed, the thumb moves only horizontally + // shiftY non necessario, la thumb si muove solo orizzontalmente document.addEventListener('mousemove', onMouseMove); document.addEventListener('mouseup', onMouseUp); @@ -27,7 +27,7 @@ function onMouseMove(event) { let newLeft = event.clientX - shiftX - slider.getBoundingClientRect().left; - // the pointer is out of slider => lock the thumb within the bounaries + // il puntatore è fuori dallo slider => blocca la thumb all'interno dei confini if (newLeft < 0) { newLeft = 0; } diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/source.view/index.html b/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/source.view/index.html index a9a545c09..e9144b13a 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/source.view/index.html +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/source.view/index.html @@ -13,7 +13,7 @@
diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/task.md b/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/task.md index 0c6da4e2c..596bac498 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/task.md +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/task.md @@ -4,13 +4,13 @@ importance: 5 # Slider -Create a slider: +Creare uno cursore a scorrimento: [iframe src="solution" height=60 border=1] -Drag the blue thumb with the mouse and move it. +Trascinare il cursore blu con il mouse e spostarlo. -Important details: +Dettagli importanti: -- When the mouse button is pressed, during the dragging the mouse may go over or below the slider. The slider will still work (convenient for the user). -- If the mouse moves very fast to the left or to the right, the thumb should stop exactly at the edge. +- Quando si preme il pulsante del mouse, durante il trascinamento il mouse dovrebbe poter andare sotto o sopra il cursore, senza interromperlo (comodo per l'utente). +- Se il mouse si muove molto velocemente da sinistra a destra, il cursore si dovrebbe fermare esattamente sul bordo. diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.md b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.md index 62cbdb9c5..8a71f0c7f 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.md +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.md @@ -1,5 +1,5 @@ -To drag the element we can use `position:fixed`, it makes coordinates easier to manage. At the end we should switch it back to `position:absolute` to lay the element into the document. +Per trascinare l'elemento possiamo usare `position:fixed`, il quale semplifica la gestione delle coordinate. Alla fine dovremmo tornare ad utilizzare `position:absolute` per posizionare l'elemento all'interno del document. -When coordinates are at window top/bottom, we use `window.scrollTo` to scroll it. +Se le coordinate sono in alto/basso rispetto alla finestra, useremo `window.scrollTo` per scrollare la pagina fino al punto desiderato. -More details in the code, in comments. +Maggiori informazioni nel codice, tra i commenti. diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/index.html b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/index.html index d79ef30a4..ae0ea06cc 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/index.html +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/index.html @@ -8,17 +8,17 @@ -

Place superheroes around the soccer field.

+

Posizionare i supereroi nel campo di calcio.

-

Superheroes and the ball are elements with the class "draggable". Make them really draggable.

+

I supereroi ed il pallone sono elementi che hanno la classe "draggable". Renderli realmente trascinabili.

-

Important: limit dragging by the window. If a draggable reaches window top or bottom, then the page should scroll to let us drag it further.

+

Importante: limitare il trascinamento nella finestra. Se un oggetto trascinabile arriva sulla parte alta o bassa dello schermo la pagina dovrebbe scrollare per permettere di proseguire il trascinamento.

-

If your screen is big enough to fit the whole document -- make the window smaller to get vertical scrolling, so that you could test it.

+

Se lo schermo è abbastanza grande da contenere l'intero documento, rendere la finestra più piccola per poter attivare lo scrolling verticale, in modo da poterlo testare.

-

In this task it's enough to handle vertical scrolling. There's no horizontal scrolling usually, and it's handled the similar way if needed.

+

Per questa attività, gestire lo scrolling verticale è sufficiente. Quello orizzontale solitamente non è previsto, ma verrebbe gestito in modo simile nel caso servisse.

-

And one more thing: heroes may never leave the page. If they reach the edge of the document, no dragging outside of it.

+

E ancora: gli eroi non dovrebbero mai lasciare l'area della pagina. Se arrivassero al bordo del documento, non dovrebbero essere trascinati all'esterno.

diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/soccer.css b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/soccer.css index 00d5d70fc..ee268e01a 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/soccer.css +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/soccer.css @@ -10,7 +10,7 @@ html, body { float: left; } -/* heroes and the ball (dragables) */ +/* eroi e pallone (draggables) */ .hero { background: url(https://js.cx/drag-heroes/heroes.png); diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/soccer.js b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/soccer.js index 10ae2eeed..94025b4e5 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/soccer.js +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/soccer.js @@ -24,9 +24,9 @@ document.addEventListener('mousedown', function(event) { moveAt(event.clientX, event.clientY); } - // on drag start: - // remember the initial shift - // move the element position:fixed and a direct child of body + // all'inizio del trascinamento: + // memorizza lo spostamento iniziale + // sposta l'elemento con position:fixed come diretto figlio del body function startDrag(element, clientX, clientY) { if(isDragging) { return; @@ -45,7 +45,7 @@ document.addEventListener('mousedown', function(event) { moveAt(clientX, clientY); }; - // switch to absolute coordinates at the end, to fix the element in the document + // cambia su coordinate absolute alla fine, per fissare l'elemento nel docuemnto function finishDrag() { if(!isDragging) { return; @@ -61,49 +61,49 @@ document.addEventListener('mousedown', function(event) { } function moveAt(clientX, clientY) { - // new window-relative coordinates + // nuove coordinate relative alla window let newX = clientX - shiftX; let newY = clientY - shiftY; - // check if the new coordinates are below the bottom window edge - let newBottom = newY + dragElement.offsetHeight; // new bottom + // controlla se le nuove coordinate sono sotto il bordo della finestra + let newBottom = newY + dragElement.offsetHeight; // nuovo bottom - // below the window? let's scroll the page + // sotto la finestra? scrolliamo la pagina if (newBottom > document.documentElement.clientHeight) { - // window-relative coordinate of document end + // coordinate relative alla window della fine del documento let docBottom = document.documentElement.getBoundingClientRect().bottom; - // scroll the document down by 10px has a problem - // it can scroll beyond the end of the document - // Math.min(how much left to the end, 10) + // lo scrolling del documento giù di 10px porta il problema che + // può scrollare oltre la fine del document + // Math.min(quanto a sinistra dall fine, 10) let scrollY = Math.min(docBottom - newBottom, 10); - // calculations are imprecise, there may be rounding errors that lead to scrolling up - // that should be impossible, fix that here + // i calcoli sono imprecisi, perché potrebbero esserci errori di arrotondamento che portano a scollare in su + // la cui cosa dovrebbe essere impossibile, e quindi lo aggiustiamo if (scrollY < 0) scrollY = 0; window.scrollBy(0, scrollY); - // a swift mouse move make put the cursor beyond the document end - // if that happens - - // limit the new Y by the maximally possible (right at the bottom of the document) + // un movimento rapido del mouse porta il cursore oltre la fine del documento + // se succede + // limitiamo la nuova Y al massimo possibile (giusto poco sopra il documento) newY = Math.min(newY, document.documentElement.clientHeight - dragElement.offsetHeight); } - // check if the new coordinates are above the top window edge (similar logic) + // controlla se le nuove coordinate sono sopra i bordi superiori (logica simile) if (newY < 0) { - // scroll up + // scrolla in alto let scrollY = Math.min(-newY, 10); - if (scrollY < 0) scrollY = 0; // check precision errors + if (scrollY < 0) scrollY = 0; // controlla la precisione degli errori window.scrollBy(0, -scrollY); - // a swift mouse move can put the cursor beyond the document start - newY = Math.max(newY, 0); // newY may not be below 0 + // un movimento rapido del mouse porta il cursore oltre l'inizio del documento + newY = Math.max(newY, 0); // newY non dovrebbe essere meno di 0 } - // limit the new X within the window boundaries - // there's no scroll here so it's simple + // limita la nuova X all'interno dei limiti della finestra + // qui non abbiamo scrolling quindi è semplice if (newX < 0) newX = 0; if (newX > document.documentElement.clientWidth - dragElement.offsetWidth) { newX = document.documentElement.clientWidth - dragElement.offsetWidth; diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/index.html b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/index.html index d79ef30a4..a4c371da8 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/index.html +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/index.html @@ -8,17 +8,17 @@ -

Place superheroes around the soccer field.

+

Posizionare i supereroi nel campo di calcio.

-

Superheroes and the ball are elements with the class "draggable". Make them really draggable.

+

I supereroi ed il pallone sono elementi che hanno la classe "draggable". Renderli realmente trascinabili.

-

Important: limit dragging by the window. If a draggable reaches window top or bottom, then the page should scroll to let us drag it further.

+

Importante: limitare il trascinamento nella finestra. Se un oggetto trascinabile arriva sulla parte alta o bassa dello schermo, la pagina dovrebbe scrollare per permettere proseguire il trascinamento.

-

If your screen is big enough to fit the whole document -- make the window smaller to get vertical scrolling, so that you could test it.

+

Se lo schermo è abbastanza grande da contenere l'intero documento, rendere la finestra più piccola per poter attivare lo scrolling verticale, in modo da poterlo testare.

-

In this task it's enough to handle vertical scrolling. There's no horizontal scrolling usually, and it's handled the similar way if needed.

+

Per questa attività, gestire lo scrolling verticale è sufficiente. Quello orizzontale solitamente non è previsto, ma verrebbe gestito in modo simile nel caso servisse.

-

And one more thing: heroes may never leave the page. If they reach the edge of the document, no dragging outside of it.

+

E ancora: gli eroi non dovrebbero mai lasciare l'area della pagina. Se arrivassero al bordo del documento, non dovrebbero essere trascinati all'esterno.

diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/soccer.css b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/soccer.css index 00d5d70fc..ee268e01a 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/soccer.css +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/soccer.css @@ -10,7 +10,7 @@ html, body { float: left; } -/* heroes and the ball (dragables) */ +/* eroi e pallone (draggables) */ .hero { background: url(https://js.cx/drag-heroes/heroes.png); diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/soccer.js b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/soccer.js index ee04fda1c..6906f1aee 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/soccer.js +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/soccer.js @@ -1 +1 @@ -// Your code +// il tuo codice diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/task.md b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/task.md index 91fbaa0f2..a2e89e3b9 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/task.md +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/task.md @@ -2,19 +2,19 @@ importance: 5 --- -# Drag superheroes around the field +# Trascina i supereroi nel campo -This task can help you to check understanding of several aspects of Drag'n'Drop and DOM. +Questa attività può essere d'aiuto per comprendere vari aspetti del Drag'n'Drop e del DOM. -Make all elements with class `draggable` -- draggable. Like a ball in the chapter. +Rendere tutti gli elementi trascinabili, attraverso la classe `draggable`, come il pallone nel capitolo appena studiato. -Requirements: +Requisiti: -- Use event delegation to track drag start: a single event handler on `document` for `mousedown`. -- If elements are dragged to top/bottom window edges -- the page scrolls up/down to allow further dragging. -- There is no horizontal scroll (this makes the task a bit simpler, adding it is easy). -- Draggable elements or their parts should never leave the window, even after swift mouse moves. +- Utilizzare la event delegation per tenere traccia dell'inizio del trascinamento: un solo gestore evento sul `document` per il `mousedown`. +- Se gli elementi vengono trascinati verso i bordi in alto o in basso, la pagina deve scrollare di conseguenza. +- Lo scroll in orizzontale non è previsto, (il che rende l'attività più semplice, aggiungerlo è facile). +- Gli elementi trascinabili o le loro parti non devono mai uscire fuori dalla finestra, anche dopo movimenti veloci. -The demo is too big to fit it here, so here's the link. +La demo è troppo grande per essere inclusa qui, quindi ecco il link. [demo src="solution"] diff --git a/2-ui/3-event-details/4-mouse-drag-and-drop/article.md b/2-ui/3-event-details/4-mouse-drag-and-drop/article.md index 6cb1152c1..b2e6003ae 100644 --- a/2-ui/3-event-details/4-mouse-drag-and-drop/article.md +++ b/2-ui/3-event-details/4-mouse-drag-and-drop/article.md @@ -1,54 +1,54 @@ -# Drag'n'Drop with mouse events +# Drag'n'Drop con gli eventi del mouse -Drag'n'Drop is a great interface solution. Taking something and dragging and dropping it is a clear and simple way to do many things, from copying and moving documents (as in file managers) to ordering (dropping items into a cart). +Il Drag'n'Drop, in termini di interfaccia utente, è una soluzione grandiosa. Il fatto di poter prendere qualcosa, trascinarla e rilasciarla è un modo semplice ed intuitivo per fare tantissime operazioni, dal copiare e spostare documenti (come nei gestori di files), al fare un ordine online (rilasciando un prodotto in un carrello). -In the modern HTML standard there's a [section about Drag and Drop](https://html.spec.whatwg.org/multipage/interaction.html#dnd) with special events such as `dragstart`, `dragend`, and so on. +Nello standard HTML attuale c'è una [sezione sul Drag and Drop](https://html.spec.whatwg.org/multipage/interaction.html#dnd) con eventi speciali come `dragstart`, `dragend`, e via dicendo. -These events allow us to support special kinds of drag'n'drop, such as handling dragging a file from OS file-manager and dropping it into the browser window. Then JavaScript can access the contents of such files. +Questi eventi ci permettono di supportare tipi particolari di drag'n'drop, come gestire il trascinamento di un file dal sistema operativo ed il rilascio dentro la finestra del browser, in modo che JavaScript possa accedere al contenuto di tali files. -But native Drag Events also have limitations. For instance, we can't prevent dragging from a certain area. Also we can't make the dragging "horizontal" or "vertical" only. And there are many other drag'n'drop tasks that can't be done using them. Also, mobile device support for such events is very weak. +Ma i Drag Events hanno anche delle limitazioni. Ad esempio, non possiamo prevenire il trascinamento da una certa sezione. Inoltre non possiamo rendere il trascinamento solo "orizzontale" o "verticale". E ci sono tante altre azioni drag'n'drop che non è possibile sfruttare. Inoltre il supporto dei dispositivi mobile per questo tipo di eventi è abbastanza scarso. -So here we'll see how to implement Drag'n'Drop using mouse events. +Di conseguenza, vedremo come implementare il Drag'n'Drop solo tramite l'utilizzo degli eventi del mouse. -## Drag'n'Drop algorithm +## Algortimo del Drag'n'Drop -The basic Drag'n'Drop algorithm looks like this: +L'algoritmo di base del Drag'n'Drop è qualcosa del genere: -1. On `mousedown` - prepare the element for moving, if needed (maybe create a clone of it, add a class to it or whatever). -2. Then on `mousemove` move it by changing `left/top` with `position:absolute`. -3. On `mouseup` - perform all actions related to finishing the drag'n'drop. +1. Al `mousedown` - prepara l'elemento per lo spostamento, se necessario (magari crea un suo clone, o aggiungi una classe o altro). +2. Quindi al `mousemove` spostalo variando `left/top` con il `position:absolute`. +3. Al `mouseup` - esegui tutte le azioni coinvolte per completare il drag'n'drop. -These are the basics. Later we'll see how to other features, such as highlighting current underlying elements while we drag over them. +Queste sono le basi. Dopo vedremo come gestire altre caratteristiche, come evidenziare gli altri elementi sottostanti mentre effettuiamo il trascinamento su di essi. -Here's the implementation of dragging a ball: +Ecco un'implementazione del trascinamento di un pallone: ```js ball.onmousedown = function(event) { - // (1) prepare to moving: make absolute and on top by z-index + // (1) preparazione dello spostamento: imposta il posizionamento assoluto e lo z-index al massimo valore utile ball.style.position = 'absolute'; ball.style.zIndex = 1000; - // move it out of any current parents directly into body - // to make it positioned relative to the body + // spostamento all'esterno da ogni elemento genitore e direttamente verso il body della pagina + // per posizionarlo relativamente ad esso document.body.append(ball); - // centers the ball at (pageX, pageY) coordinates + // centratura del pallone alle coordinate (pageX, pageY) function moveAt(pageX, pageY) { ball.style.left = pageX - ball.offsetWidth / 2 + 'px'; ball.style.top = pageY - ball.offsetHeight / 2 + 'px'; } - // move our absolutely positioned ball under the pointer + // spostamento del nostro pallone con posizionamento assoluto sotto il puntatore moveAt(event.pageX, event.pageY); function onMouseMove(event) { moveAt(event.pageX, event.pageY); } - // (2) move the ball on mousemove + // (2) spostamento del pallone al mousemove document.addEventListener('mousemove', onMouseMove); - // (3) drop the ball, remove unneeded handlers + // (3) rilascio del pallone, rimozione dei gestori non necessari ball.onmouseup = function() { document.removeEventListener('mousemove', onMouseMove); ball.onmouseup = null; @@ -57,19 +57,19 @@ ball.onmousedown = function(event) { }; ``` -If we run the code, we can notice something strange. On the beginning of the drag'n'drop, the ball "forks": we start dragging its "clone". +Eseguendo il codice, noteremo qualcosa di anomalo. All'inizio del drag'n'drop, il pallone si "duplica": abbiamo cominciato trascinando il suo "clone". ```online -Here's an example in action: +Ecco un esempio in azione: [iframe src="ball" height=230] -Try to drag'n'drop with the mouse and you'll see such behavior. +Prova a fare il drag'n'drop con il mouse per vedere questo comportamento anomalo. ``` -That's because the browser has its own drag'n'drop support for images and some other elements. It runs automatically and conflicts with ours. +Questo accade perché il browser ha una sua implementazione del drag'n'drop per immagini e qualche altro elemento. La esegue in automatico e va quindi in conflitto con la nostra. -To disable it: +Per disabilitarlo: ```js ball.ondragstart = function() { @@ -77,42 +77,42 @@ ball.ondragstart = function() { }; ``` -Now everything will be all right. +Ora è tutto a posto. ```online -In action: +In azione: [iframe src="ball2" height=230] ``` -Another important aspect -- we track `mousemove` on `document`, not on `ball`. From the first sight it may seem that the mouse is always over the ball, and we can put `mousemove` on it. +Un altro aspetto importante -- noi teniamo traccia di `mousemove` nel `document`, non su `ball`. A prima vista potrebbe sembrare che il mouse sia sempre sopra il pallone, e possiamo attivare `mousemove` su di essa. -But as we remember, `mousemove` triggers often, but not for every pixel. So after swift move the pointer can jump from the ball somewhere in the middle of document (or even outside of the window). + Ma come noto, `mousemove` viene generato spesso, ma non per ogni pixel. Quindi a seguito di qualche movimento rapido potrebbe saltare dal pallone a qualche punto nel bel mezzo del documento (o anche fuori dalla finestra). -So we should listen on `document` to catch it. +Di conseguenza dovremmo metterci in ascolto sul `document` per la cattura. -## Correct positioning +## Posizionamento corretto -In the examples above the ball is always moved so, that it's center is under the pointer: +Negli esempi precedenti il pallone viene sempre spostato in modo che il suo centro sia sotto il puntatore: ```js ball.style.left = pageX - ball.offsetWidth / 2 + 'px'; ball.style.top = pageY - ball.offsetHeight / 2 + 'px'; ``` -Not bad, but there's a side-effect. To initiate the drag'n'drop, we can `mousedown` anywhere on the ball. But if "take" it from its edge, then the ball suddenly "jumps" to become centered under the mouse pointer. +Non male, ma c'è un effetto collaterale. Per innescare il drag'n'drop, potremmo cominciare `mousedown` da un punto qualunque del pallone. Ma se lo "prendessimo" dai bordi, si centrerebbe repentinamente sotto il puntatore facendo una specie di "salto". -It would be better if we keep the initial shift of the element relative to the pointer. +Sarebbe meglio se mantenessimo lo spostamento iniziale dell'elemento rispetto al puntatore. -For instance, if we start dragging by the edge of the ball, then the pointer should remain over the edge while dragging. +Ad esempio, se cominciassimo dal bordo del pallone, il puntatore dovrebbe rimanere sul bordo mentre lo trasciniamo. ![](ball_shift.svg) -Let's update our algorithm: +Aggiorniamo il nostro algoritmo: -1. When a visitor presses the button (`mousedown`) - remember the distance from the pointer to the left-upper corner of the ball in variables `shiftX/shiftY`. We'll keep that distance while dragging. +1. Quando un utente preme il pulsante (`mousedown`) - memorizza la distanza del puntatore dall'angolo in alto a sinistra del pallone nelle variabili `shiftX/shiftY`. Manterremo questa distanza durante il trascinamento. - To get these shifts we can substract the coordinates: + Per ottenere questi scostamenti possiamo sottrarre le coordinate: ```js // onmousedown @@ -120,16 +120,16 @@ Let's update our algorithm: let shiftY = event.clientY - ball.getBoundingClientRect().top; ``` -2. Then while dragging we position the ball on the same shift relative to the pointer, like this: +2. Quindi, durante il trascinamento posizioneremo il pallone con lo stesso scostamento relativo al puntatore, in questo modo: ```js // onmousemove - // ball has position:absoute + // il pallone ha position:absoute ball.style.left = event.pageX - *!*shiftX*/!* + 'px'; ball.style.top = event.pageY - *!*shiftY*/!* + 'px'; ``` -The final code with better positioning: +Il codice definitivo con un posizionamento ottimale: ```js ball.onmousedown = function(event) { @@ -145,8 +145,8 @@ ball.onmousedown = function(event) { moveAt(event.pageX, event.pageY); - // moves the ball at (pageX, pageY) coordinates - // taking initial shifts into account + // sposta il pallone alle coordinate (pageX, pageY) + // tenendo conto dello scostamento iniziale function moveAt(pageX, pageY) { ball.style.left = pageX - *!*shiftX*/!* + 'px'; ball.style.top = pageY - *!*shiftY*/!* + 'px'; @@ -156,10 +156,10 @@ ball.onmousedown = function(event) { moveAt(event.pageX, event.pageY); } - // move the ball on mousemove + // muovi il pallone al mousemove document.addEventListener('mousemove', onMouseMove); - // drop the ball, remove unneeded handlers + // rilascia il pallone, rimuovi i gestori non necessari ball.onmouseup = function() { document.removeEventListener('mousemove', onMouseMove); ball.onmouseup = null; @@ -173,32 +173,32 @@ ball.ondragstart = function() { ``` ```online -In action (inside `