Skip to content

Commit 166c6e6

Browse files
authored
Merge pull request #252 from pierangelomiceli/017_dnd
Drag'n'Drop with mouse events
2 parents b29198c + 7d7ab04 commit 166c6e6

File tree

14 files changed

+161
-161
lines changed

14 files changed

+161
-161
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
As we can see from HTML/CSS, the slider is a `<div>` with a colored background, that contains a runner -- another `<div>` with `position:relative`.
1+
Come possiamo notare dall'HTML/CSS, lo slider è un `<div>` con sfondo colorato, che contiene un altro `<div>` con `position:relative`.
22

3-
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`.
3+
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`.
44

5-
Then we implement horizontal-only Drag'n'Drop with limitation by width.
5+
Quindi andremo a implementare il Drag'n'Drop esclusivamente orizzontale con il vincolo sulla larghezza.

2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/solution.view/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616
let thumb = slider.querySelector('.thumb');
1717

1818
thumb.onmousedown = function(event) {
19-
event.preventDefault(); // prevent selection start (browser action)
19+
event.preventDefault(); // previene dal cominciare la selezione (browser action)
2020

2121
let shiftX = event.clientX - thumb.getBoundingClientRect().left;
22-
// shiftY not needed, the thumb moves only horizontally
22+
// shiftY non necessario, la thumb si muove solo orizzontalmente
2323

2424
document.addEventListener('mousemove', onMouseMove);
2525
document.addEventListener('mouseup', onMouseUp);
2626

2727
function onMouseMove(event) {
2828
let newLeft = event.clientX - shiftX - slider.getBoundingClientRect().left;
2929

30-
// the pointer is out of slider => lock the thumb within the bounaries
30+
// il puntatore è fuori dallo slider => blocca la thumb all'interno dei confini
3131
if (newLeft < 0) {
3232
newLeft = 0;
3333
}

2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/source.view/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</div>
1414

1515
<script>
16-
// ...your code...
16+
// ...il tuo codice...
1717
</script>
1818

1919
</body>

2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/task.md

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

55
# Slider
66

7-
Create a slider:
7+
Creare uno cursore a scorrimento:
88

99
[iframe src="solution" height=60 border=1]
1010

11-
Drag the blue thumb with the mouse and move it.
11+
Trascinare il cursore blu con il mouse e spostarlo.
1212

13-
Important details:
13+
Dettagli importanti:
1414

15-
- 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).
16-
- If the mouse moves very fast to the left or to the right, the thumb should stop exactly at the edge.
15+
- 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).
16+
- Se il mouse si muove molto velocemente da sinistra a destra, il cursore si dovrebbe fermare esattamente sul bordo.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
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.
1+
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.
22

3-
When coordinates are at window top/bottom, we use `window.scrollTo` to scroll it.
3+
Se le coordinate sono in alto/basso rispetto alla finestra, useremo `window.scrollTo` per scrollare la pagina fino al punto desiderato.
44

5-
More details in the code, in comments.
5+
Maggiori informazioni nel codice, tra i commenti.

2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88

99
<body>
1010

11-
<h2>Place superheroes around the soccer field.</h2>
11+
<h2>Posizionare i supereroi nel campo di calcio.</h2>
1212

13-
<p>Superheroes and the ball are elements with the class "draggable". Make them really draggable.</p>
13+
<p>I supereroi ed il pallone sono elementi che hanno la classe "draggable". Renderli realmente trascinabili.</p>
1414

15-
<p>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.</p>
15+
<p>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.</p>
1616

17-
<p>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.</p>
17+
<p>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.</p>
1818

19-
<p>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.</p>
19+
<p>Per questa attività, gestire lo scrolling verticale è sufficiente. Quello orizzontale solitamente non è previsto, ma verrebbe gestito in modo simile nel caso servisse.</p>
2020

21-
<p>And one more thing: heroes may never leave the page. If they reach the edge of the document, no dragging outside of it.</p>
21+
<p>E ancora: gli eroi non dovrebbero mai lasciare l'area della pagina. Se arrivassero al bordo del documento, non dovrebbero essere trascinati all'esterno.</p>
2222

