虽然并非总是如此,但一个属性(如transition
或animation
)在某些浏览器上工作而在其他浏览器上不工作的最常见原因之一是由于供应商前缀。
什么是厂商前缀?
在 Firefox 4 版本引入之时,CSS 过渡模块规范还处于工作草案状态。实际上,在规范定稿之前(通常是它达到候选推荐阶段),浏览器厂商会向属性、值和 @-规则添加 供应商前缀,以防止兼容性问题,万一规范发生变化。
供应商前缀正是它们名称描述的东西 - 属性或值的供应商特定前缀。它们通常为每个浏览器以特定方式实现,因为属性或值仍处于许多实验阶段(即候选推荐阶段之前),而该阶段是 考虑实现就绪的阶段。
最常见的前缀是 -moz-
(Mozilla Firefox)、-webkit-
(Chrome、Safari 等) 和 -ms-
(Microsoft Internet Explorer),但还有更多。
何时需要使用它们?
这完全取决于您要服务的浏览器、您使用的属性和值以及您在开发网站的哪个阶段。有些网站试图保持当前列表,但它们并不总是准确或及时更新。
以下是一些最常见的前缀属性和值。如果您的项目不支持右侧评论中提到的浏览器,则无需在 CSS 中包含它。
过渡
无前缀的属性有时具有前缀等效项,例如-webkit-transition
。
为了获得最佳的浏览器支持,请参照以下内容:
.foo {
-webkit-transition: <transition shorthand value>;
-moz-transition: <transition shorthand value>;
-o-transition: <transition shorthand value>;
transition: <transition shorthand value>;
}
请注意,在transition
中存在一个-ms-
前缀,但它仅在早期版本的IE10中实现,这些版本已不再可用,因此永远不需要使用。它在IE10 RTM及更高版本中以无前缀方式实现。
转换
.foo {
-webkit-transform: <transform-list>;
-ms-transform: <transform-list>;
transform: <transform-list>;
}
动画
动画需要添加前缀属性和相应的关键帧前缀,如下所示:
.foo {
-webkit-animation: bar;
-moz-animation: bar;
-o-animation: bar;
animation: bar;
}
@-webkit-keyframes bar { }
@-moz-keyframes bar { }
@-o-keyframes bar { }
@keyframes bar { }
弹性盒模型
在类似 弹性盒模型 的情况下,值也可以添加前缀。注意:为了实现最大的浏览器兼容性,在某些浏览器中需要额外添加像 ordinal-group
、flex-flow
、flex-direction
、order
、box-orient
等特定于弹性盒子模型的属性的前缀。
.foo {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-flex: <flex shorthand value>;
-moz-box-flex: <flex shorthand value>;
-webkit-flex: <flex shorthand value>;
-ms-flex: <flex shorthand value>;
flex: <flex shorthand value>;
}
计算
.foo {
width: -webkit-calc(<mathematical expression>);
width: -moz-calc(<mathematical expression>);
width: calc(<mathematical expression>);
}
渐变
请参考CSS-Tricks上的CSS渐变文档以获取更多信息。
.foo {
background-color: <color>;
background-image: url(bar.svg);
background-image: -webkit-gradient(linear, left top, right top, from(<color-stop>), to(<color-stop>));
background-image: -webkit-linear-gradient(left, <color-stop>, <color-stop>);
background-image: -moz-linear-gradient(left, <color-stop>, <color-stop>);
background-image: -o-linear-gradient(left, <color-stop>, <color-stop>);
background-image: linear-gradient(to right, <color-stop>, <color-stop>);
}
请注意,left
和to right
表示同一方向,即从左到右,因此left
和to left
指向相反的方向。有关一些背景信息,请参见此答案。
border-radius(在大多数情况下不需要)
.foo {
-webkit-border-radius: <length | percentage>;
-moz-border-radius: <length | percentage>;
border-radius: <length | percentage>;
}
盒子阴影(大多数情况下不需要)
.foo {
-webkit-box-shadow: <box-shadow shorthand value>;
-moz-box-shadow: <box-shadow shorthand value>;
box-shadow: <box-shadow shorthand value>;
}
如何使用JavaScript实现这些功能?
要在JavaScript中访问带前缀的属性和事件,请使用CSS前缀的驼峰命名法相对应的名称。这也适用于事件监听器,例如foo.addEventListener('webkitAnimationIteration', bar)
(其中foo
是DOM对象,例如document.getElementsById('foo')
)。
foo.style.webkitAnimation = '<animation shorthand value>';
foo.style.mozAnimation = '<animation shorthand value>';
foo.style.oAnimation = '<animation shorthand value>';
前缀工具
在线前缀工具可能有帮助,但不总是可靠的。始终确保在您希望支持的设备上测试项目,以确保每个设备都包含适当的前缀。
CSS 预处理器函数:
JavaScript 前缀函数:
另请参阅: 为什么浏览器会为 CSS 属性创建供应商前缀?