2020年11月13日金曜日

マウスオーバーした時にアニメーションを付ける

指定した要素をマウスオーバー(ホバー)した時にアニメーションを付ける。

デモページを表示

CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* bounce */
.bounce {  /* http://anicollection.github.io/#/attention_seekers/bounce */
  animation-duration: 1s;
  animation-fill-mode: both;
  animation-name: bounce;
  transform-origin: center bottom
}
 
@keyframes bounce {
  0%, 100%, 20%, 53%, 80% {
    transition-timing-function: cubic-bezier(0.215, .61, .355, 1);
    transform: translate3d(0, 0, 0)
  }
  40%,
  43% {
    transition-timing-function: cubic-bezier(0.755, .050, .855, .060);
    transform: translate3d(0, -30px, 0)
  }
  70% {
    transition-timing-function: cubic-bezier(0.755, .050, .855, .060);
    transform: translate3d(0, -15px, 0)
  }
  90% {
    transform: translate3d(0, -4px, 0)
  }
}
 
/* rubberBand */
.rubberBand {  /* http://anicollection.github.io/#/attention_seekers/rubberBand */
  animation-duration: 1s;
  animation-fill-mode: both;
  animation-name: rubberBand
}
 
@keyframes rubberBand {
  0% {
    transform: scale3d(1, 1, 1)
  }
  30% {
    transform: scale3d(1.25, .75, 1)
  }
  40% {
    transform: scale3d(0.75, 1.25, 1)
  }
  50% {
    transform: scale3d(1.15, .85, 1)
  }
  65% {
    transform: scale3d(.95, 1.05, 1)
  }
  75% {
    transform: scale3d(1.05, .95, 1)
  }
  100% {
    transform: scale3d(1, 1, 1)
  }
}
 
/* tada */
.tada {  /* http://anicollection.github.io/#/attention_seekers/tada */
  animation-duration: 1s;
  animation-fill-mode: both;
  animation-name: tada
}
 
@keyframes tada {
  0% {
    transform: scale3d(1, 1, 1)
  }
  10%,
  20% {
    transform: scale3d(.9, .9, .9) rotate3d(0, 0, 1, -3deg)
  }
  30%,
  50%,
  70%,
  90% {
    transform: scale3d(1.1, 1.1, 1.1) rotate3d(0, 0, 1, 3deg)
  }
  40%,
  60%,
  80% {
    transform: scale3d(1.1, 1.1, 1.1) rotate3d(0, 0, 1, -3deg)
  }
  100% {
    transform: scale3d(1, 1, 1)
  }
}
補足
  • マウスオーバー(ホバー)した時に設定するアニメーションのクラス( bounce, rubberBand, tada )を用意しておく。
  • レイアウトが崩れる場合は リセットCSS・共通CSS の内容を確認。

jQuery

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$(function(){
 
  $('[data-hover-animation]').on('mouseover', function(){
    var t = $(this);
    var c = t.attr('data-hover-animation');
    t.addClass(c);
  })
     
  $('[data-hover-animation]').on('mouseout', function() {
    var t = $(this);
    var c = t.attr('data-hover-animation');
    t.removeClass(c);
  });
 
});
補足
  • マウスオーバー(ホバー)した時に data-hover-animation 属性に設定したアニメーションのクラスを追加し、マウスアウトしたら追加されたアニメーションのクラスを削除する。
  • デモページで使用している jQuery のバージョンは 1.12.4 。

HTML

1
2
3
4
5
<ul>
  <li data-hover-animation="bounce"><span class="btn">映画の一覧を表示</span></li>
  <li data-hover-animation="rubberBand"><span class="btn">音楽の一覧を表示</span></li>
  <li data-hover-animation="tada"><span class="btn">ドラマの一覧を表示</span></li>
</ul>
補足
  • マウスオーバー(ホバー)した時のアニメーションを data-hover-animation 属性に指定する。

関連投稿