Skip to content

Instantly share code, notes, and snippets.

@greggman
Last active May 22, 2019 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggman/23083581edb7b1b60187ff19f6da4279 to your computer and use it in GitHub Desktop.
Save greggman/23083581edb7b1b60187ff19f6da4279 to your computer and use it in GitHub Desktop.
Mouse Capture JavaScript

Mouse Capture

Mouse capture in this document means you want it so when the user clicks and drags on some element that element continues to receive events until the user releases the mouse button even if they drag outside of the element or even outside the window.

Solution:

  1. On element mousedown add events to document or window for mousemove and mouseup.

    do NOT call e.preventDefault() else it doesn't capture

  2. Remove mousemove and mouseup listeners on mouseup

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