Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the transform class property of an element in Javascript

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.

like image 250
TrulyXax Avatar asked Dec 19 '22 15:12

TrulyXax


2 Answers

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'));
like image 54
Shashank Agrawal Avatar answered May 05 '23 08:05

Shashank Agrawal


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]);
like image 40
Alan Avatar answered May 05 '23 07:05

Alan