...is a huge pain.
var transform = 'translate3d(0,0,0)';
elem.style.webkitTransform = transform;
elem.style.mozTransform = transform;
elem.style.msTransform = transform;
elem.style.oTransform = transform;
Is there a library/framework/better way to do this? Preferably with just one line of JS?
It's currently late 2015, and the situation has changed slightly. First of all, McBrainy's comment about capitalization above is important. The
webkit
prefix is nowWebkit
, but luckily only used by Safari at this point. Both Chrome and Firefox supportel.style.transform
without the prefix now, and I think IE does as well. Below is a slightly more modern solution for the task at hand. It first checks to see if we even need to prefix our transform property:Afterwards, we can just use a simple one-liner call to update the
transform
property on an element:If you are setting up your workflow with Gulp for example you can use Postcss autoprefixer which is handy tool in solving browser vendor prefixes. It uses JS to transform you css.
You can find the respective vendor prefix using Javascript with the following code-
The above returns an object of the respective browser's vendor prefix.
Saved a lot of duplicate code in my scripts.
Source - David Walsh's blog: https://davidwalsh.name/vendor-prefix
I don't know of any library that does this, but if they are all just prefixes--that is, there is no difference in name or syntax--writing a function yourself would be trivial.
Then you can just use this in most cases.
There's this jquery plugin that take care of it https://github.com/codler/jQuery-Css3-Finalize