简体   繁体   English

编写CSS转换的最简洁方法

[英]Most concise way to write a CSS transition

Is there a more concise way to write the following code? 是否有更简洁的方法来编写以下代码?

element{
    transition: all 700ms linear,
        transform 700ms cubic-bezier(0.4, 0.25, 0.14, 1.5),
        background 700ms cubic-bezier(0.4, 0.25, 0.14, 1.5);
}

What I'm trying to do is to apply a diferent transition timing function to transform and background properties, while using a linear one for the rest of the properties. 我要做的是将不同的转换计时功能应用于转换和背景属性,而对其余属性使用线性转换计时功能。 This is working right, but I'm trying to keep it as DRY as possible. 这是正常的,但我试图尽可能保持干燥

There's not much you can do here. 你在这里做的并不多。 You could specify the transition duration separately: 您可以单独指定转换持续时间:

element{
    transition: all linear,
        transform cubic-bezier(0.4, 0.25, 0.14, 1.5),
        background cubic-bezier(0.4, 0.25, 0.14, 1.5);
    transition-duration: 700ms;
}

But that's about it. 但那是关于它的。 You can't specify your custom curve only once due to the way comma-separated transition values work. 由于逗号分隔的转换值的工作方式,您无法仅指定一次自定义曲线。 The entire list of values is repeated in the order they were specified, rather than having the last value be repeated infinitely. 整个值列表按指定顺序重复,而不是无限重复最后一个值。

That is to say, if you do this: 也就是说,如果你这样做:

element{
    transition-property: all, transform, background;
    transition-duration: 700ms;
    transition-timing-function: linear, cubic-bezier(0.4, 0.25, 0.14, 1.5);
}

What happens is that the missing value for transition-timing-function is filled as linear , not your custom curve. 发生的情况是, transition-timing-function的缺失值填充为linear ,而不是自定义曲线。 The result will not match your intended transition for background . 结果与您预期的background转换不符。

And if you try to take advantage of that by changing the order of transition properties like so, such that the missing value for background takes on your custom curve instead: 如果您尝试通过更改过渡属性的顺序来利用这一点,那么background的缺失值将取代您的自定义曲线:

element{
    transition-property: transform, all, background;
    transition-duration: 700ms;
    transition-timing-function: cubic-bezier(0.4, 0.25, 0.14, 1.5), linear;
}

What happens is that the all transition will override the transform transition, even though you specified it separately, because the all comes later and it includes any previously-listed properties. 会发生的是,即使您单独指定了转换, all转换也将覆盖 transform转换,因为all后来包含任何先前列出的属性。

Well, the most DRY way would be to use something like SASS and write a mixin, then use that instead of copy pasting that code everywhere. 好吧,最干的方法是使用类似SASS的东西并编写一个mixin,然后使用它而不是复制粘贴到处的代码。 Another suggestion I'll make is to avoid using all , and instead enumerate the properties you'd like animated via the linear transition. 我要做的另一个建议是避免使用all ,而是通过线性转换枚举你想要设置动画的属性。 While this might be more verbose, I'm willing to bet it'll be way more performant, especially if you ever put graphically demanding things like box-shadow into the element's style. 虽然这可能更加冗长,但我愿意打赌它会更加高效,特别是如果你曾经将像box-shadow一样的图形要求的东西放入元素的风格中。

So, while your way would work, I feel like the more unique transitions you have, the more valuable this formulation becomes: 因此,虽然您的方式可行,但我觉得您拥有的过渡更独特,此配方变得越有价值:

element {
    transition-duration: 2s;
    transition-property: all, background, transform;
    transition-timing-function: linear, cubic-bezier(0.4, 0.25, 0.14, 1.5), cubic-bezier(0.4, 0.25, 0.14, 1.5);
}

Of course, I recommended enumerating properties, to avoid costly and unsolicited animations. 当然,我建议枚举属性,以避免昂贵和未经请求的动画。 In that case, something like this makes more sense: 在这种情况下,这样的事情更有意义:

element {
    transition-duration: 2s;
    transition-property: height, background, transform, width;
    transition-timing-function: linear, cubic-bezier(0.4, 0.25, 0.14, 1.5), cubic-bezier(0.4, 0.25, 0.14,1.5), linear;
}

Edit: as mentioned by @BoltClock, I was wrong about everything below this bold text, so please disregard it. 编辑:正如@BoltClock所提到的,我对这个粗体文本下面的所有内容都错了,所以请忽略它。 The comma separated arguments are applied again the order they were originally specified - neither the first nor the last repeats for additional transition-property values. 逗号分隔的参数再次按照它们最初指定的顺序应用 - 既不是第一次也不是最后一次重复transition-property值。 I do still think what I said about not using all is important advice for performance and future proofed scoping though! 我仍然认为我所说的不使用all内容对于性能和未来证明的范围来说是重要的建议!

Take note of how I ordered the arguments: if there are more arguments for transition-property than transition-timing-function , the all of the extra properties will default to the first value listed for transition-property as their default. 请注意我如何排序参数:如果transition-property参数多于transition-timing-function ,则所有额外属性将默认为transition-property列出的第一个值作为其默认值。 So, put a linear (default) value first, then enumerate all your unique timing functions to match the correct property, and then any properties you amend to the end (like width ) will automatically default to linear ! 因此,首先放置一个linear (默认)值,然后枚举所有独特的计时函数以匹配正确的属性,然后您修改到最后的任何属性(如width )将自动默认为linear So, even if you add 5 more properties to the end of your property list, only background and transform will have their unique cubic-bezier timing functions. 因此,即使您在属性列表的末尾添加了5个属性,只有backgroundtransform将具有其独特的cubic-bezier定时函数。 For example: 例如:

element {
    transition-duration: 2s;
    transition-property: height, background, transform, width, oneProp, twoProp, threeProp, etcProp;
    transition-timing-function: linear, cubic-bezier(0.4, 0.25, 0.14, 1.5), cubic-bezier(0.4, 0.25, 0.14,1.5);
}

Everything in the above uses the linear timing function except background and transform . 上面的所有内容都使用除backgroundtransform之外的linear定时功能。 I feel like this is a good compromise between performance and DRY CSS. 我觉得这是性能和DRY CSS之间的良好折衷。 I made a jsFiddle for you to check out the various options - comment out the different CSS to try each way: 我为你制作了一个jsFiddle来查看各种选项 - 注释掉不同的CSS来尝试各种方式:

http://jsfiddle.net/530cumas/4/ http://jsfiddle.net/530cumas/4/

To the best of my CSS knowledge (after a quick google), your current method is the shortest (and, therefore, the most "DRY") way to do what you want. 就我的CSS知识而言(在快速谷歌之后),您当前的方法是最短的(因此,最“干”)方式来做你想要的。 Granted, I'm not a CSS guru, so I could be totally wrong. 当然,我不是CSS大师,所以我可能完全错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM