Skip to content

Instantly share code, notes, and snippets.

@think2011
Created Dec 17, 2014
Embed
What would you like to do?
简单的拖动功能
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