Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the position of an element after css3 translation in JavaScript?

Tags:

javascript

css

I saw this posted in 2 different forms on stackoverflow, but the solutions don't work for me.

Essentially, I have an item that I will translate. When I do a obj.style.left or obj.offsetLeft, after the element has been translated, I get 0. Is there anyway I can get the coordinates/position of an element after it has been translated with css3?

I can't use jQuery (because I can't and also because I want to understand the solution, not just use the library without understanding what is happening underneath)

Any ideas?

Thanks much!

like image 490
ant Avatar asked Feb 12 '11 01:02

ant


People also ask

How do you get the position of an element in a JavaScript?

Use the Element. getBoundingClientRect() Function to Get the Position of an Element in JavaScript. The getBoundingClientRect() function produces a DOMRect object containing information about an element's size and position in the viewport.

How do you find the position of an element in HTML?

Use element. getBoundingClientRect() property to get the position of an element. Output: Example 2: In this example, move the pointer over the document to get the position of an element.

How do you position an element on top of another?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).


2 Answers

You can use getBoundingClientRect():

Imagine the following html block:

<div class="centralizer popup">
    <div class="dragger"></div>
</div>

And the style:

body {
    background:#ccc;
}
.centralizer {
    position:absolute;
    top:150px;
    left:170px;
    width:352px;
    height:180px;
    overflow:auto;
    -webkit-transform: translateY(-50%) translateX(-50%);
    -moz-transform: translateY(-50%) translateX(-50%);
    -ms-transform: translateY(-50%) translateX(-50%);
    transform: translateY(-50%) translateX(-50%);
}
.popup {
    background:white;
    box-shadow:0 0 0 2px rgba(0, 0, 0, 0.1), 0 0 15px rgba(0, 0, 0, 0.3);
    border-radius:3px;
}
.dragger {
    background:#eee;
    height:35px;
    border-radius:3px 3px 0 0;
    border-bottom:1px solid #ccc;
}

Now you can use the following javascript to get the correct position:

var popup = document.querySelector('.popup');
var rect = popup.getBoundingClientRect();

console.log("popup.getBoundingClientRect(): \n" + "x: " + rect.left + "\ny: " + rect.top);

You can check the result in the jsfiddle:

I tested on FF, IE and chrome, and it worked on all my tests.

like image 168
Thiago Bustamante Avatar answered Sep 29 '22 23:09

Thiago Bustamante


Ok, hold tight because this is not nice:

window.addEventListener('load', function(){
  var node = document.getElementById("yourid");
  var curTransform = new   WebKitCSSMatrix(window.getComputedStyle(node).webkitTransform);
  console.log(node.offsetLeft + curTransform.m41); //real offset left
  console.log(node.offsetTop + curTransform.m42); //real offset top
});

You can play with it here:

http://jsfiddle.net/duopixel/wzZ5R/

like image 35
methodofaction Avatar answered Oct 01 '22 23:10

methodofaction