释放的代码为什么一定要放在fnDown里面
下面这个代码:
function fnDown() {
var oDrag = document.getElementById('loginPanel');
//document.title = event.clientX + ',' + event.clientY; //显示鼠标坐标
var disx = event.clientX - oDrag.offsetLeft;
var disy = event.clientY - oDrag.offsetTop;
//移动
document.onmousemove = function (event) {
event = event || window.event;
var l = event.clientX - disx;
var t = event.clientY - disy;
var winW = document.documentElement.clientWidth || document.body.clientWidth,
winH = document.documentElement.clientHeight || document.body.clientHeight,
maxW = winW - oDrag.offsetWidth - 10,
maxH = winH - oDrag.offsetHeight;
if (l < 0) {
l = 0;
} else if (l > maxW) {
l = maxW;
}
if (t < 10) {
t = 10;
} else if (t > maxH - 10) {
t = maxH;
}
oDrag.style.left = l + 'px';
oDrag.style.top = t + 'px'
}
//释放
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
}
}
我把释放那一块移出fnDown,在拖动的时候。第一次拖动鼠标然后松开鼠标是正常的,第二次拖鼠标的时候再送开鼠标,释放的程序就好像没执行一样,光标移至和图片一起移动。请问这是为什么?