How to connect html element to mouse using jQuery?

How to connect html element to mouse cursor using jQuery. It should be something like "draggable", but I want this element to snap to the cursor after double-clicking and to follow the cursor until the left mouse button is pressed.

+6
source share
1 answer

You want to use .mousemove() and .offset() .

 $("#clickedElement").dblclick(function () { var $someElement = $("#elementToCling"); $(document).mousemove(function (e) { $someElement.offset({ top: e.pageY, left: e.pageX }); }).click(function () { $(this).unbind("mousemove"); }); }); 

Working demo: http://jsfiddle.net/EbbxA/

+13
source

Source: https://habr.com/ru/post/901547/


All Articles