Home Web Front-end JS Tutorial How to get the mouse click position in JavaScript? Summary of three methods to obtain

How to get the mouse click position in JavaScript? Summary of three methods to obtain

Oct 29, 2018 pm 01:41 PM
javascript Location Mouse click

In fact, we have been dealing with elements in DOM. This is also a common aspect of mobile phone interaction. However, many programmers are disappointed. Different results will appear in different browsers. Let’s talk about it below. Let’s take a look at how to use js to get the position of the mouse click, and summarize a detailed tutorial for you.

How to get the mouse click position in JavaScript? Summary of three methods to obtain

1: The mouse is relative to the screen

If we involve the location of the mouse click, it is relatively simple, we can get After the mouse clicks, screenX and screenY are used to determine the approximate location of the click, thereby determining the relative position to the top and bottom margins of the screen. We do not consider factors such as iframe, but the performance is still the same in different browsers. of.

Example:

function getMousePos(event) {             
     var e = event || window.event;             
      return {'x':e.screenX,'y':screenY}  
}
Copy after login

2: The mouse is relative to the browser window

If the position is determined through the above simple code, often these are still Not enough, because in general, we need to get the coordinates of the mouse relative to the browser window, and respectively obtain the top margin and left margin of the mouse relative to the window, which can be represented by clientX and clientY.

For example:

function getMousePos(event) {              
     var e = event || window.event;              
     return {'x':e.clientX,'y':clientY} 
}
Copy after login

Three: Mouse relative to the document

We can use clientX and clientY to get the coordinates of the current browser window, but these The conditions are all limited. When the page appears scrolling, what should I do with the coordinates relative to the document? At this time, we only need to add the scrolling displacement.

For example:

function getMousePos(event) {
    var e = event || window.event;
    var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
    var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
    var x = e.pageX || e.clientX + scrollX;
    var y = e.pageY || e.clientY + scrollY;
    //alert('x: ' + x + '\ny: ' + y);
    return { 'x': x, 'y': y };
}
Copy after login

The current problem with Firefox browser will be much simpler, because Firefox supports the two attributes of pageX and pageY, and has already taken page scrolling into account.

The above is how to get the position of mouse click in JavaScript? Get a complete introduction to the three methods. If you want to know more about JavaScript Video Tutorial, please pay attention to the php Chinese website.


The above is the detailed content of How to get the mouse click position in JavaScript? Summary of three methods to obtain. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Win11 startup path and how to open it Win11 startup path and how to open it Jan 03, 2024 pm 11:13 PM

Win11 startup path and how to open it

Details on how to turn on environment variable settings on Windows 11 Details on how to turn on environment variable settings on Windows 11 Dec 30, 2023 pm 06:07 PM

Details on how to turn on environment variable settings on Windows 11

Understand the location and structure of pip installation package storage Understand the location and structure of pip installation package storage Jan 18, 2024 am 08:23 AM

Understand the location and structure of pip installation package storage

How to create underline animation when mouse clicks text in PPT How to create underline animation when mouse clicks text in PPT Mar 26, 2024 pm 06:40 PM

How to create underline animation when mouse clicks text in PPT

How to change the location of Gaode Map Home How to change the location of Gaode Map Home Feb 27, 2024 pm 07:31 PM

How to change the location of Gaode Map Home

Where is Kuaishou published and how to change its location? How to add a location to a video that has been uploaded? Where is Kuaishou published and how to change its location? How to add a location to a video that has been uploaded? Mar 21, 2024 pm 06:00 PM

Where is Kuaishou published and how to change its location? How to add a location to a video that has been uploaded?

Location of Origami Bird at Stardome Railway Crocker Film and Television Park Location of Origami Bird at Stardome Railway Crocker Film and Television Park Mar 27, 2024 pm 11:51 PM

Location of Origami Bird at Stardome Railway Crocker Film and Television Park

Where is the Meituan Daily Voucher location_Meituan Daily Voucher location introduction Where is the Meituan Daily Voucher location_Meituan Daily Voucher location introduction Mar 27, 2024 pm 05:11 PM

Where is the Meituan Daily Voucher location_Meituan Daily Voucher location introduction

See all articles