◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

We have a horizontal Drag'n'Drop here.

To position the element we use `position:relative` and slider-relative coordinates for the thumb. Here it's more convenient here than `position:absolute`.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
let thumb = slider.querySelector('.thumb');

thumb.onmousedown = function(event) {
event.preventDefault(); // prevent selection start (browser action)

let shiftX = event.clientX - thumb.getBoundingClientRect().left;
// shiftY not needed, the thumb moves only horizontally

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

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

// the pointer is out of slider => lock the thumb within the bounaries
if (newLeft < 0) {
newLeft = 0;
}
Expand Down
12 changes: 6 additions & 6 deletions 2-ui/3-event-details/4-mouse-drag-and-drop/1-slider/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ importance: 5

---

# Slider

Create a slider:

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

Drag the blue thumb with the mouse and move it.

Important details:

- 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.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
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`.

Then, when coordinates are at window top/bottom, we use `window.scrollTo` to scroll it.

More details in the code, in comments.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ html, body {
float: left;
}

/* heroes and the ball (dragables) */

.hero {
background: url(https://js.cx/drag-heroes/heroes.png);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ html, body {
float: left;
}

/* heroes and the ball (dragables) */

.hero {
background: url(https://js.cx/drag-heroes/heroes.png);
Expand Down
21 changes: 10 additions & 11 deletions 2-ui/3-event-details/4-mouse-drag-and-drop/2-drag-heroes/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ importance: 5

---

# Drag superheroes around the field

This task can help you to check understanding of several aspects of Drag'n'Drop and DOM.

Make all elements with class `draggable` -- draggable. Like a ball in the chapter.

Requirements:

- 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.
- Draggable elements should never leave the window, even after swift mouse moves.

The demo is too big to fit it here, so here's the link.

[demo src="solution"]
Loading
Toggle all file notes Toggle all file annotations