Home Web Front-end JS Tutorial Summary of obtaining mouse position related attributes in JavaScript_javascript skills

Summary of obtaining mouse position related attributes in JavaScript_javascript skills

May 16, 2016 pm 04:34 PM
javascript Attributes

Javascript does not have a mouse object. Obtaining mouse coordinates depends on the powerful event object.

By monitoring the mousemove of the document, we can obtain the mouse position in real time.

But! ! There are too many mouse-related attributes in the event, which is very confusing! As follows:

event.layerX event.layerY
event.clientX event.clientY
event.pageX event.pageY
event.offsetX event.offsetY
event.screenX event.screenY
event.x event.y

6 groups in total!

And the difference between them is not obvious, and the browsers are not compatible!

The purpose of this article is to clarify their differences and select those that are not recommended.

Test code

Run the following code directly:

Copy code The code is as follows:



http://www.w3.org/1999/xhtml"> />








Show results


In-screen div

Tall and wide. . .

off-screen DIV



<script><br> var jg = document.getElementById('jg');<br> document.onmousemove = function (e) {<br> e = e || window.event;<br> jg.innerHTML = 'layerX:' e.layerX <br> ',layerY:' e.layerY <br> ', <br/>clientX:' e.clientX <br> ',clientY:' e.clientY <br> ', <br/>pageX:' e.pageX <br> ',pageY :' e.pageY <br> ',<br/>offsetX:' e.offsetX <br> ',offsetY:' e.offsetY <br> ',<br/>screenX:' e.screenX <br> ',screenY:' e.screenY <br> ',<br/>x:' e.x <br> ',y:' e.y;<br> }<br> </script>


During the testing process, an artifact was discovered: chrome (Google browser) and IE9 are fully compatible with all the above attributes! It is very convenient to compare them.

After comparison, the results are as follows:

Explanation of each attribute

clientX and Y are the coordinates of the mouse relative to the browser viewport (i.e. the part outside the scroll bar is ignored); all browsers support it.

ScreenX and Y are the coordinates of the mouse relative to the left side (top edge) of the entire screen. They are basically out of touch with the document; they are fully compatible.

offsetX and Y are the coordinates of the mouse relative to the currently pointed object. If the mouse is pointing to the button, offsetX is relative to the button; firefox does not support

x and y are the same as layerX and Y in standard browsers. They are attributes that can be used to replace layerX in IE

pageX and Y are the coordinates of the mouse relative to the left side (top edge) of the entire page, including those outside the viewport; IE7 and 8 do not support them.

Key points: layerX and layerY

LayerX and Y are new attributes introduced by standard browsers and are also supported by IE9. It can be used to replace offsetX and Y. However, it is defined as: the coordinates of the nearest element with positioning information relative to the currently pointing element. This "with positioning" means that there are css attributes that are not positioned by default (that is, not static).

If the currently pointed element is positioned, then layerX and Y will return the coordinates relative to this element; otherwise, check the parent tag of this element; if there is still no positioning, continue; all the way to the root element—— html node.

So, in Firefox, if you want the offsetX value, you must add position positioning information!

Compatibility summary:

1. Firefox does not support offsetX, offsetY and x, y attributes, but they can be replaced by layerX;
2. x and y in ie are equivalent to layerX and layerY in firefox&chrome;
3. There is a 2px distance between the document boundary of IE7 and 8 and the browser window boundary, so when the window is maximized, the minimum screenX is 2px;
4. Although layerX and Y in ie9 have values, they are inexplicably incorrect. It seems to be related to the html tag. For example, in the code of my example, drag the scroll bar to the far right and slowly move the mouse from the blank space to the large DIV. At this time, the difference between the rightmost part of the big DIV and the rightmost part of the first DIV will also be calculated into layerX. . . As there are more and more elements at the end, this calculation becomes more complicated; however, firefox and chrome's layerX do not have this problem. So, don’t use layerX in IE9.
5. In chrome, although x and y have values, they are exactly the same as clientX and Y! Therefore, it is not recommended to use x,y attributes.

Compatibility Remediation

In standard browsers, position and event.layerX can be used to implement the event.offsetX attribute;

There is no pageX, pageY in IE7 and 8. You can only use document.documentElement.scrollLeft event.clientX to find it.

So, one of x, y or offsetX, offsetY in IE can be removed.

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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

bottom attribute syntax in CSS bottom attribute syntax in CSS Feb 21, 2024 pm 03:30 PM

Bottom attribute syntax and code examples in CSS In CSS, the bottom attribute is used to specify the distance between an element and the bottom of the container. It controls the position of an element relative to the bottom of its parent element. The syntax of the bottom attribute is as follows: element{bottom:value;} where element represents the element to which the style is to be applied, and value represents the bottom value to be set. value can be a specific length value, such as pixels

Introduction to the attributes of Hearthstone's Despair Thread Introduction to the attributes of Hearthstone's Despair Thread Mar 20, 2024 pm 10:36 PM

Thread of Despair is a rare card in Blizzard Entertainment's masterpiece &quot;Hearthstone&quot; and has a chance to be obtained in the &quot;Wizbane's Workshop&quot; card pack. Can consume 100/400 arcane dust points to synthesize the normal/gold version. Introduction to the attributes of Hearthstone's Thread of Despair: It can be obtained in Wizbane's workshop card pack with a chance, or it can also be synthesized through arcane dust. Rarity: Rare Type: Spell Class: Death Knight Mana: 1 Effect: Gives all minions a Deathrattle: Deals 1 damage to all minions

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

See all articles