这是一个非常直截了当的问题,但我找不到关于CSS过渡属性的非常好的文档。这是CSS片段:
.nav a
{
text-transform:uppercase;
text-decoration:none;
color:#d3d3d3;
line-height:1.5 em;
font-size:.8em;
display:block;
text-align:center;
text-shadow: 0 -1.5em 0 rgba(255, 255, 255, 0.15);
-webkit-transition: color .2s linear;
-moz-transition: color .2s linear;
-o-transition: color .2s linear;
transition: color .2s linear;
-webkit-transition: text-shadow .2s linear;
-moz-transition: text-shadow .2s linear;
-o-transition: text-shadow .2s linear;
transition: text-shadow .2s linear;
}
.nav a:hover
{
color:#F7931E;
text-shadow: 0 1.5em 0 rgba(247, 147, 30, 0.15);
}
如您所见,过渡属性会相互覆盖。就目前而言,文本阴影将动画,但不是颜色。我怎样让他们同时动画?谢谢你的回答。
答案 0 :(得分:502)
转换属性在所有支持转换的浏览器中以逗号分隔:
.nav a {
transition: color .2s, text-shadow .2s;
}
ease
是默认的计时功能,因此您无需指定它。如果您真的想要linear
,则需要指定它:
transition: color .2s linear, text-shadow .2s linear;
这开始变得重复,所以如果您要在多个属性中使用相同的时间和计时功能,最好继续使用各种transition-*
属性而不是速记:
transition-property: color, text-shadow;
transition-duration: .2s;
transition-timing-function: linear;
答案 1 :(得分:35)
您还可以简单地使用:
.nav a {
-webkit-transition: all .2s;
}
答案 2 :(得分:29)
以下内容将允许同时进行多次转换:
-webkit-transition: color .2s linear, text-shadow .2s linear;
-moz-transition: color .2s linear, text-shadow .2s linear;
-o-transition: color .2s linear, text-shadow .2s linear;
transition: color .2s linear, text-shadow .2s linear;
答案 3 :(得分:22)
如果您将所有属性设置为相同的动画,则可以单独设置每个属性,这样您就不会重复代码。
transition: all 2s;
transition-property: color, text-shadow;
此处有更多内容:CSS transition shorthand with multiple properties?
我会避免全部使用属性(transition-property覆盖' all'),因为最终可能会出现意外行为和意外性能下降。
答案 4 :(得分:2)
.nav a {
transition: color .2s, text-shadow .2s;
}
答案 5 :(得分:1)
这是一个LESS mixin,用于同时转换两个属性:
.transition-two(@transition1, @transition1-duration, @transition2, @transition2-duration) {
-webkit-transition: @transition1 @transition1-duration, @transition2 @transition2-duration;
-moz-transition: @transition1 @transition1-duration, @transition2 @transition2-duration;
-o-transition: @transition1 @transition1-duration, @transition2 @transition2-duration;
transition: @transition1 @transition1-duration, @transition2 @transition2-duration;
}
答案 6 :(得分:1)
可以使多个转换设置为持续时间,延迟和定时功能的不同值。要分割不同的转换,请使用,
button{
transition: background 1s ease-in-out 2s, width 2s linear;
-webkit-transition: background 1s ease-in-out 2s, width 2s linear; /* Safari */
}
答案 7 :(得分:0)
还可以避免完全指定属性。
#box {
transition: 0.4s;
position: absolute;
border: 1px solid darkred;
bottom: 20px; left: 20px;
width: 200px; height: 200px;
opacity: 0;
}
#box.on {
opacity: 1;
height: 300px;
width: 500px;
}
答案 8 :(得分:0)
在 Sass
中,您可以使用以下代码实现
@mixin transition($transitions...) {
$unfoldedTransitions: ();
@each $transition in $transitions {
$unfoldedTransitions: append($unfoldedTransitions, unfoldTransition($transition), comma);
}
-webkit-transition: $unfoldedTransitions;
transition: $unfoldedTransitions;
}
@function unfoldTransition ($transition) {
// Default values
$property: all;
$duration: .2s;
$easing: null; // Browser default is ease, which is what we want
$delay: null; // Browser default is 0, which is what we want
$defaultProperties: ($property, $duration, $easing, $delay);
// Grab transition properties if they exist
$unfoldedTransition: ();
@for $i from 1 through length($defaultProperties) {
$p: null;
@if $i <= length($transition) {
$p: nth($transition, $i)
} @else {
$p: nth($defaultProperties, $i)
}
$unfoldedTransition: append($unfoldedTransition, $p);
}
@return $unfoldedTransition;
}
// Usage: @include transition(width, height 0.3s ease-in-out);
所有功劳都归于tobiasahlin
https://gist.github.com/tobiasahlin