lessを使ってる時にキーフレームアニメーションを簡単に書く方法がないか調べていたら教えてくれているページがあったのでメモ
参考:LESS CSS でキーフレームアニメーションを楽に書く
- // 今回作ったのは、ピコピコ動くアニメーション
- // まずはmix-inでアニメーションを指定
- .pikopiko() {
- @_r: 10deg;
- from { .rotate(0); }
- 5% { .rotate(@_r); }
- 10% { .rotate(@_r * -1); }
- 15% { .rotate(@_r); }
- 20% { .rotate(@_r * -1); }
- 23% { .rotate(0deg); }
- to { .rotate(0deg); }
- }
-
- //作ったアニメーションを各ブラウザ用にプレフィックスをつけて指定
- //現在、キーフレームアニメーションでプレフィックスが必要なのはwebkitだけらしい
- @keyframes pikopiko { .pikopiko(); }
- @-webkit-keyframes pikopiko { .pikopiko(); }
-
- // アニメーションを簡単に指定するmix-inも作成
- .animation(@name, @duration: 300ms, @delay: 0, @ease: ease) {
- -webkit-animation: @name @duration @delay @ease;
- -moz-animation: @name @duration @delay @ease;
- -ms-animation: @name @duration @delay @ease;
- -o-animation: @name @duration @delay @ease;
- animation: @name @duration @delay @ease;
- }
-
- .animation-count(@count) {
- animation-iteration-count: @count;
- -webkit-animation-iteration-count: @count;
- -moz-animation-iteration-count: @count;
- -o-animation-iteration-count: @count;
- -ms-animation-iteration-count: @count;
- }
-
- //アニメーションを設定する
- .element {
- .animation(pikopiko, 4s);
- .animation-count(infinite);
- }
// 今回作ったのは、ピコピコ動くアニメーション
// まずはmix-inでアニメーションを指定
.pikopiko() {
@_r: 10deg;
from { .rotate(0); }
5% { .rotate(@_r); }
10% { .rotate(@_r * -1); }
15% { .rotate(@_r); }
20% { .rotate(@_r * -1); }
23% { .rotate(0deg); }
to { .rotate(0deg); }
}
//作ったアニメーションを各ブラウザ用にプレフィックスをつけて指定
//現在、キーフレームアニメーションでプレフィックスが必要なのはwebkitだけらしい
@keyframes pikopiko { .pikopiko(); }
@-webkit-keyframes pikopiko { .pikopiko(); }
// アニメーションを簡単に指定するmix-inも作成
.animation(@name, @duration: 300ms, @delay: 0, @ease: ease) {
-webkit-animation: @name @duration @delay @ease;
-moz-animation: @name @duration @delay @ease;
-ms-animation: @name @duration @delay @ease;
-o-animation: @name @duration @delay @ease;
animation: @name @duration @delay @ease;
}
.animation-count(@count) {
animation-iteration-count: @count;
-webkit-animation-iteration-count: @count;
-moz-animation-iteration-count: @count;
-o-animation-iteration-count: @count;
-ms-animation-iteration-count: @count;
}
//アニメーションを設定する
.element {
.animation(pikopiko, 4s);
.animation-count(infinite);
}
すげー記述量減った!
ありがたし。
自分1人で作業するならcompassを使うんだけど、黒い画面が苦手な人もいる場合はguiのあるlessで作業してる。
0 件のコメント:
コメントを投稿