Code:
function Point2D(x, y) {
this.x = x;
this.y = y;
};
function Oblong2D(l, t, r, b) {
this.left = l;
this.top = t;
this.right = r;
this.bottom = b;
this.width = this.right - this.left;
this.height = this.bottom - this.top;
this.relCentre = new Point2D(this.width / 2, this.height / 2);
this.absCentre = new Point2D(this.left + this.relCentre.x, this.top + this.relCentre.y);
};
function getBoundingBox(obj) {
var curleft = 0,
oObj = obj;
if (obj.offsetParent)
while (obj.offsetParent) {
curleft += obj.offsetLeft;
obj = obj.offsetParent;
}
else if (obj.x) curleft += obj.x;
obj = oObj;
var curtop = 0;
if (obj.offsetParent)
while (obj.offsetParent) {
curtop += obj.offsetTop;
obj = obj.offsetParent;
}
else if (obj.y) curtop += obj.y;
var curright = curleft + oObj.offsetWidth,
curbottom = curtop + oObj.offsetHeight;
return new Oblong2D(curleft, curtop, curright, curbottom);
}
function move(el) {
var elBounds = getBoundingBox(el);
document.onmousemove = function(e) {
var ev = e || window.event;
el.style.position = "absolute";
el.style.left = (ev.pageX || ev.clientX + (window.document.body.scrollLeft || window.document.documentElement.scrollLeft)) - elBounds.relCentre.x + "px";
el.style.top = (ev.pageY || ev.clientY + (window.document.body.scrollTop || window.document.documentElement.scrollTop)) - elBounds.relCentre.y + "px";
return false;
};
el.onmouseup = function(e) {
var ev = e || window.event;
document.onmousemove = null;
this.style.left = (ev.pageX || ev.clientX + (window.document.body.scrollLeft || window.document.documentElement.scrollLeft)) - elBounds.relCentre.x + "px";
this.style.top = (ev.pageY || ev.clientY + (window.document.body.scrollTop || window.document.documentElement.scrollTop)) - elBounds.relCentre.y + "px";
};
}
Bookmarks