Javascript event cursor position

How to find the coordinates of the cursor with JavaScript?

In this tutorial, we will learn how we can find the coordinates of the mouse cursor with JavaScript. We have to find out the X and Y-coordinates or we can say that the horizontal and the vertical position of the cursor on the screen with JavaScript.

JavaScript provides us with two different properties to get the coordinates of the mouse cursor when the mouse button is pressed anywhere on the screen −

Using event.clientX Property

The event.clientX property is used to find out the value of the horizontal position or the X-coordinate of the cursor where the event was triggered. It returns a numeric value that specifies the horizontal position of the cursor.

Syntax

Following is the syntax to get the horizontal position of the cursor −

function function_name(event)

Steps

Step 1 − In step one, we need to define the callback function that performs some activity when the event triggers.

Step 2 − In the second step, we will declare a variable and assign it a value using event.clientX property, that returns the horizontal or the X-coordinate of the cursor position.

Читайте также:  Колонтитул html при печати

Step 3 − This step contains the statements that are required to display the returned value on the screen that is stored in the variable declared in last step.

Example

Below example illustrates how we can get the X-coordinate of the cursor using JavaScript −

!DOCTYPE html> html onclick="display(event)"> body> h3>Find the coordinates of the cursor with JavaScript/h3> p id="para">Click anywhere on the screen to see X-coordinate of the cursor using "event.clientX" property./p> p id="result">/p> script> function display(event) let X = event.clientX; let result = document.getElementById("result"); result.innerHTML = "X-coordinate: " + X; > /script> /body> /html>

In this example, we find out the position of the cursor on X-axis or in horizontal direction using the event.clientX property.

Using event.clientY Property

It is used to find out the position of the cursor in the vertical direction or on the Y-axis where the event was triggered. Similar to the event.clientX property, it also returns a numeric value that holds the position of the cursor on the Y-axis or Y-coordinate.

Syntax

Following is the syntax to use event.clientY property to get the Y-coordinate of cursor using JavaScript −

function function_name(event)

Steps

Step 1 − In step one, we will define the callback function that performs some activity on the webpage when the event triggers.

Step 2 − In second step, we will declare a variable and assign it a value using the event.clientY property, that returns the vertical or the Y-coordinate of the cursor position.

Step 3 − This step includes the statements that are required to display the returned value of the cursor position on the Y-coordinate or in the vertical direction on the screen that is stored in the variable declared in the last step.

Example

Below example illustrates how we can get the X-coordinate of the cursor using JavaScript −

!DOCTYPE html> html onclick="display(event)"> body> h3>Find the coordinates of the cursor with JavaScript/h3> p id="para">Click anywhere on the screen to see Y-coordinate of the cursor using "event.clientY" property./p> p id="result">/p> script> function display(event) let Y = event.clientY; let result = document.getElementById("result"); result.innerHTML = "Y-coordinate: " + Y; > /script> /body> /html>

In this example, we have find out the Y-coordinate of the cursor using The event.clientY property of JavaScript.

Let us understand how we can find out the position of the cursor on a 2D plane using both properties in the same example.

Steps

Step 1 − In step one, we will define the callback function that performs some activity on the webpage when the event triggers.

Step 2 − In second step, we will declare two variables and assign them values using the event.clientX and the event.clientY properties, which return the horizontal and the vertical or the X and the Y-coordinates of the cursor position on the screen.

Step 3 − This step includes the statements that are required to display the returned value of the cursor position on the Y-coordinate or in the vertical direction on the screen that is stored in the variable declared in the last step.

Example

Below example illustrates how we can get the X-coordinate of the cursor using JavaScript −

!DOCTYPE html> html onclick="display(event)"> body> h3>Find the coordinates of the cursor with JavaScript/h3> p id="para">Click anywhere on the screen to see X and Y-coordinate of the cursor using JavaScript properties./p> p id="result">/p> script> function display(event) let X = event.clientX; let Y = event.clientY; let result = document.getElementById("result"); result.innerHTML = "X-coordinate: " + X + "
Y-coordinate: : "
+ Y; > /script> /body> /html>

In above example, we have used the event.clientX and event.clientY properties simultaneously to find out the position of cursor on 2D plane using JavaScript.

