/    Sign up×
Community /Pin to ProfileBookmark

best method to get x/y/w/h of a page element

Suppose I have a page element (DIV, SPAN whatever) and want to retrieve the position and width/height of it. What is the best way?

for x/y i have this:

[code=php]
function getElementPos (el) {
var curleft = curtop = 0;
if (!el.offsetParent) {
alert(“Your browser does not support a necessary feature. Please upgrade to a newer browser”);
return;
}
curleft = obj.offsetLeft
curtop = obj.offsetTop
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft
curtop += obj.offsetTop
}
return {left:curleft, top:curtop};
}
[/code]

For width/height I am familiar with element.style.width but am wondering how reliable that is across all browsers. and whether it works if no style or class has been defined for an element.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 28.2008 — Try offsetWidth and offsetHeight, added to scrollWidth and scrollHeight.

But please lose the alert message.

Chances are seeing that on your webpage isn't going to make anyone yearn for a better browser.

Just a faster back button.

A good site is a good site with the scripts turned off.
Copy linkTweet thisAlerts:
@sneakyimpauthorMar 28.2008 — that error message is more for me than anyone...my target audience is quite likely to have the very latest browsers so i'm not particularly concerned. I appreciate what you're saying but have bigger fish to fry at the moment.

I'm having a bit of trouble. I'm making a 'click shield' for my page to prevent interaction with sections that are in the middle of an AJAX query. My function to postion the click shield is always too small. Both scroll height and width and offset Height/width result in a click shield that is too small in an amount proportional to the amount of border/margin/padding that the target element has.

[code=php]
JTA.ClickShield.prototype.show = function() {
this.el.style.display = '';
if (this.elTarget) {
this.el.style.position = 'absolute';
var pos = JTA.util.getElementPos(this.elTarget);
this.el.style.top = pos.top + 'px';
this.el.style.left = pos.left + 'px';
this.el.style.width = this.elTarget.scrollWidth + 'px';
this.el.style.height = this.elTarget.scrollHeight + 'px';
}
}
[/code]


It places things perfectly but they are always too small by the sum of padding, border, and margin amounts.
Copy linkTweet thisAlerts:
@sneakyimpauthorMar 28.2008 — NEVERMIND! I realized that the underlying DIV was being resized after the fact due to the application of a class. I'm good!
×

Success!

Help @sneakyimp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...