js:
var isDrag = 0;
var divMove;
var divAndMouseX;//鼠标落点距离div左上角x坐标的差距
var divAndMouseY;//鼠标落点距离div左上角y坐标的差距
function down(div) {
isDrag = 1;
divMove = div;
divAndMouseX = event.clientX - parseInt(document.getElementById(div).offsetLeft);
divAndMouseY = event.clientY - parseInt(document.getElementById(div).offsetTop);
}
function up() {
isDrag = 0;
}
function move() {
if (isDrag == 1) {
document.getElementById(divMove).style.left = event.clientX - divAndMouseX;
document.getElementById(divMove).style.top = event.clientY - divAndMouseY;
}
}
html:
<div id="tip" onmousedown="down(this.id)" onmousemove="move()" onmouseup="up()">