twitterのような「いいねボタン」のアニメーション

https://i.gyazo.com/5cc05020be8e8cc9d4f06bfe9fce7c76.gif

手順

アニメーションの付け方

  1. アニメーション用の画像を準備
  2. cssでアニメーションを定義

twitterのいいねボタンはパラパラ漫画みたいに画像を切り替えてる(左から右へスライド)

今回は下記画像を作成した。

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/7ef2d8aa-fa44-415f-b3e5-c8aaec541ee4/Untitled.png

# css
# backgroundにアニメーションの画像を設定
.heart {
  width: 60px;
  height: 60px;
  background: url(heart_animation.png) no-repeat;
  background-size: 2800*0.6px 100*0.6px;
  background-position: 0 0;
  cursor: pointer;
}

# アニメーションの開始と終了の定義
@keyframes heartBlast {
  0% {
    background-position: left;
  }
  100% {
    background-position: right;
  }
}

//HeartAnimationクラスを持つ要素へアニメーションを付与
.HeartAnimation {
  display: inline-block;
  background-position: right;
  -webkit-animation-name: heartBlast;
  animation-name: heartBlast;
  -webkit-animation-duration: .8s;
  animation-duration: .8s;
  -webkit-animation-iteration-count: 1;
  animation-iteration-count: 1;
  -webkit-animation-timing-function: steps(28);
  animation-timing-function: steps(28);
}

今回はアニメーション(いいねボタン)を付けたい箇所に.heartクラスをもつ要素を入れればOK

アニメーションのプロパティ

大変参考になったリンク