日期:2014-05-16 浏览次数:20454 次
var b = document.getElementById("block");
var drag = false;
function getevent(e) {
return (e)?e:window.event;
}
b.addEventListener("onmousedown",function(e) {
console.log("done!");
drag = true;
var x = getevent(e).clientX;
var y = getevent(e).clientY;
postop = b.offsetTop; posleft = b.offsetLeft;
b.addEventListener("onmousemove",function(e) {
if(!drag) return false;
b.style.top = postop + getevent(e).clientY - y +"px";
b.style.left = posleft + getevent(e).clientX - x +"px";
});
});
b.addEventListener("onmouseup",function(e) {
drag = false;
});
b.onmousedown = function(e) {
drag = true;
postop = b.offsetTop; posleft = b.offsetLeft;
x = getevent(e).clientX; y = getevent(e).clientY;
b.onmousemove = function(e) {
if(!drag) return false;
b.style.top = postop + getevent(e).clientY - y +"px";
b.style.left = posleft + getevent(e).clientX - x +"px";
}
}
b.onmouseup = function(e) {
drag = false;
}