2323
<div id="field">
2424

2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/soccer.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ html, body {
1010
float: left;
1111
}
1212

13-
/* heroes and the ball (dragables) */
13+
/* eroi e pallone (draggables) */
1414

1515
.hero {
1616
background: url(https://js.cx/drag-heroes/heroes.png);

2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/solution.view/soccer.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ document.addEventListener('mousedown', function(event) {
2424
moveAt(event.clientX, event.clientY);
2525
}
2626

27-
// on drag start:
28-
// remember the initial shift
29-
// move the element position:fixed and a direct child of body
27+
// all'inizio del trascinamento:
28+
// memorizza lo spostamento iniziale
29+
// sposta l'elemento con position:fixed come diretto figlio del body
3030
function startDrag(element, clientX, clientY) {
3131
if(isDragging) {
3232
return;
@@ -45,7 +45,7 @@ document.addEventListener('mousedown', function(event) {
4545
moveAt(clientX, clientY);
4646
};
4747

48-
// switch to absolute coordinates at the end, to fix the element in the document
48+
// cambia su coordinate absolute alla fine, per fissare l'elemento nel docuemnto
4949
function finishDrag() {
5050
if(!isDragging) {
5151
return;
@@ -61,49 +61,49 @@ document.addEventListener('mousedown', function(event) {
6161
}
6262

6363
function moveAt(clientX, clientY) {
64-
// new window-relative coordinates
64+
// nuove coordinate relative alla window
6565
let newX = clientX - shiftX;
6666
let newY = clientY - shiftY;
6767

68-
// check if the new coordinates are below the bottom window edge
69-
let newBottom = newY + dragElement.offsetHeight; // new bottom
68+
// controlla se le nuove coordinate sono sotto il bordo della finestra
69+
let newBottom = newY + dragElement.offsetHeight; // nuovo bottom
7070

71-
// below the window? let's scroll the page
71+
// sotto la finestra? scrolliamo la pagina
7272
if (newBottom > document.documentElement.clientHeight) {
73-
// window-relative coordinate of document end
73+
// coordinate relative alla window della fine del documento
7474
let docBottom = document.documentElement.getBoundingClientRect().bottom;
7575

76-
// scroll the document down by 10px has a problem
77-
// it can scroll beyond the end of the document
78-
// Math.min(how much left to the end, 10)
76+
// lo scrolling del documento giù di 10px porta il problema che
77+
// può scrollare oltre la fine del document
78+
// Math.min(quanto a sinistra dall fine, 10)
7979
let scrollY = Math.min(docBottom - newBottom, 10);
8080

81-
// calculations are imprecise, there may be rounding errors that lead to scrolling up
82-
// that should be impossible, fix that here
81+
// i calcoli sono imprecisi, perché potrebbero esserci errori di arrotondamento che portano a scollare in su
82+
// la cui cosa dovrebbe essere impossibile, e quindi lo aggiustiamo
8383
if (scrollY < 0) scrollY = 0;
8484

8585
window.scrollBy(0, scrollY);
8686

87-
// a swift mouse move make put the cursor beyond the document end
88-
// if that happens -
89-
// limit the new Y by the maximally possible (right at the bottom of the document)
87+
// un movimento rapido del mouse porta il cursore oltre la fine del documento
88+
// se succede
89+
// limitiamo la nuova Y al massimo possibile (giusto poco sopra il documento)
9090
newY = Math.min(newY, document.documentElement.clientHeight - dragElement.offsetHeight);
9191
}
9292

93-
// check if the new coordinates are above the top window edge (similar logic)
93+
// controlla se le nuove coordinate sono sopra i bordi superiori (logica simile)
9494
if (newY < 0) {
95-
// scroll up
95+
// scrolla in alto
9696
let scrollY = Math.min(-newY, 10);
97-
if (scrollY < 0) scrollY = 0; // check precision errors
97+
if (scrollY < 0) scrollY = 0; // controlla la precisione degli errori
9898

9999
window.scrollBy(0, -scrollY);
100-
// a swift mouse move can put the cursor beyond the document start
101-
newY = Math.max(newY, 0); // newY may not be below 0
100+
// un movimento rapido del mouse porta il cursore oltre l'inizio del documento
101+
newY = Math.max(newY, 0); // newY non dovrebbe essere meno di 0
102102
}
103103

104104

105-
// limit the new X within the window boundaries
106-
// there's no scroll here so it's simple
105+
// limita la nuova X all'interno dei limiti della finestra
106+
// qui non abbiamo scrolling quindi è semplice
107107
if (newX < 0) newX = 0;
108108
if (newX > document.documentElement.clientWidth - dragElement.offsetWidth) {
109109
newX = document.documentElement.clientWidth - dragElement.offsetWidth;

2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88

99
<body>
1010

11-
<h2>Place superheroes around the soccer field.</h2>
11+
<h2>Posizionare i supereroi nel campo di calcio.</h2>
1212

13-
<p>Superheroes and the ball are elements with the class "draggable". Make them really draggable.</p>
13+
<p>I supereroi ed il pallone sono elementi che hanno la classe "draggable". Renderli realmente trascinabili.</p>
1414

15-
<p>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.</p>
15+
<p>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.</p>
1616

17-
<p>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.</p>
17+
<p>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.</p>
1818

19-
<p>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.</p>
19+
<p>Per questa attività, gestire lo scrolling verticale è sufficiente. Quello orizzontale solitamente non è previsto, ma verrebbe gestito in modo simile nel caso servisse.</p>
2020

21-
<p>And one more thing: heroes may never leave the page. If they reach the edge of the document, no dragging outside of it.</p>
21+
<p>E ancora: gli eroi non dovrebbero mai lasciare l'area della pagina. Se arrivassero al bordo del documento, non dovrebbero essere trascinati all'esterno.</p>
2222

2323
<div id="field">
2424

2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/source.view/soccer.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ html, body {
1010
float: left;
1111
}
1212

13-
/* heroes and the ball (dragables) */
13+
/* eroi e pallone (draggables) */
1414

1515
.hero {
1616
background: url(https://js.cx/drag-heroes/heroes.png);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
// Your code
1+
// il tuo codice

2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/task.md

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

33
---
44

5-
# Drag superheroes around the field
5+
# Trascina i supereroi nel campo
66

7-
This task can help you to check understanding of several aspects of Drag'n'Drop and DOM.
7+
Questa attività può essere d'aiuto per comprendere vari aspetti del Drag'n'Drop e del DOM.
88

9-
Make all elements with class `draggable` -- draggable. Like a ball in the chapter.
9+
Rendere tutti gli elementi trascinabili, attraverso la classe `draggable`, come il pallone nel capitolo appena studiato.
1010

11-
Requirements:
11+
Requisiti:
1212

13-
- Use event delegation to track drag start: a single event handler on `document` for `mousedown`.
14-
- If elements are dragged to top/bottom window edges -- the page scrolls up/down to allow further dragging.
15-
- There is no horizontal scroll (this makes the task a bit simpler, adding it is easy).
16-
- Draggable elements or their parts should never leave the window, even after swift mouse moves.
13+
- Utilizzare la event delegation per tenere traccia dell'inizio del trascinamento: un solo gestore evento sul `document` per il `mousedown`.
14+
- Se gli elementi vengono trascinati verso i bordi in alto o in basso, la pagina deve scrollare di conseguenza.
15+
- Lo scroll in orizzontale non è previsto, (il che rende l'attività più semplice, aggiungerlo è facile).
16+
- Gli elementi trascinabili o le loro parti non devono mai uscire fuori dalla finestra, anche dopo movimenti veloci.
1717

18-
The demo is too big to fit it here, so here's the link.
18+
La demo è troppo grande per essere inclusa qui, quindi ecco il link.
1919

2020
[demo src="solution"]

0 commit comments

Comments
 (0)