var isDrag = 0; //是否可拖动标志,可拖动为1,不可拖动为2
var divAndMouseX; //鼠标落点距离div左上角x坐标的差距
var divAndMouseY; //鼠标落点距离div左上角y坐标的差距
var divMove;
//鼠标按下时
function down(div) {//鼠标按下
isDrag = 1; //将div设置为可拖动
divMove = div; //用于下边拖动时的定位(新的位置的坐标)
//首先计算鼠标与拖动对象左上角的坐标差,然后在下边拖动方法中,用鼠标的坐标减坐标差就是新位置的坐标了
divAndMouseX = event.clientX - parseInt(div.style.left);
divAndMouseY = event.clientY - parseInt(div.style.top);
}
function move() {//拖动过程计算div坐标
if (isDrag == 1) {
divMove.style.left = event.clientX - divAndMouseX;
divMove.style.top = event.clientY - divAndMouseY;
}
}