var d;
addEvent(document, 'mousedown', mousedown);
function mousedown(e){
e = e || event;
var target = e.target || e.srcElement;
if (target.className == 'top' && target.parentNode.className == 'drag') {
d = target.parentNode;
d.startX = parseInt (getStyle(d, 'left'));
d.startY = parseInt (getStyle(d, 'top'));
d.mouseX = e.clientX;
d.mouseY = e.clientY;
d.X = document.documentElement.clientWidth;
d.Y = document.documentElement.clientHeight;
addEvent(document, 'mousemove', mousemove);
addEvent(document, 'mouseup', mouseup);
}
}
function mousemove(e){
e = e || event;
var target = e.target || e.srcElement;
cancelEvent(e);
d.style.left = d.startX + (e.clientX - d.mouseX) + 'px';
d.style.top = d.startY + (e.clientY - d.mouseY) + 'px';
if (d.startX + e.clientX > d.mouseX) {
if (d.X - e.clientX > d.offsetWidth + d.startX - d.mouseX) {
moveX(d.startX + e.clientX - d.mouseX);
} else {
moveX(d.X - d.offsetWidth);
}
} else {
moveX(0);
}
if (d.startY + e.clientY > d.mouseY) {
//moveY(d.startY + (e.clientY - d.mouseY));
if (d.Y - e.clientY > d.offsetHeight + d.startY - d.mouseY) {
moveY(d.startY + e.clientY - d.mouseY);
} else {
moveY(d.Y - d.offsetHeight);
}
} else{
moveY(0);
}
}
function mouseup(e){
e = e || event;
var target = e.target || e.srcElement;
moveTX();
removeEvent(document, 'mousemove', mousemove);
removeEvent(document, 'mouseup', mouseup);
}
function moveX(x){
d.style.left = x + 'px';
}
function moveY(y){
d.style.top = y + 'px';
}
function moveTX(){
for (var i=0, b=128; i<5; i++){
setTimeout(d.style.left = (parseInt(d.style.left)+b) + 'px',1000);
b=b/2;
}
}
Прошу прощения, если код корявый