【发布时间】:2015-06-08 22:34:24
【问题描述】:
我真的很喜欢 React 为你净化事件的方式,所以我很惊讶地发现它们并没有为你的 CSS 样式添加前缀!
无论如何,我开始像这样实现自己的基本前缀:
var prefixes = ["ms", "Moz", "Webkit", "O"];
var properties = [
'userSelect',
'transform',
'transition',
'transformOrigin',
'transformStyle',
'transitionProperty',
'transitionDuration',
'transitionTimingFunction',
'transitionDelay',
'borderImage',
'borderImageSlice',
'boxShadow',
'backgroundClip',
'backfaceVisibility',
'perspective',
'perspectiveOrigin',
'animation',
'animationDuration',
'animationName',
'animationDelay',
'animationDirection',
'animationIterationCount',
'animationTimingFunction',
'animationPlayState',
'animationFillMode',
'appearance',
'flexGrow',
];
function vendorPrefix(property, value) {
var result = {}
result[property] = value
if( properties.indexOf(property) == -1 ){
return result;
}
property = property[0].toUpperCase() + property.slice(1);
for (var i = 0; i < prefixes.length; i++) {
result[prefixes[i] + property] = value;
};
return result;
}
React.prefix = function(obj) {
var result = {};
for(var key in obj){
var prefixed = vendorPrefix(key, obj[key])
for(var pre in prefixed){
result[pre] = prefixed[pre]
}
}
return result;
};
但是我realized a big problem,React 使用对象作为样式并适当地为弹性框添加前缀,您需要为值添加前缀,而不是属性。因此,我不能同时包含以下所有样式:
.page-wrap {
display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6 */
display: -moz-box; /* OLD - Firefox 19- (buggy but mostly works) */
display: -ms-flexbox; /* TWEENER - IE 10 */
display: -webkit-flex; /* NEW - Chrome */
display: flex; /* NEW, Spec - Opera 12.1, Firefox 20+ */
}
任何想法如何解决这个问题?
【问题讨论】:
-
内联样式功能今天是有限的。我建议在这些情况下只使用传统的类/
className。 -
或者你可以检测浏览器版本,但是类可能是要走的路。
-
啊,我明白了。我希望最终取消 CSS 和 CSS 预处理器