2

I know, with CSS, we have to use vendor prefixes such as -ms-, -moz-, -webkit- and -o- for transform property. And for animation property just -moz-, -webkit- and -o-. I want to know how to set each of them from Javascript? And transition property also.

Looking for something like this -

object.style.vendorTransform     // whole list
object.style.vendorAnimation     // whole list
object.style.vendorTransition    // whole list

Thanks in advance.

Note- I don't need any plugin, I need the pure Javascript code.

2
  • I just wanted to ask how to write code using vendor prefixes from javascript? I am confused about the letter case, like for transform and with moz prefix - object.style.MozTransform - is true OR object.style.mozTransform is true?? and similarly for webkit, ms and o prefixes.. Commented Jun 5, 2015 at 19:43
  • Note that jQuery from version 1.8 automatically determines the required vendor prefix. See this question: stackoverflow.com/questions/17487716/… Commented Jun 5, 2015 at 19:44

3 Answers 3

2

When I understand your question correctly you want to do the transform in JS, and not CSS (in that case I'd recommend SASS with the compass framework)

You could do it like this, so the right prefix is used:

var sty = ele.style, transform, transforms = ["webkitTransform", "MozTransform", "msTransform", "OTransform", "transform"];
for (var i in transforms) {
    if (transforms[i] in sty) {
        transform = transforms[i];
        break;
    }
}

and later on:

ele.style[transform] = "whatever transform you like to do";

So you do not have to set each of them, and of course you can reuse the "transform" variable for other elements, so the check only has to be done once.

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

2 Comments

what I wanted was the list of all vendor prefixes with correct case to be used in JS. like I was confused whether set object.msTransform OR object.MsTransform.. that's it.. In your answer I found those.. Thanks.
Third line of the code example has to be if (transforms[i] in sty) { — check against the value instead of the index.
0

Less is a pretty neat JavaScript tool you can use to do this.

1 Comment

I've no experience with Less, Sass, compass, etc. Neither I have read much about them. I'm just a beginner.
0

Try something like this:

// es6
function animate(element, transformValue = 'none', transitionValue = 'none') {
  const prefixes = ['webkit', 'moz', 'ms', 'o', '']

  if (element) {
    for (let value of prefixes) {    
      element.style[value + 'Transform'] = transformValue
      element.style[value + 'Transition'] = transitionValue
    }
  }
}

const el = document.getElementById('someId')
animate(el, 'translate3d(200px, 0, 0)', 'transform 400ms ease-out')

2 Comments

Made me smile, but all the browsers that do support ES6 syntax and e.g for..of.. loops, do support these CSS properties without vendor prefix ;-)
@Kaiido, what about babel and old browsers supporting?

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.