下面是否有更好更简单的方法来编写不透明度的缓入效果?
CSS:
.button-hover {
font-family: arial black;
font-size: 100px;
color: #000;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
opacity: 1;
}
.button-hover:hover {
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
opacity: 0.5;
}
正如你所看到的那样,我重复这些行两次似乎并不理想:
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
HTML:
<div class="container">
<a href="#" class="button-hover">HOVER ME</a>
</div>
答案 0 :(得分:7)
不要重复转换规则。 CSS pre-processors可以帮助提供商前缀,但您真的不需要(也不应该)重复{的转换声明{1}}。只需在元素的默认状态下将它们设置为一次,如下所示:
node_modules
:hover
答案 1 :(得分:3)
SASS &amp; LESS 可以让您轻松上手。您可以使用 SASS&amp; LESS Mixins 为此。
示例(SASS):
/* Create a Mixin (SASS) */
@mixin transition($property, $time, $method) {
-webkit-transition: $property $time $method;
-moz-transition: $property $time $method;
-ms-transition: $property $time $method;
-o-transition: $property $time $method;
transition: $property $time $method;
}
/* Include this Mixin (SASS) */
.button-hover:hover {
@include transition(opacity, 1s, ease-in-out);
}
示例(LESS):
/* Create a Mixin (LESS) */
.transition(@property, @time, @method) {
-webkit-transition: @arguments;
-moz-transition: @arguments;
-ms-transition: @arguments;
-o-transition: @arguments;
transition: @arguments;
}
/* Include this Mixin (LESS) */
.button-hover:hover {
.transition(opacity, 1s, ease-in-out);
}
这将转换为 CSS :
.button-hover:hover {
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-ms-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
答案 2 :(得分:0)