var mouseDownX, mouseDownY, initX, initY,doc_onmousemove, doc_onmouseup, flag = false, isMoving = false;
el.onmousedown = function(e) {
var obj = document.getElementById(binding.value);//移动目标元素
//表示鼠标已按下
flag = true;
//鼠标按下时的鼠标所在的X,Y坐标
mouseDownX = e.clientX;
mouseDownY = e.clientY;
//初始位置的X,Y 坐标
initX = obj.offsetLeft;
initY = obj.offsetTop;
//保存原来绑定在document的事件
doc_onmousemove = document.onmousemove;
doc_onmouseup = document.onmouseup;
document.onmousemove = move;
document.onmouseup = obj.onMouseOut = stop;
function move(e) {
if (flag && !isMoving) {
obj.style.left = parseInt(e.clientX) - parseInt(mouseDownX) + parseInt(initX) + "px";
obj.style.top = parseInt(e.clientY) - parseInt(mouseDownY) + parseInt(initY) + "px";
isMoving = true;
setTimeout(function() {
isMoving = false;
}, 10);
}
}
function stop(){
flag = false;
document.onmousemove = doc_onmousemove; //原来的事件回复绑定
document.onmouseup = doc_onmouseup;
}
return false;//可以防止在拖动的时候选中文本
};
js 拖动
最新推荐文章于 2023-11-28 11:58:47 发布