I want to get translateY
value from the in-line css with the JavaScript.
Here is the in-line value:
style ="transition-property: transform; transform-origin: 0px 0px 0px; transform: translate(0px, -13361.5px) scale(1) translateZ(0px);"
These code give to me the total list in to variable:
var tabletParent = document.getElementById("scroller");
var toTop = tabletParent.style.transform;
console.log(toTop);
console.log
translate(0px, -12358.8px) scale(1) translateZ(0px)
Expecting toTop
as -12358.8px.
If you want the raw value without 'px' you could use a regex like this:
and get the value like this:
Here is a jsFiddle demonstrating.
There are multiple ways. One of the first that come to my mind is parsing the string you get.
For example:
However this will only work with translateZ explicitly defined (not translate3d nor matrix3d). A most consistent way might be getComputedStyle, but this would always get the value in px unit and thus is only truely valid at the time you compute it (a window resize can change it):
See this fiddle showing both methods (note that I've been using chrome for the tests, so I've prefixed your CSS with
-webkit-
).EDIT:
To get translateY, if your visitors browser is recent enough to support getComputedStyle, you could change my
getComputedTranslateZ
function to handle both matrix and matrix3d values. It is simpler than trying to parse every possible css strings (translateY, translate, translate3d, matrix, matrix3d):