Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
181
rated 0 times [  184] [ 3]  / answers: 1 / hits: 18785  / 14 Years ago, thu, october 14, 2010, 12:00:00

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.


More From » css

 Answers
7

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 = parseInt(document.getElementById('container').style.top.split('px')[0], 10); // This row returns 20


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


[#95330] Monday, October 11, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austynp

Total Points: 505
Total Questions: 118
Total Answers: 106

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
austynp questions
;