Update prefix.md

This commit is contained in:
Angelos Chalaris
2018-03-12 22:29:23 +02:00
committed by GitHub
parent 9cd5323488
commit 0fd0cde099

View File

@ -2,22 +2,18 @@
Returns the prefixed version (if necessary) of a CSS property that the browser supports. Returns the prefixed version (if necessary) of a CSS property that the browser supports.
Use an array of vendor prefix strings and loop through them, testing if `document.body` has one of them defined in its `CSSStyleDeclaration` object, otherwise return `null`. Use `Array.findIndex()` on an array of vendor prefix strings to test if `document.body` has one of them defined in its `CSSStyleDeclaration` object, otherwise return `null`.
Use `String.charAt()` and `String.toUpperCase()` to capitalize the property, which will be appended to the vendor prefix string. Use `String.charAt()` and `String.toUpperCase()` to capitalize the property, which will be appended to the vendor prefix string.
```js ```js
const prefix = property => { const prefix = prop => {
const capitalizedProp = prop.charAt(0).toUpperCase() + prop.slice(1);
const prefixes = ['', 'webkit', 'moz', 'ms', 'o']; const prefixes = ['', 'webkit', 'moz', 'ms', 'o'];
const upperProp = property.charAt(0).toUpperCase() + property.slice(1); const i = prefixes.findIndex(prefix => typeof document.body.style[(prefix ? prefix + capitalizedProp : prop)] !== 'undefined');
for (let i = 0; i < prefixes.length; i++) { return i !== -1 ? i === 0 ? prop : prefixes[i] + capitalizedProp : null;
const prefix = prefixes[i];
const prefixedProp = prefix ? prefix + upperProp : property;
if (typeof document.body.style[prefixedProp] !== 'undefined') return prefixedProp;
}
return null;
} }
``` ```
```js ```js
prefix('transform'); // 'transform' on a supported browser, otherwise 'webkitTransform' or 'mozTransform' or 'msTransform' or 'oTransform' prefix('appearance'); // 'appearance' on a supported browser, otherwise 'webkitAppearance', 'mozAppearance', 'msAppearance' or 'oAppearance'
``` ```