Skip to content

Instantly share code, notes, and snippets.

@ktsn
Created August 23, 2016 01:46
Show Gist options
  • Save ktsn/6866f87cfa6487fe355e1e01d79bfca1 to your computer and use it in GitHub Desktop.
Save ktsn/6866f87cfa6487fe355e1e01d79bfca1 to your computer and use it in GitHub Desktop.
特定の要素上でマウスイベントが発生した時に、その要素におけるマウスの座標を取得する
el.addEventListener('mousemove', event => {
const rect = event.currentTarget.getBoundingClientRect()
const pos = {
left: event.clientX - rect.left,
top: event.clientY - rect.top
}
})
@ktsn
Copy link
Author

ktsn commented Aug 23, 2016

client(X|Y) ページの左上を基準としたマウスの座標
getBoundingClientRect ページの左上を基準とした対象とする要素の左上の座標

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment