Skip to content

Instantly share code, notes, and snippets.

@think2011
Created December 17, 2014 14:33
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 think2011/5380bee9cd37e1bfbab6 to your computer and use it in GitHub Desktop.
Save think2011/5380bee9cd37e1bfbab6 to your computer and use it in GitHub Desktop.
简单的拖动功能
var box = document.querySelector('#box');
box.onmousedown = function (e) {
box.style.position = 'absolute';
var diffX = e.clientX - box.offsetLeft;
var diffY = e.clientY - box.offsetTop;
document.onmousemove = function (e) {
box.style.left = e.clientX - diffX + 'px';
box.style.top = e.clientY - diffY + 'px';
}
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment