Skip to content

Instantly share code, notes, and snippets.

@jazzyjackson
Last active August 20, 2017 20:53
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 jazzyjackson/7772af1576ea6a45ba2a39ccb307c8e2 to your computer and use it in GitHub Desktop.
Save jazzyjackson/7772af1576ea6a45ba2a39ccb307c8e2 to your computer and use it in GitHub Desktop.
function createUpdatePos({clientX, clientY}){ //this is a function creator. When a mouse/touchdown event occurs, the initial position
var theLastX = clientX //is enclosed in a function, those variables are updated on mousemove and will persist
var theLastY = clientY //as long as the function exists. On touch/mouseup events, the function is destroyed (the variable it was assigned to is reassigned null)
return function({clientX, clientY, buttons}){
var movementX = clientX - theLastX
var movementY = clientY - theLastY
theLastX = clientX
theLastY = clientY
var currentXpos = parseInt(document.activeElement.style.left)
var currentYpos = parseInt(document.activeElement.style.top)
document.activeElement.style.left = (currentXpos + movementX) + 'px'
document.activeElement.style.top = (currentYpos + movementY) + 'px'
}
}
function handleDrag(event){
if(event.target != this) return undefined // exit function if a mousedown managed to bubble up to me.
window.updatePos = createUpdatePos(this.parentElement)
document.body.addEventListener('mousemove', window.updatePos)
document.body.addEventListener('mousemove', window.cancelDrag)
document.body.setAttribute('dragging',true) //textArea would occasionally steal focus because it thought I wanted to type. I'll disable in during the move.'
}
function cancelDrag(){
if(!event.buttons){
document.body.removeEventListener('mousemove', window.updatePos)
document.body.setAttribute('dragging',false) //textArea would occasionally steal focus because it thought I wanted to type. I'll disable in during the move.'
}
}
document.body.addEventListener('mouseup', cancelDrag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment