"How to use Javascript get cursor position" Code Answer's
First of all, we will explain what "javascript get cursor position" is.
It means how we can get the cursor position in Javascript. Certainly, to get this, we can use the MouseEvent client properties, that is, the client X Property and the client Y Property. We put these together to get the cursor position.
Moreover, we will open the page, then a div will be used to divide this page into sections. Then a function will be added to it to help us acquire cursor coordinates by using the onmousemove event. This information will only be available to us if the user clicks on this div.
Javascript track mouse pointer
var pointerX = -1;
var pointerY = -1;
document.onmousemove = function(event) {
pointerX = event.pageX;
pointerY = event.pageY;
}
setInterval(pointerCheck, 1000);
function pointerCheck() {
console.log('Cursor at: '+pointerX+', '+pointerY);
Source:
Notice: Undefined index: host in /home/codeprozone/public_html/question.php on line 286
position of the mouse cursor javascript
(function() {
document.onmousemove = handleMouseMove;
function handleMouseMove(event) {
var eventDoc, doc, body;
event = event || window.event; // IE-ism
// If pageX/Y aren't available and clientX/Y are,
// calculate pageX/Y - logic taken from jQuery.
// (This is to support old IE)
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 );
}
// Use event.pageX / event.pageY here
}
})();
Source:
Notice: Undefined index: host in /home/codeprozone/public_html/question.php on line 286
This parameter will be used in our function to get the cursor position to obtain the x and y coordinates of the cursor. The client X property can be used to obtain the cursor's x coordinate. We will use the client Y attribute to acquire the y coordinate.
Finally, the findings will be displayed to the user using the textContent attribute.