Seph Reed
Seph Reed

Reputation: 10878

ScreenX and ScreenY won't align element with cursor when browser has screen zoom

When a screen is zoomed, the event.screenX and event.screenY of a MouseEvent are not equatable to px offsets from the top and bottom. They fall slowly behind when zoomed out, and jump slowly ahead when zoomed in.

How can I take screen zoom into account to make an element follow my cursor?

Upvotes: 1

Views: 216

Answers (1)

Seph Reed
Seph Reed

Reputation: 10878

The trick is to not use screenX and screenY, and instead use clientX and clientY. These measurements are zoom relative, and also viewport relative (so they will still work if your code is embedded in a viewport).

Upvotes: 2

Related Questions