When you click on an element, you need to start tracking the position of the mouse cursor, the place where you clicked must have coordinates 0; 0. Found a way to track the cursor position relative to the screen, but I don’t know how to do it relative to the pressed place. It is advisable to show on the basis of my code, since its already familiar and understandable))

position = false; $(document).mousedown(function() { position = true; }) $(document).mouseup(function() { position = false; }) document.onmousemove = mousemove; function mousemove(event) { if (position) { mouse_x = y = 0; if (document.attachEvent != null) { mouse_x = window.event.clientX; mouse_y = window.event.clientY; } else if (!document.attachEvent && document.addEventListener) { mouse_x = event.clientX; mouse_y = event.clientY; } console.log('x=' + mouse_x + '; ' + 'y=' + mouse_y); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <h1>НаТми ΠΈ Π΄Π΅Ρ€ΠΆΠΈ</h1> 

    1 answer 1

     position = false; startx = 0; starty = 0; $(document).mousedown(function(event) { position = true; startx = event.clientX; starty = event.clientY; }) $(document).mouseup(function() { position = false; }) document.onmousemove = mousemove; function mousemove(event) { if (position) { mouse_x = y = 0; if (document.attachEvent != null) { mouse_x = window.event.clientX; mouse_y = window.event.clientY; } else if (!document.attachEvent && document.addEventListener) { mouse_x = event.clientX; mouse_y = event.clientY; } console.log('x=' + (mouse_x - startx) + '; ' + 'y=' + (mouse_y - starty)); } } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <h1>НаТми ΠΈ Π΄Π΅Ρ€ΠΆΠΈ</h1>