ここまで動かせる!楽しいcss アニメーションのサンプル集

TwitterFacebookHatena
  • 公開:2015-3-16
  • 更新:2023-10-26
  • 文章量:7417
  • CSS

TL;DR

小気味よいアニメーションを用いた Web サイトを国内でも多く見かけるようになってきました。CSS3 により画像編集や Flash が減りメンテナンス性も向上しています。2015 年もアニメーションを取り入れるサイトは増えていくでしょう。

今回は CSS アニメーションを使って、面白い動きができるコードをご紹介します。ざっくりとしたソースで申し訳ないです、微調整はお願いします。

エフェクトサンプル

1.アニメのようにテキスト下の画像を移動させる

アニメのタイトルのように、テキスト上に画像を載せて画像のみ移動させる方法。background-clipを使って画像をテキストでくり抜き、アニメーションでbackground-positionの位置を右に移動させます。

<div class="type-mask">サンプルテキスト --------</div>
.type-mask h2 {
  -webkit-animation: scrollmask 10s ease 1.5s infinite;
  -webkit-background-clip: text;
  background-image: url(img/sample.jpg);
  color: transparent;
  font-size: 80px;
}
@-webkit-keyframes scrollmask {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 100% 0;
  }
}

2.映画のように文字や画像に遠近感を出して動かす

rotateXによる X 軸(横)回転だけだと縦に縮んだようにしか見えませんが、遠近感の指定をするperspectiveを使うと、文字や画像のように立体的に見せることができます。perspectiveの数値は小さいほど遠近感が強くなります。

<div class="type-pers-parent">
  <div class="type-pers-child">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi porttitor nisl id est auctor mollis sed non urna. Sed et ornare neque.</div>
</div>
.type-pers-parent {
  perspective: 150px;
  -webkit-perspective: 150px;
  height: 200px;
  background-color: #222;
  overflow: hidden;
}
.type-pers-child {
  text-align: justify;
  padding: 5%;
  -webkit-animation: typetext linear 15s infinite;
  color: #f1c40f;
  letter-spacing: 1px;
}

@-webkit-keyframes typetext {
  0% {
    transform: rotateX(45deg) translate(0, 0);
  }
  100% {
    transform: rotateX(45deg) translate(0, -500px);
  }
}

3.動く斜線プログレスバー

linear-gradientで斜線ストライプを作ったら、background-positionで左右の位置をアニメーションで動かします。

<div class="type-stripe"></div>
.type-stripe {
  height: 10px;
  background: linear-gradient(-45deg, #3498db 25%, #fff 25%, #fff 50%, #3498db 50%, #3498db 75%, #fff 75%, #fff);
  -webkit-animation: stripeBg 10s linear infinite;
  background-size: 20px 20px;
  border: 1px solid #3498db;
}
@-webkit-keyframes stripeBg {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 100% 0;
  }
}

4.文字をバウンドさせながら表示する

文字や画像をバウンドさせながら揃えます。ロゴに使うなどページが表示された時に 1 回だけ動かしたい場合はinfiniteを外して以下のように記述します。ボールが跳ねる時に横と縦を一瞬だけ伸縮させたいので、方向転換したタイミングでscaleを付与しています。

<div class="type-textbound">
  <span class="textbound1">L</span>
  <span class="textbound2">O</span>
  <span class="textbound3">G</span>
  <span class="textbound4">O</span>
</div>
.type-textbound {
  font-size: 60px;
  text-align: center;
}

.textbound1 {
  -webkit-animation: bound 0.5s ease-in-out;
}
.textbound3 {
  -webkit-animation: bound 0.7s ease-in-out;
}
.textbound2 {
  -webkit-animation: bound 0.9s ease-in-out;
}
.textbound4 {
  -webkit-animation: bound 1s ease-in-out;
}

@-webkit-keyframes bound {
  0% {
    transform: scale(0) translate(0, -500px);
  }
  50% {
    transform: scaleX(2) translate(0, 200px);
  }
  70% {
    transform: scaleY(0.5) translate(0, -100px);
  }
  90% {
    transform: scaleX(1.5) translate(0, 50px);
  }
  100% {
    transform: scale(1) translate(0, 0);
  }
}

5.文字を一瞬だけ反射させる

transform: rotateで光の反射を斜めに傾け、色を透過させ文字の上をアニメーションで通過させます。擬似要素である:before にアニメーションを組み込んでます。

<div class="type-shine">SAMPLE</div>
.type-shine {
  font-size: 60px;
  position: relative;
  overflow: hidden;
}
.type-shine:before {
  -webkit-animation: shine 0.6s ease 1.5s;
  content: '';
  position: absolute;
  top: 0;
  left: -250px;
  width: 100%;
  height: 100%;
  transform: rotate3d(0, 0, 1, -45deg) translate3d(0, -120%, 0);
}
@-webkit-keyframes shine {
  0% {
    transform: rotate3d(0, 0, 1, -45deg) translate3d(0, -120%, 0);
    background: rgba(255, 255, 255, 0.5);
  }
  100% {
    transform: rotate3d(0, 0, 1, -25deg) translate3d(0, 150%, 0);
    background: rgba(255, 255, 255, 0.5);
  }
}

