4

I want to make two events depends on a scale property of a menu. So this is my div:

<div class="starting-point">
    <span class="" style="transform: scaleX(1) scaleY(1); height: 2760.56px; width: 2760.56px; top: -1380.28px; left: -1380.28px;"></span>
</div>

How can I get the transform: scaleX(1) scaleY(1) into a variable? Sometimes the values are scaleX(0) scaleY(0) and I want to perform different actions depending on these values.

I assigned dddd class to that span and I tried this, but there is no scale or something useful in the results.

var style = getComputedStyle(document.getElementsByClassName('dddd')[0])

Thanks a lot.

4 Answers 4

4

Here you go:

var style = getComputedStyle(document.getElementsByClassName('dddd')[0], null);

console.log(style.getPropertyValue('transform'));

Edit 1:

If you prefer not to add the class, you can change your code like this:

var style = getComputedStyle(document.querySelector('.starting-point span'), null);

console.log(style.getPropertyValue('transform'));

Edit 2:

You can change it to use jQuery selector as well:

var style = getComputedStyle($('.starting-point span')[0], null);

console.log(style.getPropertyValue('transform'));
Sign up to request clarification or add additional context in comments.

5 Comments

this is what i was looking for, thanks. But how about if i don't assign any class name to that span? how to get it ? div children are undefined
You can change your selector to something like this: document.querySelector('.starting-point span')
Glad this helped!!
Yes, that method is not a DOM or jQuery method. You can try this instead: getComputedStyle($('.starting-point span')[0], null).getPropertyValue('transform') ;
Edit 2 is exactly what i was looking for :)
3

You could also use a more generic method by using regular expressions.

var matrix = $('.selector').css('transform');
var values = matrix.match(/-?[\d\.]+/g);

This will get all your transform properties you could then get a specific property based on your index value. Such as:

console.log(values[0]);

Comments

3

Since you are adding jquery tag it is cleaner if you used its APIs

var style = $('.starting-point span').attr('style').split(';')[0];

Comments

1

NB: transform_target here for me is the scale;

var variable = $('.classTarget').css('transform','transform_target')[array index]
var newVariable = varaible.style.transform.match(/(-?[0-9\.]+)/g)[array index you want]

console.log(newVariable)

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.