Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
159
rated 0 times [  161] [ 2]  / answers: 1 / hits: 26350  / 13 Years ago, wed, november 2, 2011, 12:00:00

Say a div has this applied to it:



-webkit-transform: translate3d(0px, -200px, 0px)


How could I retrieve those values with jQuery?


More From » jquery

 Answers
7

The value gets stored either as a matrix or a matrix3d, depending on whether or not the z value was set. Assuming no other transformations, for a 2D matrix, X and Y are the last two values. For a 3D matrix, X, Y, Z, 1 are the last four digits.



You could use a regular expression to get the values:



function getTransform(el) {
var results = $(el).css('-webkit-transform').match(/matrix(?:(3d)(d+(?:, d+)*(?:, (d+))(?:, (d+))(?:, (d+)), d+)|(d+(?:, d+)*(?:, (d+))(?:, (d+))))/)

if(!results) return [0, 0, 0];
if(results[1] == '3d') return results.slice(2,5);

results.push(0);
return results.slice(5, 8);
}

[#89331] Monday, October 31, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emiliano

Total Points: 381
Total Questions: 109
Total Answers: 93

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
emiliano questions
;