这是一个非常简单的问题,但是我找不到关于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);
}

正如您所看到的,转换属性正在相互覆盖。现在,文本阴影会动画化,但颜色不会。我如何让他们同时动画?谢谢你的回答。


当前回答

下面是一个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;
}

其他回答

在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

在所有支持转换的浏览器中,转换属性以逗号分隔:

.nav a {
  transition: color .2s, text-shadow .2s;
}

Ease是默认的计时函数,所以您不必指定它。如果你真的想要线性,你需要指定它:

transition: color .2s linear, text-shadow .2s linear;

这开始变得重复,所以如果你打算在多个属性中使用相同的时间和计时函数,最好继续使用各种过渡-*属性,而不是简写:

transition-property: color, text-shadow;
transition-duration: .2s;
transition-timing-function: linear;

下面的代码允许同时进行多个转换:

-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;

例如:http://jsbin.com/omogaf/2

编辑:我在犹豫是否要删除这篇文章。就理解CSS语法而言,让人们知道所有的声明都存在是件好事,有时这可能比上百万个单独的声明更好,这取决于您的CSS结构。另一方面,它可能会有性能损失,尽管我还没有看到任何数据支持这一假设。现在,我就不谈这个问题了,但我希望人们知道这是一个混合的袋子。

原来的帖子:

你也可以简单地显着:

.nav a {
    transition: all .2s;
}

FWIW:如果没有指定,则暗示all,因此transition: .2s;会把你送到相同的地方。

下面是一个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;
}