-
-
Save addyosmani/1131285 to your computer and use it in GitHub Desktop.
Get the vendor prefix for a property in a specific context.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getPrefix(prop, context) { | |
var vendorPrefixes = ['moz', 'webkit', 'khtml', 'o', 'ms'], | |
upper = prop.charAt(0).toUpperCase() + prop.slice(1), | |
pref, len = vendorPrefixes.length, | |
q; | |
while (len--) { | |
q = vendorPrefixes[len]; | |
if (context.toString().indexOf('style')) { | |
q = q.charAt(0).toUpperCase() + q.slice(1); | |
} | |
if (q + upper in context) { | |
pref = q; | |
} | |
} | |
if (prop in context) { | |
pref = prop; | |
} | |
return pref ? '-' + pref.toLowerCase() + '-' : ''; | |
} | |
//LocalStorage test | |
console.log(getPrefix('localStorage', window)); | |
//Page Visibility API | |
console.log(getPrefix('hidden', document)); | |
//CSS3 transforms | |
console.log(getPrefix('transform', document.createElement('div').style)); | |
//CSS3 transitions | |
console.log(getPrefix('transition', document.createElement('div').style)); | |
//File API test (very basic version) | |
console.log(getPrefix('FileReader', window)); |
(please ignore if still editing) I've tested out the suggested changes on the existing test cases and the first 4 appear to be failing at the moment in FF5 whilst Chrome/Webkit is working fine. Will play around with the code a little more to find out if there's something obvious breaking!.
Hello,
I was working on the same pb some times ago http://jsfiddle.net/molokoloco/f6Z3D/
I have to test also the empty prefix string, for some old properties like "opacity" that are no more "-moz-opacity"
Also there is a trap with the jQuery method for reading style properties... so i have to call it with
{{{
cssProp[cssPrefix('Transform')] = 'rotate(20deg)';
cssProp[cssPrefix('borderRadius')] = '5px'; // Keep the camelCaze (jQuery like)
}}}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Mathias!.