CSS3とjqueryを使ってクリックされた所を起点に波紋アニメーションさせる
- カテゴリー:
- CSS
5月ですね。
今年はセルインメイではないんですかね(滝汗
今回の記事の内容とは違うのですが、先日、お仕事でCSSを使い波紋を描くようなアニメーションをさせる必要があったので、いろいろと応用できるのではないかと思い、メモ程度にコードなどを残したいと思います。
以下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 |
#circle { -webkit-animation-name: circle; -webkit-animation-duration: 0.8s; -webkit-animation-iteration-count: 1; -webkit-animation-timing-function: ease-in-out; -webkit-animation-direction: normal; -webkit-animation-delay: 0s; -moz-animation-name: circle; -moz-animation-duration: 0.8s; -moz-animation-iteration-count: 1; -moz-animation-timing-function: ease-in-out; -moz-animation-direction: normal; -moz-animation-delay: 0s; animation-name: circle; animation-duration: 0.8s; animation-iteration-count: 1; animation-timing-function: ease-in-out; animation-direction: normal; animation-delay: 0s; filter: alpha(opacity=0); -moz-opacity: 0; opacity: 0; border: 1px solid #ff0000; -webkit-border-radius: 90px; -moz-border-radius: 90px; border-radius: 90px; box-shadow: 0 0 5px #333333; -moz-box-shadow: 0 0 5px #333333; -webkit-box-shadow: 0 0 5px #333333; position: absolute; } @keyframes circle { 0% { margin: 0; width: 0px; height: 0px; filter: alpha(opacity=100); -moz-opacity: 1.0; opacity: 1.0; } 100% { margin: -78px 0 0 -78px; width: 156px; height: 156px; filter: alpha(opacity=0); -moz-opacity: 0; opacity: 0; } } @-webkit-keyframes circle { 0% { margin: 0; width: 0px; height: 0px; filter: alpha(opacity=100); -moz-opacity: 1.0; opacity: 1.0; } 100% { margin: -78px 0 0 -78px; width: 156px; height: 156px; filter: alpha(opacity=0); -moz-opacity: 0; opacity: 0; } } |
念のため、ベンダープリフィックス入れてるんで、かなりコード自体は長く感じてしまうかもしれません。
#circleがアニメーションを適用させるためのボックスとなりborder、border-radiusの設定で赤いラインの入った丸を描きます。
box-shadowはおまけです。
opacityが0になっているのは、アニメーション終了時に非表示の状態にするためです。※他の方法もあるとは思います。
animation-******とついているのがアニメーションに関する設定なのですが、circleという名前のアニメーションを適用するように設定し、あとは、アニメーションさせる回数や速度、イージングの設定などを行っています。
@keyframes circleの中がそのアニメーションの内容で縦横0pxの円が0.8sかけて156pxに大きくなりながら、opacityが1から0に変化していきます。
続いてjqueryの記述です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$(function(){ $("#wrapper").append("<div id='circle'></div>"); $("#circle").hide(); $("#wrapper").click(function(e){ var px=e.pageX; var py=e.pageY; $("#circle").hide(); $("#circle").css("top",py).css("left",px).show(); }); }) |
上記では、#wrapper内がクリックされた時に処理を行っています。
処理の内容はまずはじめに#wrapperの中に波紋アニメーション用の#circleを挿入し、非表示にしています。
そして、#wrapper内がクリックされたら、クリックされた位置を取得し、#circleのtopとleftにその取得した座標を挿入し、表示させる。
主要なブラウザでは、動作の確認ができたのですが、IEだと比較的新しいバージョンでも動作しないようです。
もっとうまい記述方法もあるとは思いますが、アレンジしていただければと思いますm(。。)m
サンプルはこちら