Home >Web Front-end >JS Tutorial >Is Mouse Position Retrievable in JavaScript Without Mouse Movement Events?

Is Mouse Position Retrievable in JavaScript Without Mouse Movement Events?

Susan Sarandon
Susan SarandonOriginal
2024-10-23 19:03:02756browse

Is Mouse Position Retrievable in JavaScript Without Mouse Movement Events?

Can Mouse Position Be Retrieved in JavaScript Without Mouse Movement?

Question:

Is it feasible to obtain the mouse position using JavaScript after page load without any mouse movement event?

Answer:

Ordinarily, it is not possible to retrieve the mouse position without mouse movement events. However, an elaborate workaround can be implemented.

Workaround:

  1. Create a transparent div element that covers the entire document.
  2. Generate a significant number of small (<1px) anchor () elements within this div, each with a CSS hover rule that modifies a property (e.g., font-family).
  3. Iterate through these elements on page load, checking their computed style until one with the hover font is found.
  4. Extract the coordinates from this identified element to approximate the mouse position within the document.

Caution:

While technically possible, this approach is highly inefficient and impractical. Avoid implementing it in actual projects.

The above is the detailed content of Is Mouse Position Retrievable in JavaScript Without Mouse Movement Events?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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