Skip to content

Instantly share code, notes, and snippets.

@mtmzorro
Last active December 23, 2015 08:09
Show Gist options
  • Save mtmzorro/6605824 to your computer and use it in GitHub Desktop.
Save mtmzorro/6605824 to your computer and use it in GitHub Desktop.
css3 Prefixer
/**
* css3Prefixer
*
* Example: document.getElementById('div').setCss3Style('transition', 'translateY(100px) translateX(500px) scale(3)');
*/
Object.prototype.setCss3Style = function(key, value){
if (typeof this.getAttribute !== 'undefined') {
var currStyle = this.getAttribute('style');
var prefixs = ["-webkit-", "-moz-", "-o-", "-ms-", ""];
var css3 = "";
if (currStyle !== null) {
var styleArr = currStyle.split(';');
var keyReg = new RegExp(key);
for (var i = 0; i < styleArr.length; i++) {
if (styleArr[i].search(keyReg) !== -1) {
styleArr.splice(i, prefixs.length);
break;
}
}
css3 = styleArr.join(';');
}
for (var i = 0; i < prefixs.length; i++) {
css3 += prefixs[i] + key + ':' + value + ';';
}
this.setAttribute('style', css3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment