Here is a handy functions to get the posisiton of a control using Javascript:
Usage:
alert(getElementPosition('txtName').top + ' ' + getElementPosition('txtName').left);
Function:
function getElementPosition(elemID) {
var offsetTrail = document.getElementById(elemID);
var offsetLeft = 0;
var offsetTop = 0;
while (offsetTrail) {
offsetLeft += offsetTrail.offsetLeft;
offsetTop += offsetTrail.offsetTop;
offsetTrail = offsetTrail.offsetParent;
}
return { left: offsetLeft, top: offsetTop };
}
We can get the current scroll position of the document using:
var scrolly = typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement.scrollTop;Hope this helps you...
var scrollx = typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement.scrollLeft;
3 comments:
How do you invoke this in c# asp.net page? Tried it and it's not working. Would you be able to give a sample, please?
not working properly
Yes, it is working perfectly. I used it to pop up text over an linked image,
function popupText(imageid, str, height, width) {
//this.style.cursor = 'pointer';
document.getElementById("imagepopup").style.display = "block"
document.getElementById("imagepopup").innerHTML = str
myvar = document.getElementById(imageid);
offsetLeft = 0;
offsetTop = 0;
myvar.style.cursor = 'pointer';
while (myvar) {
offsetLeft += myvar.offsetLeft;
offsetTop += myvar.offsetTop;
myvar = myvar.offsetParent;
}
offsetTop += 20;
offsetLeft += 20;
document.getElementById("imagepopup").style.top = offsetTop + "px";
document.getElementById("imagepopup").style.left = offsetLeft + "px";
document.getElementById("imagepopup").style.height = height + "px"
document.getElementById("imagepopup").style.width = width + "px"
}
Post a Comment