Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How attach html-element to mouse using jQuery?

How do I attach html-element to mouse cursor using jQuery. This should be something like 'draggable', but I want that element clung to the cursor after mouse double-click and to follow the cursor until the left mouse button is pressed.

like image 424
user1048261 Avatar asked Nov 15 '11 19:11

user1048261


People also ask

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.

What is Mousemove event in jQuery?

The mousemove event occurs whenever the mouse pointer moves within the selected element. The mousemove() method triggers the mousemove event, or attaches a function to run when a mousemove event occurs. Note: Each time a user moves the mouse one pixel, a mousemove event occurs.

How can create href link in jQuery?

JavaScript Code:ready(function(){ $('#button1'). click(function(){ $(". link"). attr('href','https://www.w3resource.com/'); }); });


1 Answers

You'll 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/

like image 171
gilly3 Avatar answered Sep 19 '22 17:09

gilly3