Getting absolute position of an element relative to browser
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...
Finding element's position relative to the document
You can traverse the offsetParent
up to the top level of the DOM.
function getOffsetLeft( elem )
{
var offsetLeft = 0;
do {
if ( !isNaN( elem.offsetLeft ) )
{
offsetLeft += elem.offsetLeft;
}
} while( elem = elem.offsetParent );
return offsetLeft;
}
How can I get an object's absolute position on the page in Javascript?
var cumulativeOffset = function(element) {
var top = 0, left = 0;
do {
top += element.offsetTop || 0;
left += element.offsetLeft || 0;
element = element.offsetParent;
} while(element);
return {
top: top,
left: left
};
};
(Method shamelessly stolen from PrototypeJS; code style, variable names and return value changed to protect the innocent)
Force position: absolute to be relative to the document and not the parent container
You will have to place the div
outside of the position:relative
element and into body
.
Absolute position of an element on the screen using jQuery
See .offset()
here in the jQuery doc. It gives the position relative to the document, not to the parent. You perhaps have .offset()
and .position()
confused. If you want the position in the window instead of the position in the document, you can subtract off the .scrollTop()
and .scrollLeft()
values to account for the scrolled position.
Here's an excerpt from the doc:
The .offset() method allows us to retrieve the current position of an
element relative to the document. Contrast this with .position(),
which retrieves the current position relative to the offset parent.
When positioning a new element on top of an existing one for global
manipulation (in particular, for implementing drag-and-drop),
.offset() is the more useful.
To combine these:
var offset = $("selector").offset();
var posY = offset.top - $(window).scrollTop();
var posX = offset.left - $(window).scrollLeft();
You can try it here (scroll to see the numbers change): http://jsfiddle.net/jfriend00/hxRPQ/
Javascript get absolute position of a div
The getBoundingClientRect gives coordinates in viewport coordinates (or coordinates relative to the visible content shown in your browser window). With absolute positioning, you need document coordinates. To convert viewport coordinates to document coordinates, add the scroll offsets of the page to the left and top values returned by getBoundingClientRect:
//technique used in JavaScript: Definitive Guide
var scrollOffsets = (function () {
var w = window;
// This works for all browsers except IE versions 8 and before
if (w.pageXOffset != null) return {x: w.pageXOffset, y:w.pageYOffset};
// For IE (or any browser) in Standards mode
var d = w.document;
if (document.compatMode == "CSS1Compat")
return {x:d.documentElement.scrollLeft, y:d.documentElement.scrollTop};
// For browsers in Quirks mode
return { x: d.body.scrollLeft, y: d.body.scrollTop };
}());
//Your code:
var oCell = document.createElement("td");
//changed from height to top and gets document coordinates of position
var top = oCell.getBoundingClientRect().top + scrollOffsets.y;
//changed from right to left
var left = oCell.getBoundingClientRect().left + scrollOffsets.x;
oCell.oBoxPositionTop = top;
oCell.oBoxPositionSide = left;
Related Topics
Is It the Last 'Script' Element the Currently Running Script
Storing Arbitrary Info in HTML Tags for JavaScript
Trying to Set a Span Element Equal to a Variable Value in a Js Rock, Paper, Scissors Game
How to Add HTML Inside a Title Attribute
Phantomjs Page.Content Isn't Retrieving the Page Content
Good Beginners Tutorial to Socket.Io
How to Change the Playing Speed of Videos in HTML5
JavaScript Loading Screen While Page Loads
Disable Zoom on Input Focus in Android Webpage
How to Automatically Close Alerts Using Twitter Bootstrap
Invert Colors of an Image in CSS or JavaScript
JavaScript Hide/Show Div on Checkbox: Checked/Unchecked
CSS and JavaScript: Function to Change Color Effects Hover Style
How to Override Expansionpanelsummary Deep Elements with Styled-Components
How to Make a Rotated Element Height:100% of Its Parent