Contact!

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: ${event.clientX}, Mouse Y: ${event.clientY}`);
});

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.

  1. Akash says:

    It is very useful

  2. alpineupvc says:

    your website blogs supports my rankings, thanks for sharing the posts!

  3. deo says:

    Thanks, it’s great to see a modern tutorial using goodies like string interpolation.

  4. Mike says:

    Yes, but how do you get the current mouse position if you just need to know where it is a particular moment, rather than relying on/waiting for an event to trigger?

  5. Shakya says:

    It’s kinda complex.

  6. Thomas says:

    Does this mean that you can store the mouse coordinates in a variable? This can be useful!

Join the discussion!

You might like:

Create, register and use shortcodes in WordPress

Create, register and use shortcodes in WordPress

by Gav 18/03/2023

Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.

How to use guard clauses in JavaScript

How to/why use guard clauses in JavaScript

by Gav 16/03/2023

Learn how to improve code readability and performance by using guard clauses in JavaScript. Discover their benefits and best practices.

Implements and Extends, Object Oriented TypeScript

Implements and Extends, Object Oriented TypeScript

by Gav 15/03/2023

Learn the difference between implements and extends in TypeScript. Use Implements to implement interfaces and types, and extends to inherit from classes.

Reading/Parsing and Writing YAML files in PHP, Symfony

Reading/Parsing and Writing YAML files, PHP Symfony

by Gav 14/03/2023

In this tutorial we will look at using YAML in PHP. Learn about Parsing and Writing YAML files using Symfony's YAML component.

Measuring code execution performance in JavaScript

Measuring code execution performance in JavaScript

by Gav 13/03/2023

Measuring code execution performance is an important way to identify bottlenecks. Use these methods in JavaScript to help optimise your code.

Measuring script/code execution time in PHP using microtime

Measuring script/code execution time in PHP, microtime

by Gav 06/03/2023

Find bottlenecks, optimise and clean your code, and speed up your apps by measuring the execution time of your PHP scripts using microtime.

Regenerate WordPress media image sizes, programmatically

Regenerate WordPress media image sizes, programmatically

by Gav 25/02/2021

Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.

Magic Constants in PHP. What they are and how to use them.

Magic Constants in PHP. What they are and how to use them

by Gav 15/02/2021

Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.

Detecting Keypress JavaScript

Detect single and multiple keypress events: JavaScript

by Gav 16/10/2019

Learn how to use event listeners to detect and handle single and multiple keypress events in JavaScript. Add modifier keys to your application!