In this tutorial, we have learnt about the JavaScript properties to find out the coordinates of the cursor with help of individual and a example where we used both the properties to find the position of cursor on 2D plane or X and Y-coordinates simultaneously.

Источник

How to Get the Cursor Position in JavaScript

To get the cursor position in JavaScript, we can use the MouseEvent client properties, that is the clientX Property and the clientY Property.

event.clientX event.clientY

To get the cursor position we will need to attach a mouse event to either a div on the page or to the whole page document itself. We will need to attach a function to that event, and then use the function to get the position of the cursor.

A good event to use to get the position of the cursor is the onmousemove event. Whenever a user moves their mouse on the specified element, the onmousemove event will be triggered. We can then call a function, and the important part, we will pass in a parameter to the function, that will contain information about our cursor.

Here is the HTML setup we can use for the first part of getting the cursor position.

As you can see in the code above, we attach the function getCusorPosition and pass it a parameter we call event. We will use this in the next and final part below.

function getCursorPosition(event)

As we can see from the code above in our function, we take the parameter that has been passed to us called event and get the properties clientX and clientY of it.

We put these together to get the cursor position. Let’s take a look at this in action below.

How to Get and Display the Cursor Position in JavaScript

In this example, we will create a div that takes up a section of the page. We will then add the onmousemove event to the div to run a function to help us get the X and Y coordinates of the cursor. The user will have to move their mouse in this div for us to get this information.

Finally, we will have two divs below where we can display the x and y positions of our cursor to the user.

In our function getCursorPosition, we will use the parameter that has been passed to us to get the x and y coordinates of the cursor. To get the x coordinate of the cursor, we will use the clientX property. To get the y coordinate of the cursor, we will use the clientY property.

We will finally use the textContent property to display the results to the user.

And that’s it. Here is the JavaScript code:

function getCursorPosition(event)

The final code and output for this example is below:

Источник

Get the current position of the mouse from a JavaScript event

Get the current mouse position from a JavaScript event

Would you like to be able to open a modal, or a context menu? Maybe you are making a browser game, or simply adding a sparkly trail to your mouse. To do all of these things you need to know the current mouse position relative to the screen.

This simple problem was something I found myself googling fairly often when I was new to coding. I would usually include some catch like ‘get the current mouse position without using an event’. As far as I’m aware it isn’t possible to get the current mouse position without triggering a mouse event.

So how can we get the mouse position from a mouse event?

Getting the current X and Y coordinates from an event

To get the current mouse position we are going to trigger a mouse event. In this case we will use ‘mousemove’ to log the current X and Y coordinates of the mouse to the console. For a more detailed list of mouse events you could have a read of this.

First we set up an event listener for our event:

document.addEventListener('mousemove', (event) => < >);

Now we can access our event object to get the client X and Y coordinates and log them to the console using string interpolation:

document.addEventListener('mousemove', (event) => < console.log(`Mouse X: $, Mouse Y: $`); >);

As a result, if you now open your console you will see that every time you move the mouse there will be a log of the mouse coordinates ‘helpfully’ spammed all over your screen.

Источник

How to Get the Current Mouse Position in JavaScript

To get the current position of the mouse in JavaScript, add a mousemove event listener to the window object and access the clientX and clientY properties of the MouseEvent object in the listener to get the X and Y coordinates of the mouse respectively.

The mousemove event is triggered on an element when the mouse hovers it. To be more precise, it is fired when the mouse is moved and the cursor’s hotspot is within the element’s bounds.

We attach the event listener to the window object to trigger the event whenever the mouse has moved anywhere on the page.

The mousemove event listener receives a MouseEvent object used to access information and perform actions related to the event. We use the clientX and clientY properties of this object to get the position of the mouse on the X-coordinate and Y-coordinate respectively in the application’s viewport.

Get current mouse position relative to element in React

In the first example, we get the current mouse position in global coordinates. In global coordinates, position (0, 0) is at the top-left of the webpage and position (Xmax, Ymin) is at the bottom right.

We might instead want to get the mouse position within the region of an element.

To get the current mouse position relative to an element, set a mousemove event handler on the element, then calculate the local X and Y positions using properties of the MouseEvent object passed to the event handler.

Источник

Оцените статью