0 / 0

CSS3 FOR
TOMORROW
- CSSアニメーション-

M高津戸壮

自己紹介

高津戸 壮といいます

フロントエンドエンジニア

HTML/CSSコーディングや、
JavaScriptによるUI実装を主な仕事にしています。

Twitter:@Takazudo mail:takazudo@gmail.com

株式会社ピクセルグリッド

HTML~CSS~JavaScript中心に
フロント側の実装を主な業務としています。

http://www.pxgrid.com/

CSS3のアニメーション機能をご紹介します。

※注意点

本日紹介する内容は、主としてAppleが提案し、採用の検討が進められている項目が殆どです。現状、CSSアニメーションの実装が進んでいるブラウザはSafari, Google Chromeになるため、このプレゼンテーション内では、webkitのvendor prefixを付けた形でご紹介させていただきます。

div{
    transition         : all .5s; /* CSS3 */
    -webkit-transition : all .5s; /* webkitの独自実装 */
}

ちなみにこのスライドは
CSS3とJavaScriptで作られています。

Transitions

値の変化が起こるとき、
アニメーションを付けることができます。

transition = 移り変わり,移行, 変化

マウスオーバーで背景色が変化。

これは <a href="#">リンク</a> です。
a{
   background-color: #ff0000;
}
a:hover{
   background-color: #00ff00;
}
これはリンクです。

transitionをつけてみます。

これは <a href="#">リンク</a> です。
a{
   background-color: #ff0000;
   -webkit-transition: background-color 1s;
}
a:hover{
   background-color: #00ff00;
}
これはリンクです。

classを付け変えることでも変化。

img{ -webkit-transition:all 0.5s; }
img.dietOk{ width:100px; height:300px; }
img.normal{ width:250px; height:250px; }
img.dietNG{ width:800px; height:200px; }

The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.

CSSだけで手軽に
アニメーションが可能に。

Transforms

transform = 変形

-webkit-transform: translateX(400px)

-webkit-transform: translateY(-200px)

-webkit-transform: scale(2)

-webkit-transform: rotate(30deg)

-webkit-transform: skew(30deg);

組み合わせも可能。

img{
   -webkit-transform: translateX(400px) rotate(330deg) scale(1.5);
}

transitionと組み合わせると…

div img{ -webkit-transition: all 0.5s; }
div:hover img{
   -webkit-transform: translateX(400px) rotate(330deg) scale(1.5);
}

要素の配置が自由自在に。

Animations

より複雑なアニメーションが手軽に。

/* アニメーションを定義 */
@-webkit-keyframes 'pigeon1' {
    from{ -webkit-transform: translateX(0); }
    33%{ -webkit-transform: translateX(10px); }
    66%{ -webkit-transform: translateX(-10px); }
    to{ -webkit-transform: translateX(0); }
}
img{
    -webkit-animation-name: 'pigeon1'; /* アニメーション名を指定 */
    -webkit-animation-iteration-count: infinite; /* 回数:無限 */
    -webkit-animation-duration: 0.1s; /* 0.1秒で */
}

Demo (fademenu)

@-webkit-keyframes 'menuReveal' {
   from{
      opacity:0;
      -webkit-transform: translateY(3em);
   }
   to{
      opacity:1;
      -webkit-transform: translateY(0);
   }
}
ul{
   -webkit-animation-name: 'menuReveal';
   -webkit-animation-iteration-count:1; /* 1回 */
   -webkit-animation-duration:1s;
}

Demo (gallery)

ul li{
   -webkit-animation-name: 'imgReveal';
   -webkit-animation-iteration-count: 1;
   -webkit-animation-duration: 1s;
}
@-webkit-keyframes 'imgReveal' {
   from{ opacity: 0; -webkit-transform: translateY(-100px); }
   to{ opacity: 1; -webkit-transform: translateY(0); }
}

+JavaScriptで0.1秒おきにimgを作成

Demo (tagfall)

@-webkit-keyframes 'tagFall' {
from{ -webkit-transform: translateY(0) rotate(0) scale(0) skewY(0); }
30%{ -webkit-transform: translateY(0) rotate(-720deg) scale(.5) skewY(0); }
93%{ -webkit-transform: translateY(600px) rotate(0) scale(4) skewY(0); }
to{ -webkit-transform: translateY(600px) rotate(0) scale(4) skewY(90deg); }
}
a:hover{
   position:relative; color:#fff; background:#000;
}
a.animate{
   -webkit-animation-name: 'tagFall';
   -webkit-animation-iteration-count:infinite;
}
a.animate:hover{ -webkit-animation-play-state:paused; }

+JavaScriptで -webkit-animation-duration をずらして指定

animation-play-state

細かいアニメーションも自由に!

JavaScriptと
CSSアニメーション

JavaScriptでもアニメーションできるし…

jQuery!

$('#animationTest').click(function(){
    $('#box').animate({
        left: '400px'
    },1000);
});

裏では時間間隔を空けながらスタイルを指定しています。

・・・
box.style.left = "300px";
box.style.left = "301px";
box.style.left = "302px";
box.style.left = "303px";
box.style.left = "304px";
box.style.left = "305px";
box.style.left = "306px";
・・・

ライブラリを使えば簡単にできますが、実はかなり原始的な方法。

transitionが使えれば、classを変えるだけ。
後はブラウザがよしなにやってくれます。

$('#animationTest').click(function(){
    $('#box').addClass('toRight');
});
#box{ -webkit-transition: left 1s; }
#box.toRight{ left: 400px; }

animationでも同じことができます。

$('#animationTest').click(function(){
    $('#box').addClass('toRight');
});
@-webkit-keyframes 'toRight' {
   from{ left:60px } to{ left:400px }
}
#box.toRight{
   left:400px;
   -webkit-animation-name: 'toRight';
   -webkit-animation-iteration-count: 1;
   -webkit-animation-duration: 1s;
}

ライブラリを使えばアニメーションできますが…

自由で管理性の高い
アニメーションが可能に。

アニメーションとの
付き合い方

こういうのとか…

CSS3!!!

サイトで使いますか…?

Writing HTML: Lesson 17: Don't Blink
by Maricopa Center for Learning and Instruction (MCLI)

CSS3 FOR
TOMORROW

unobtrusive animation
出しゃばらないアニメーション

素敵なUIを素敵な
CSSアニメーションで。

参考

CSS Transitions Module Level 3
http://www.w3.org/TR/css3-transitions/
CSS 2D Transforms Module Level 3
http://www.w3.org/TR/css3-2d-transforms/
CSS Animations Module Level 3
http://www.w3.org/TR/css3-animations/
Surfin' Safari - CSS Animation (1)
http://webkit.org/blog/138/css-animation/
Surfin' Safari - CSS Animation (2)
http://webkit.org/blog/324/css-animation-2/
Dev.Opera - CSS3 transitions and 2D transforms
http://dev.opera.com/articles/view/css3-transitions-and-2d-transforms/

終わり