25

I'm trying to get the -webkit-transform: translateY('') property in a variable.

HTML

<form class="form-con" style="-webkit-transform: translateY(-802px);"></form>

JS

$('.foo').click(function() {
    var current_pull = parseInt($('.form-con').css('transform').split(',')[4]);
});

This is returning '0', instead of the correct value.

3
  • 1
    What browser are you using? In Chrome you have to get the value with .css('-webkit-transform') - your code returns undefined for that function so the split fails. Be wary of cross-browser issues with this Commented Feb 24, 2014 at 12:26
  • I'm using Chrome. Though Jack's answer did the trick. Commented Feb 24, 2014 at 12:27
  • 1
    You'd have better to retrieve setted value from: $('.form-con')[0].style['webkitTransform'] Commented Feb 24, 2014 at 12:30

5 Answers 5

41

You can use:

 var obj = $('.form-con');
 var transformMatrix = obj.css("-webkit-transform") ||
   obj.css("-moz-transform")    ||
   obj.css("-ms-transform")     ||
   obj.css("-o-transform")      ||
   obj.css("transform");
 var matrix = transformMatrix.replace(/[^0-9\-.,]/g, '').split(',');
 var x = matrix[12] || matrix[4];//translate x
 var y = matrix[13] || matrix[5];//translate y
Sign up to request clarification or add additional context in comments.

4 Comments

In what case would matrix[12] and matrix[13] relevant?
@StevenPalinkas: that is in case of 3d transformation where matrix of 4*4 size is defined. Check third property in doc here w3schools.com/cssref/css3_pr_transform.asp
Does it make sense still to differ between '-moz-transform', '-ms-transform', etc. since all browsers are compatible to 'rotate (nnn deg)' ?
Even though I prefer the regex here over the more usual approach I came across to get the values, I did notice that with a particularly small number it strips the e notation from the value - making it become NaN.
34
$('.foo').click(function() {
    var current_pull = parseInt($('.form-con').css('transform').split(',')[5]);
});

1 Comment

Gives me always 0 back
4

I think the answer is here:

function getRotationDegrees(obj) {
    var matrix = obj.css("-webkit-transform") ||
    obj.css("-moz-transform")    ||
    obj.css("-ms-transform")     ||
    obj.css("-o-transform")      ||
    obj.css("transform");
    if(matrix !== 'none') {
        var values = matrix.split('(')[1].split(')')[0].split(',');
        var a = values[0];
        var b = values[1];
        var angle = Math.round(Math.atan2(b, a) * (180/Math.PI));
    } else { var angle = 0; }
    //return (angle < 0) ? angle + 360 : angle;
    return angle;
}

Comments

0

I think the order of properties is indeterminate.

var matrix = $obj.css('transform');
var translate = {};

// translateX 
var matchX = matrix.match(/translateX\((-?\d+\.?\d*px)\)/);
if(matchX) {
  translate.x = matchX[1];
}

// translateY
var matchY = matrix.match(/translateY\((-?\d+\.?\d*px)\)/);
if(matchY) {
  translate.y = matchY[1];
}

console.log(translate);

Comments

0

You can get the transform string using outerHTML rather than matrix3d because it doesn't recalculate vw when resizing:

let imgEl = $('img');
let defaultMatrix = imgEl.prop('outerHTML');
defaultMatrix = getStringProp(defaultMatrix, 'style="', '"');
console.log(defaultMatrix);
let trZ = getStringProp(defaultMatrix, 'translate3d(', ')');
trZ = trZ.split(', ')[2];
console.log(trZ);

function getStringProp(str, first, last) {
  str = str.substring(str.indexOf(first) + first.length, str.length);
  return str.substring(str.indexOf(last) + last.length - 1, 0);
}

Here is the example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.