最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Getting the transform class property of an element in Javascript - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share edited Jul 21, 2016 at 6:58 Shashank Agrawal 25.8k11 gold badges96 silver badges125 bronze badges asked Jul 21, 2016 at 6:47 Michael CommonsMichael Commons 8224 gold badges12 silver badges31 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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'));

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]);

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

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

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)
发布评论

评论列表(0)

  1. 暂无评论