LoginSignup
4
0

More than 3 years have passed since last update.

Material UIのrippleアニメーションを模倣してみた

Posted at

React用のMaterial UIで実装できるボタン押下時のエフェクト。

Image from Gyazo

クリックした地点から波紋が広がるようなアニメーションを見て

これはぜひ実装したい!

と思ったのでやってみた

作ったもの

Image from Gyazo

コードはこちら
CodeSandBox

buttonの構造

はこちら

<button class="base-root">
  <span class="label">button</span>
  <span class="ripple-root">
    // ↓buttonのmousedownで生成↓
    <span class="ripple-ripple ripple-rippleVisible">
      <span class="ripple-child ripple-childLeaving"></span>
    </span>
  </span>
</button>

class名は本家とちょっと変えたけど概ねこんな感じ

ripple-childLeavingはmouseup時に付くっぽい

ripple-rippleの大きさ

rippleの大きさを見てみると
本家は三平方の定理的なさむしんぐを使っておっきな正方形を作ってるっぽいけど
めんどくさいので、適当にbase-rootの2倍でやってみる

と思ったけど普通に半径求めた

// calculation
const rootWidth = rippleRoot.clientWidth;// buttonの幅
const rootHeight = rippleRoot.clientHeight;// buttonの高さ
const radius = Math.sqrt(Math.pow(rootWidth, 2) + Math.pow(rootHeight, 2));// 円の半径
const top = e.layerY - radius;
const left = e.layerX - radius;
rippleRipple.style.width = `${radius * 2}px`;
rippleRipple.style.height = `${radius * 2}px`;
rippleRipple.style.top = `${top}px`;
rippleRipple.style.left = `${left}px`;

これでクリックした地点を中心とした要素が作れた

DOM操作

については何か言うことはない。

コード見てくれ

問題点

連続でクリックするとボタンが白くなっちゃう

本家

Image from Gyazo

模倣

Image from Gyazo

ので解決策知ってる人教えろくださいm(__)m

pulsateを結局使わなかった

本家のコードやcssに時折**-pulsateという文字列があって、
keyframeも設定されてたからどこかで使うんじゃないかと思ってたんだけど
結局使わんかった
こちらもどこで使うのか知ってる人いたら教え(ry

ちなみにpulsateとはどっくんどっくんする的な意味っぽい

4
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0