0

In my css file i say

@media (min-width: 287px) {
    #footer-div
    {
        transform: translateY(93%);
    }
}
@media (min-width: 472px) {
    #footer-div
    {
        transform: translateY(89%);
    }
} 

etc... Now in javascript plain i want to get the value that applies. So in this case 93% or 89%. something like :

var stylenow = document.getElementById("footer-div").style.transform...
3
  • 1
    What do you mean by "get"? What would you expect it to be? Commented Aug 23, 2021 at 10:01
  • Could you explain what you are trying to do with it? because it seems like you already know its going to be 93% and if its going to be dynamic why not set it via javascript in the first place? Commented Aug 23, 2021 at 10:03
  • you can try Element.style.transform but this will give you the whole CSS value Commented Aug 23, 2021 at 10:08

1 Answer 1

2

You could use DomMatrix/WebKitCSSMatrix

var myElement = document.querySelector('.box');

function getTranslateY() {
  var style = window.getComputedStyle(myElement);
  var matrix = new WebKitCSSMatrix(style.transform);
  console.log('translateY: ', `${matrix.m42}%`);
}

document.querySelector('button').addEventListener('click', getTranslateY);
body {
  display: flex
}

.box {
  height: 100px;
  width: 100px;
  background: teal;
  transform: translateY(92%);
}
<div class="box"></div>
<button type="button">get value</button>

Sign up to request clarification or add additional context in comments.

1 Comment

DOMMatrix is the standard name for this.

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.