javascript move cursor position code example
Example 1: position of the mouse cursor javascript
(function() {
document.onmousemove = handleMouseMove;
function handleMouseMove(event) {
var eventDoc, doc, body;
event = event || window.event;
if (event.pageX == null && event.clientX != null) {
eventDoc = (event.target && event.target.ownerDocument) || document;
doc = eventDoc.documentElement;
body = eventDoc.body;
event.pageX = event.clientX +
(doc && doc.scrollLeft || body && body.scrollLeft || 0) -
(doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = event.clientY +
(doc && doc.scrollTop || body && body.scrollTop || 0) -
(doc && doc.clientTop || body && body.clientTop || 0 );
}
}
})();
Example 2: how to move an element to the cursor in javascript
$(document).ready(function() {
$(this).on("click", function(e) {
var x = e.pageX;
var y = e.pageY;
var el = $("#meh");
el.css('position', 'absolute');
el.css("left", x);
el.css("top", y);
})
})