6.カウントダウン

before 要素を付け content プロパティでカウンタ名content: counter(number);を指定することで連番を入れます。その後nth-child()とアニメーションanimation-delayを使って遅延させる順序を指定し数字を表示させます。

<div class="type-countdown">
  <div class="type-countdown-list"></div>

  <div class="type-countdown-list"></div>

  <div class="type-countdown-list"></div>

  <div class="type-countdown-list"></div>

  <div class="type-countdown-list"></div>

  <div class="type-countdown-list"></div>

  <div class="type-countdown-list"></div>

  <div class="type-countdown-list"></div>

  <div class="type-countdown-list"></div>

  <div class="type-countdown-list"></div>
</div>
.type-countdown {
  position: relative;
  height: 100px;
  width: 100%;
}

.type-countdown-list {
  counter-increment: number;
  position: absolute;
  -webkit-animation: countdown 1s;
  margin: auto 0;
  width: 100%;
  opacity: 0;
  font-size: 60px;
  text-align: center;
}

.type-countdown-list:before {
  content: counter(number);
}

.type-countdown-list:nth-child(9) {
  -webkit-animation-delay: 1s;
}
.type-countdown-list:nth-child(8) {
  -webkit-animation-delay: 2s;
}
.type-countdown-list:nth-child(7) {
  -webkit-animation-delay: 3s;
}
.type-countdown-list:nth-child(6) {
  -webkit-animation-delay: 4s;
}
.type-countdown-list:nth-child(5) {
  -webkit-animation-delay: 5s;
}
.type-countdown-list:nth-child(4) {
  -webkit-animation-delay: 6s;
}
.type-countdown-list:nth-child(3) {
  -webkit-animation-delay: 7s;
}
.type-countdown-list:nth-child(2) {
  -webkit-animation-delay: 8s;
}
.type-countdown-list:nth-child(1) {
  -webkit-animation-delay: 9s;
}

@-webkit-keyframes countdown {
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

上記のカウントダウンについては以下の書籍に詳しい内容が掲載されています。

CSS アニメーションの種類と違い

7.アニメーション

CSS アニメーションの種類と違いを簡易的ながら書いておきます。

col 1 | col 2 | col 3 ---------- | ----- | --------------------- animation | ループ有り | ページ表示後 即可動 transform | ループ無し | 変形(移動・拡大・回転・傾斜・遠近)が可能 transition | ループ無し | 滑らかに変化させる(移動時間を調整)

8.transform(変形)の種類

transform プロパティは要素に変形効果を付与します。効果を連結する場合は半角スペースで区切って複数の効果を適用させることが可能です。逆回りや縮小する場合は数値を-(マイナス)にします。

transform の種類1
col 1 col 2 col 3 col 4
translate 移動 transform: translate(20px,20px) (右、下)に 20px 移動
rotate 回転 transform: rotate(45deg) (45 度)時計回りに回転
skew 傾斜 skew(0,170deg) (横、縦)に傾斜
scale 縮尺 transform: scale(1.2) (横、縦)に拡大
perspective 遠近 transform: perspective(200) 数値が小さいほど遠近感が強くなる
transform の種類2
col 1 col 2 col 3 col 4
transform-origin 基点 transform-origin: 100% 100%; 左上からの位置
transform-style 描写 transform-style:flat;
transform-style:preserve-3d; 子要素を平らに表示するか

子要素を立体に表示するか backface-visibility | 裏面 | backface-visibility: visible;
backface-visibility: hidden; | 裏面の表示/非表示

他のエフェクト

比較的最近のもので気になったもの、面白いエフェクトを海外サイトからご紹介します。

9.左右に広がるプリローダー

擬似要素にアニメーションで指定しbackground-colorの色と幅を変化させています。

10.文字を遅延させ表示

こちらは、文字を順番に遅らせて消しているエフェクト。それぞれの文字にクラスを付けアニメーションの開始を遅らせるanimation-delayを指定し遅延、全体にtransform: scaleで伸縮させるエフェクトを付与しています。

11.動くテキストリンク

テキストリンクをホバーした際に発生する様々なエフェクト。3D 回転・注釈表示・スライド・波紋など沢山の種類のサンプルが掲載されています。

12.オープニングを連想させるテキスト

映画などのオープニングを連想させるブラーが解除されていくかのような効果。こちらもanimation-delayで文字ごと遅延、rotateYで縦軸に傾けたり、動きを組み合わせて表現しています。

13.モーダルウィンドウ2つ

js を使わずに、クリックでの制御を行うパターン2つ。一つ目はボタンをクリックでポップアップ、2つ目はクリックで全体にオーバーレイ。

14.アウトラインを使ったボタンエフェクト

ボタンにマウスオーバーする際の、少しひねりのあるエフェクト一覧。使いどころは難しいですが傾斜や円の伸縮などアイデアとしては面白いです。

ここまで動かせる!楽しいcss アニメーションのサンプル集