@@ -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+ // в начале перемещения элемента :
28+ // запоминаем место клика по элементу (shiftX, shiftY),
29+ // переключаем позиционирование элемента ( position:fixed) и двигаем элемент
3030 function startDrag ( element , clientX , clientY ) {
3131 if ( isDragging ) {
3232 return ;
@@ -45,7 +45,8 @@ 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+ // переключаемся обратно на абсолютные координаты
49+ // чтобы закрепить элемент относительно документа
4950 function finishDrag ( ) {
5051 if ( ! isDragging ) {
5152 return ;
@@ -61,49 +62,50 @@ document.addEventListener('mousedown', function(event) {
6162 }
6263
6364 function moveAt ( clientX , clientY ) {
64- // new window-relative coordinates
65+ // вычисляем новые координаты (относительно окна)
6566 let newX = clientX - shiftX ;
6667 let newY = clientY - shiftY ;
6768
68- // check if the new coordinates are below the bottom window edge
69- let newBottom = newY + dragElement . offsetHeight ; // new bottom
69+ // проверяем, не переходят ли новые координаты за нижний край окна:
70+ // сначала вычисляем гипотетический новый нижний край окна
71+ let newBottom = newY + dragElement . offsetHeight ;
7072
71- // below the window? let's scroll the page
73+ // затем, если новый край окна выходит за пределы документа, прокручиваем страницу
7274 if ( newBottom > document . documentElement . clientHeight ) {
73- // window-relative coordinate of document end
75+ // координата нижнего края документа относительно окна
7476 let docBottom = document . documentElement . getBoundingClientRect ( ) . bottom ;
7577
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)
78+ // простой скролл документа на 10px вниз имеет проблему -
79+ // он может прокручивать документ за его пределы,
80+ // поэтому используем Math.min(расстояние до конца , 10)
7981 let scrollY = Math . min ( docBottom - newBottom , 10 ) ;
8082
81- // calculations are imprecise, there may be rounding errors that lead to scrolling up
82- // that should be impossible, fix that here
83+ // вычисления могут быть не совсем точны - случаются ошибки при округлении,
84+ // которые приводят к отрицательному значению прокрутки. отфильтруем их:
8385 if ( scrollY < 0 ) scrollY = 0 ;
8486
8587 window . scrollBy ( 0 , scrollY ) ;
8688
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)
89+ // быстрое перемещение мыши может поместить курсор за пределы документа вниз
90+ // если это произошло -
91+ // ограничиваем новое значение Y максимально возможным исходя из размера документа:
9092 newY = Math . min ( newY , document . documentElement . clientHeight - dragElement . offsetHeight ) ;
9193 }
9294
93- // check if the new coordinates are above the top window edge (similar logic )
95+ // проверяем, не переходят ли новые координаты за верхний край окна (по схожему алгоритму )
9496 if ( newY < 0 ) {
95- // scroll up
97+ // прокручиваем окно вверх
9698 let scrollY = Math . min ( - newY , 10 ) ;
97- if ( scrollY < 0 ) scrollY = 0 ; // check precision errors
99+ if ( scrollY < 0 ) scrollY = 0 ; // проверяем ошибки точности
98100
99101 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
102+ // быстрое перемещение мыши может поместить курсор за пределы документа вверх
103+ newY = Math . max ( newY , 0 ) ; // newY не может быть меньше нуля
102104 }
103105
104106
105- // limit the new X within the window boundaries
106- // there's no scroll here so it's simple
107+ // ограничим newX размерами окна
108+ // согласно условию, горизонтальная прокрутка отсутствует, поэтому это не сложно:
107109 if ( newX < 0 ) newX = 0 ;
108110 if ( newX > document . documentElement . clientWidth - dragElement . offsetWidth ) {
109111 newX = document . documentElement . clientWidth - dragElement . offsetWidth ;
@@ -113,4 +115,4 @@ document.addEventListener('mousedown', function(event) {
113115 dragElement . style . top = newY + 'px' ;
114116 }
115117
116- } ) ;
118+ } ) ;
0 commit comments