From 9c045cd205e77cd8dfa52f933867e4f2cc092cf1 Mon Sep 17 00:00:00 2001 From: Alfiya <33545574+alfiya-udc@users.noreply.github.com> Date: Fri, 31 May 2019 13:20:25 +0300 Subject: [PATCH 01/25] algorithm, positioning, detecting --- .../4-mouse-drag-and-drop/article.md | 146 +++++++++--------- 1 file changed, 73 insertions(+), 73 deletions(-) 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 6390b392ae..967b8ff17f 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,42 +1,42 @@ -# Drag'n'Drop with mouse events +# Drag'n'Drop с событиями мыши -Drag'n'Drop is a great interface solution. Taking something, dragging and dropping is a clear and simple way to do many things, from copying and moving (see file managers) to ordering (drop into cart). +Drag'n'Drop - отличный способ улучшить интерфейс. Захват элемента мышкой и его перенос визуально упростят что угодно: от копирования и перетаскивания в файловых менеджерах до оформления заказа в корзине. -In the modern HTML standard there's a [section about Drag Events](https://html.spec.whatwg.org/multipage/interaction.html#dnd). +В современном стандарте HTML5 есть [раздел о событиях мыши](https://html.spec.whatwg.org/multipage/interaction.html#dnd). -They are interesting, because they allow to solve simple tasks easily, and also allow to handle drag'n'drop of "external" files into the browser. So we can take a file in the OS file-manager and drop it into the browser window. Then JavaScript gains access to its contents. +Они интересны тем, что позволяют легко решать простые задачи. Например, можно перетащить файл в браузер, так что JS получит доступ к его содержимому. -But native Drag Events also have limitations. For instance, we can limit dragging by a certain area. Also we can't make it "horizontal" or "vertical" only. There are other drag'n'drop tasks that can't be implemented using that API. +Но у встроенных событий, входящих в Drag’n’Drop, также есть ограничения. Например, нельзя организовать перенос «только по горизонтали» или «только по вертикали». Также нельзя ограничить перенос внутри заданной зоны. Есть и другие интерфейсные задачи, которые такими встроенными событиями нереализуемы. -So here we'll see how to implement Drag'n'Drop using mouse events. Not that hard either. +Поэтому здесь мы будем рассматривать Drag’n’Drop при помощи событий мыши. Это не так уж сложно. ## Drag'n'Drop algorithm -The basic Drag'n'Drop algorithm looks like this: +Основной алгоритм Drag’n’Drop выглядит так: -1. Catch `mousedown` on a draggable element. -2. Prepare the element for moving (maybe create a copy of it or whatever). -3. Then on `mousemove` move it by changing `left/top` and `position:absolute`. -4. On `mouseup` (button release) -- perform all actions related to a finished Drag'n'Drop. +1. Отслеживаем событие `mousedown` на переносимом элементе. +2. При нажатии – готовим элемент к перемещению (например, создаём его копию). +3. При наступлении `mousemove` передвигаем элемент на новые координаты путём смены `left/top` и `position:absolute`. +4. На `mouseup` (при отпускании кнопки мыши) -- остановить перенос элемента и произвести все действия, связанные с окончанием Drag’n’Drop.. -These are the basics. We can extend it, for instance, by highlighting droppable (available for the drop) elements when hovering over them. +Это есть база Drag'n'Drop. Мы могли бы расширить этот алгоритм, например, подсветив элементы при наведении на них курсора. -Here's the algorithm for drag'n'drop of a ball: +В следующем примере эти шаги реализованы для переноса мяча: ```js -ball.onmousedown = function(event) { // (1) start the process +ball.onmousedown = function(event) { // (1) отследить нажатие - // (2) prepare to moving: make absolute and on top by z-index + // (2) подготовить к перемещению: разместить поверх остального содержимого и в абсолютных координатах 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 + // переместим в body, чтобы мяч был точно не внутри position:relative document.body.append(ball); - // ...and put that absolutely positioned ball under the cursor + // и установим абсолютно спозиционированный мяч под курсор moveAt(event.pageX, event.pageY); - // centers the ball at (pageX, pageY) coordinates + // передвинуть мяч под координаты курсора + // и сдвинуть на половину ширины/высоты для центрирования function moveAt(pageX, pageY) { ball.style.left = pageX - ball.offsetWidth / 2 + 'px'; ball.style.top = pageY - ball.offsetHeight / 2 + 'px'; @@ -46,10 +46,10 @@ ball.onmousedown = function(event) { // (1) start the process moveAt(event.pageX, event.pageY); } - // (3) move the ball on mousemove + // (3) перемещать по экрану document.addEventListener('mousemove', onMouseMove); - // (4) drop the ball, remove unneeded handlers + // (4) отследить окончание переноса, удалив обработчики событий ball.onmouseup = function() { document.removeEventListener('mousemove', onMouseMove); ball.onmouseup = null; @@ -58,19 +58,19 @@ ball.onmousedown = function(event) { // (1) start the process }; ``` -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". +Если запустить этот код, то мы заметим нечто странное. При начале переноса мяч «раздваивается» и переносится не сам мяч, а его «клон». ```online -Here's an example in action: +Это можно увидеть в действии: [iframe src="ball" height=230] -Try to drag'n'drop the mouse and you'll see the strange behavior. +Попробуйте перенести мяч мышкой и вы увидите описанное, довольно-таки странное, поведение. ``` -That's because the browser has its own Drag'n'Drop for images and some other elements that runs automatically and conflicts with ours. +Всё потому, что браузер имеет свой собственный Drag’n’Drop, который автоматически запускается и вступает в конфликт с нашим. Это происходит именно для картинок и некоторых других элементов. -To disable it: +Его нужно отключить: ```js ball.ondragstart = function() { @@ -78,40 +78,40 @@ ball.ondragstart = function() { }; ``` -Now everything will be all right. +Теперь всё будет в порядке. ```online -In action: +В действии: [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. +Ещё одна особенность правильного Drag’n’Drop -- событие `mousemove` отслеживается на `document`, а не на `ball`. С первого взгляда кажется, что мышь всегда над мячом и обработчик `mousemove` можно повесить на сам мяч, а не на документ.. -But as we remember, `mousemove` triggers often, but not for every pixel. So after swift move the cursor can jump from the ball somewhere in the middle of document (or even outside of the window). +Но как мы помним, событие `mousemove` возникает хоть и часто, но не для каждого пикселя. Поэтому из-за быстрого движения курсор может выпрыгнуть и оказаться где-нибудь в середине документа (или даже за пределами окна). -So we should listen on `document` to catch it. +Вот почему мы должны отслеживать `mousemove` на всём `document`. -## Correct positioning +## Правильное позиционирование -In the examples above the ball is always centered under the pointer: +В примерах выше мяч позиционируется в центре под курсором мыши: ```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 can we `mousedown` anywhere on the ball. If do it at the edge, then the ball suddenly "jumps" to become centered. +Неплохо, но есть побочные эффекты. В частности, в самом начале переноса, особенно если мячик «взят» за край – он резко «прыгает» центром под курсор мыши. -It would be better if we keep the initial shift of the element relative to the pointer. +Было бы лучше, если бы изначальный сдвиг курсора относительно элемента сохранялся. -For instance, if we start dragging by the edge of the ball, then the cursor should remain over the edge while dragging. +Где захватили, за ту «часть элемента» и переносим: ![](ball_shift.png) -1. When a visitor presses the button (`mousedown`) -- we can remember the distance from the cursor to the left-upper corner of the ball in variables `shiftX/shiftY`. We should keep that distance while dragging. +1. Когда человек нажимает на мячик (`mousedown`) -- мы можем зафиксировать расстояние от курсора до левого верхнего угла шара в переменных `shiftX/shiftY`. И нужно удерживать это расстояние при перетаскивании. - To get these shifts we can substract the coordinates: + Чтобы получить этот сдвиг, мы можем вычесть координаты: ```js // onmousedown @@ -119,9 +119,9 @@ For instance, if we start dragging by the edge of the ball, then the cursor shou let shiftY = event.clientY - ball.getBoundingClientRect().top; ``` - Please note that there's no method to get document-relative coordinates in JavaScript, so we use window-relative coordinates here. + Заметим, что в JavaScript нет способа получить координаты относительно документа, поэтому мы используем здесь координаты относительно окна. -2. Then while dragging we position the ball on the same shift relative to the pointer, like this: +2. Далее при переносе мяча мы позиционируем шар с учётом сдвига, например так: ```js // onmousemove @@ -130,7 +130,7 @@ For instance, if we start dragging by the edge of the ball, then the cursor shou ball.style.top = event.pageY - *!*shiftY*/!* + 'px'; ``` -The final code with better positioning: +Итоговый код с правильным позиционированием: ```js ball.onmousedown = function(event) { @@ -146,7 +146,7 @@ ball.onmousedown = function(event) { moveAt(event.pageX, event.pageY); - // centers the ball at (pageX, pageY) coordinates + // центрирует мяч по координатам (pageX, pageY) 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); } - // (3) move the ball on mousemove + // передвигает мяч на событии mousemove document.addEventListener('mousemove', onMouseMove); - // (4) drop the ball, remove unneeded handlers + // отпускает мяч, удаляя обработчики ball.onmouseup = function() { document.removeEventListener('mousemove', onMouseMove); ball.onmouseup = null; @@ -173,30 +173,30 @@ ball.ondragstart = function() { ``` ```online -In action (inside `