views:

56

answers:

3

I have a div set to the css class float with float being:

.float {
display:block; 
position:fixed; 
top: 20px;
left: 0px;
z-index: 1999999999;
}
* html .float {position:absolute;}

This class causes the element to stay in a fixed position on the page (the *html part is to make it work in IE). I am using javascript to shift the position of the element horizontally and vertically.

I need to get the absolute position of the div relative to the browser window in javascript (how many pixels from the top and left of the browser window the div is). Right now, I am using the following:

pos_left = document.getElementById('container').offsetLeft;
pos_top = document.getElementById('container').offsetTop;

The code above works for IE, Chrome, and FF, but in Opera it returns 0 for both. I need a solution that works for all of those browsers. Any ideas?

Btw: Keeping tracking of the changes made by javascript is possible, but that is not the solution I am looking for due to performance reasons. Also, I am not using jquery.

A: 

Maybe you should start using jQuery ;-)

http://jqueryui.com/demos/position/

Or at least get their non-minified version and see what they are doing.

Jakub Konecki
-1 jQuery is not the answer to every javascript problem. Also, you didn't even try to answer the question.
Gabi Purcaru
I'll take a look at there source code. I would have used jquery but even minified and gzipped it's far too large for what I need. The library I created for my project is ~10 kilobytes before being gzipped.
@user396404 - I don't know the details of your project, but downloading 20kB once (or possibly nothing, if you use CDN) is not IMHO a big problem nowadays, and in most cases turns out cheaper when compared to the cost of maintaining and fixing custom code
Jakub Konecki
Normally it wouldn't be a problem but I'm trying to shave milliseconds off the load time and that's critical enough for this project that increased expenditure on development cost is acceptable. If I was doing development on something else, using jquery would make my life a lot easier. Also, according to Yahoo's research around 40% of users visit a site without cached data so even with a cdn there is still that 40% to worry about. If browsers came prepacked with jquery I would be all over it. For now, my ~2.5 kb gzipped custom framework fits my requirements like a glove without excess overhead
A: 

If you can use items style element;

<div id="container" style="top: 20px;left: 0px;z-index: 1999999999;">

You can get it with element style attribute;

var top = eval(document.getElementById('container').style.top.split('px')[0]); // This row returns 20

You can use top, left, width, height etc...

ismailperim
Why do you use eval ? This is not needed at all
Fabien Ménager
Still the same thing. Says 0 for the offset :/
@Fabien because style.top attribute is splitted and its returns string.@user396404 can u post code any web page or this page?
ismailperim
Ah nm, got it work using this. Just modified it a bit and made the following function:function styleval(v){ v = v.split('px'); return v[0]*1; }then I use pos_top = styleval(document.getElementById('container').style.top);Thanks for the advice :)
@ismailperim parseInt(document.getElementById('container').style.top) does the trick.
Fabien Ménager
A: 

You may find clues here : http://code.google.com/p/doctype/wiki/ArticlePageOffset But I think you'll need to add the parents' offsets to have the right value.

Fabien Ménager