Skip to content

Instantly share code, notes, and snippets.

@clinyong
Last active August 29, 2015 14:06
Show Gist options
  • Save clinyong/0a272fdb3c8e9223b0d1 to your computer and use it in GitHub Desktop.
Save clinyong/0a272fdb3c8e9223b0d1 to your computer and use it in GitHub Desktop.
JavaScript drag div
window.onload = addListeners;
function addListeners() {
document.getElementById('dxy').addEventListener('mousedown', mouseDown, false);
window.addEventListener('mouseup', mouseUp, false);
}
function mouseUp() {
window.removeEventListener('mousermove', divMove, true);
}
function mouseDown() {
window.addEventListener('mousermove', divMove, true);
}
function divMove(e) {
var div = document.getElementById('dxy');
div.style.position = 'absolute';
div.style.top = e.clientY + 'px';
div.style.left = e.clientX + 'px';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment