In questo tutorial vedremo come ottenere le dimensioni e la posizione di una finestra utilizzando Javascript.
var $ = function(id) {
return document.getElementById(id);
},
html = document.documentElement,
box = $(‘cnt’),
posX = $(‘left’),
posY = $(‘top’),
width = $(‘width’),
height = $(‘height’),
boxWidth = box.offsetWidth,
boxHeight = box.offsetHeight,
setPos = function() {
var htmlWidth = html.clientWidth,
htmlHeight = html.clientHeight,
marginX = htmlWidth > boxWidth ? -(boxWidth/2) : 0,
marginY = htmlHeight > boxHeight ? -(boxHeight/2) : 0,
left = marginX == 0 ? 0 : ’50%’,
top = marginY == 0 ? 0 : ’50%’;
box.style.left = left;
box.style.top = top;
box.style.marginLeft = marginX + ‘px’;
box.style.marginTop = marginY + ‘px’;
},
getPos = function() {
var curLeft =0, curTop = 0, obj = box;
if (obj.offsetParent) {
while (obj.offsetParent) {
curLeft += box.offsetLeft;
curTop += box.offsetTop;
obj = obj.offsetParent;
}
}
posX.innerHTML = curLeft;
posY.innerHTML = curTop;
};
width.innerHTML = boxWidth;
height.innerHTML = boxHeight;
setPos();
getPos();
window.onresize = function(){
setPos();
getPos();
}
fonte: www.sastgroup.com






