Created
December 17, 2014 14:33
-
-
Save think2011/5380bee9cd37e1bfbab6 to your computer and use it in GitHub Desktop.
简单的拖动功能
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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