BEGIN 从网上摘抄
// box.oncontextmenu = function (e) {
// e.preventDefault();
// };
box.onmousedown = function (e) {
var e = e || window.event;
x = e.clientX - box.offsetLeft;
y = e.clientY - box.offsetTop;
isDrop = true;
}
document.onmousemove = function (e) {
//是否为可移动状态
if (isDrop) {
// e.preventDefault();
var e = e || window.event;
var moveX = e.clientX - x; //得到距离左边移动距离
var moveY = e.clientY - y; //得到距离上边移动距离
//可移动最大距离
var maxX = document.documentElement.clientWidth - box.offsetWidth;
var maxY = document.documentElement.clientHeight - box.offsetHeight;
//范围限定 当移动的距离最小时取最大 移动的距离最大时取最小
//范围限定方法一
/*if(moveX < 0) {
moveX = 0
} else if(moveX > maxX) {
moveX = maxX;
}
if(moveY < 0) {
moveY = 0;
} else if(moveY > maxY) {
moveY = maxY;
} */
//范围限定方法二
moveX = Math.min(maxX, Math.max(0, moveX));
moveY = Math.min(maxY, Math.max(0, moveY));
box.style.left = moveX + "px";
box.style.top = moveY + "px";
} else {
return;
}
}
document.onmouseup = function () {
isDrop = false; //设置为false不可移动
}
END
自己需要做一个变更点到某个div时,使div移动
$("#ly2_Panel1 div").mousedown(function () {
var thisframe = $(this).attr("id");
var thisframe1 = document.getElementById(thisframe);
subid = thisframe.substring(4);
if (event.button == 2) {
document.onmousemove = function (e) {
thisframe1.style.left = sharepx + "px";
thisframe1.style.top = sharepy + "px";
**//sharepx ,sharepy 是我之前有获取当我鼠标悬浮在页面哪里时,获取页面PX值和PY,我声明公用变量**
sharepx = sharepx - 10;//偏差自己修改的
sharepy = sharepy -9;//偏差自己修改的
$.ajax({
url: '../Handle/Handler2.ashx',
type: 'post',
data: 'type=coordinaterev&subid=' + subid + '&px=' + sharepx + '&py=' + sharepy,
success: function (d) {
if (d == 0) {
alert("修改失敗!請聯繫管理員")
} else {
}
}
})
}
document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
}
}
})