Skip to content

Instantly share code, notes, and snippets.

@usmaanalii
Created September 5, 2017 07:55
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 usmaanalii/07c450409f85fc657aa97ca1aa883fad to your computer and use it in GitHub Desktop.
Save usmaanalii/07c450409f85fc657aa97ca1aa883fad to your computer and use it in GitHub Desktop.

27. Click and Drag

What I learned on this mini-project.

Flag variable

These can be used to control the flow of a method or other piece of functionality.

They are mainly used to hold a value until some condition changes, after which you can modify the variable. One of the most common use cases for this is holding a boolean which is dependent on some other piece of code.

https://gist.github.com/f0b8289eee82cf95d3de06adb7d8c963

In this example, the isDown variable is used to track the state of the mouse. When the mousedown event is triggered the isDown variable is changed, which is used to add further functionality.

pageX and offsetLeft

In this project, the mousedown event is used to initiate the click and drag functionality. For this to work effectively, an anchor point was used, representing the initial click point, the pageX property of the event captures this value.

https://gist.github.com/b27a471c79b49b33d48da9cdb396c266

If a margin or padding is added, then the pageX value will be off, to compensate for this adjustment, you can subtract the pixels changed (from the parent element's offset) to the window.

The code to do this, is shown below

https://gist.github.com/41c9701a48caa67528f4e359fa707f06

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