/    Sign up×
Community /Pin to ProfileBookmark

moving layers does not work with tables

I have a page that has layers. I use the following function to move layers to a specific location (next to a button that was pressed).

Code:

[code=php]
function moveIt(id) {
it = document.getElementById(id);
it.style.top = event.srcElement.offsetTop;
it.style.left = event.srcElement.offsetLeft + event.srcElement.offsetWidth + 5;
it.style.visibility = (it.style.visibility == “visible”) ? “hidden” : “visible”;
//it.style.visibility = “visible”;
}
[/code]

The moving of a layer works fine if I have my buttons not in a table. As soon as I wrap a table around buttons, layer appears always in one position (0,200) no metter which button I press. event.scrElement.offsetTop and event.scrElement.offsetLeft always return 1 ans 208 no metter which button I press.
How’s table interfering with calculating button positioning?

thanks,
camokat

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 15.2004 — offsetTop and offsetLeft are not [b]absolute[/b] postion values, they are always [b]relative[/b] to the [i]parent(s)[/i] position, if the object is nested (your case, in a table)
Copy linkTweet thisAlerts:
@camokatauthorOct 15.2004 — so how do I fix it? What values should I use?
Copy linkTweet thisAlerts:
@Warren86Oct 15.2004 — Camokat:

Take a look at this example, I found this on the web months ago.

function getRealLeft(el) {

xPos = el.offsetLeft;

tempEl = el.offsetParent;

while (tempEl != null) {

xPos += tempEl.offsetLeft;

tempEl = tempEl.offsetParent;

}

return xPos;

}

function getRealTop(el) {

yPos = el.offsetTop;

tempEl = el.offsetParent;

while (tempEl != null) {

yPos += tempEl.offsetTop;

tempEl = tempEl.offsetParent;

}

return yPos;

}

Assign the returned values to variables in this way:

trueX = getRealLeft(elementReference);

trueY = getRealTop(elementReference);
Copy linkTweet thisAlerts:
@KorOct 15.2004 — 
so how do I fix it? What values should I use?
[/quote]


Sorry... just add the parent's position to the nested element's position
Copy linkTweet thisAlerts:
@camokatauthorOct 15.2004 — Nice. Thanks guys!
×

Success!

Help @camokat 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...