CSS3 で 3Dっぽいボールを作ってバウンドさせてみた
CSS3 で 3Dっぽいボールを作ってバウンドさせてみました.
元ネタは『CREATING AN ANIMATED 3D BOUNCING BALL WITH CSS3』です.
元ネタは英語なのと色々と説明を端折っていたので, 私なりに作っていく過程を jsdo.it を使って Step by Step 形式でまとめてみました.
正月に CSS3 で鏡餅の年賀状を作ったとき(こちら)は ちょっと苦労しましたが, 今回は見た目のわりには手間が少なかったのでぜひみなさんも作ってみてください.
すべて jsdo.it で作っているので実際に動かしたり, fork してコードをイジったりして遊んで下さい.
Step 0 – HTML 部分を作ろう!
ボール用の要素, 影用の要素, それらを囲う要素をマークアップしています.
<div class="ball-wrapper"> <div class="ball"></div> <div class="ball-shadow"></div> </div>
この時点ではまだ何も表示されません. この Step 以降は HTML は一切触りません. CSS のみでスタイリングしていきます.
Step 1 – ボールを表示しよう!
ボールを表示しましょう. CSS プロパティの width, height でサイズを指定し, border-radius を指定することで 円を表示しています.
そして, box-shadow を内側に設定し, background にグラデーションを指定することで 3D 感を出しています.
.ball { position: absolute; width: 120px; height: 120px; border-radius: 70px; background: -webkit-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%); background: -moz-linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%); background: linear-gradient(top, rgba(187,187,187,1) 0%,rgba(119,119,119,1) 99%); box-shadow: inset 0 -5px 15px rgba(255,255,255,0.4), inset -2px -1px 40px rgba(0,0,0,0.4), 0 0 1px #000; }
Step 2 – 位置を調整しよう!
ball クラスを指定している要素を囲う要素 ball-wrapper クラス要素を位置調整することで 簡単にボールの表示位置を変更することができます.
今回はボールのサイズを 120px に指定しているので, position を fixed, left を 50% に設定し, margin でサイズの半分の値である 60px を逆方向にズラすことで中央位置に表示するように調整しています.
.ball-wrapper { position: fixed; width: 120px; height: 300px; margin-left: -60px; left: 50%; top: 20%; }
Step 3 – バウンドさせよう!
jump という名前の keyframes を定義し, ball クラスに animation プロパティとして jump を指定することでアニメーションさせています.
animation について詳しく知りたい方はこちらのエントリーをどうぞ
去年までは webkit しか対応していなかったはずですが, いつの間にか firefox も対応してくれてたみたいです.
.ball { ... -webkit-animation: jump 1s infinite; -moz-animation: jump 1s infinite; animation: jump 1s infinite; }
@-webkit-keyframes jump { 0% { top: 0; -webkit-animation-timing-function: ease-in; } 40% {} 50% { top: 140px; height: 120px; -webkit-animtion-timing-function: ease-out; } 55% { top: 160px; height: 100px; border-radius: 70px/60px; -webkit-animation-timing-function: ease-in; } 65% { top: 110px; height: 120px; border-radius: 50%; -webkit-animation-timing-function: ease-out; } 95% { top: 0; -webkit-animation-timing-function: ease-in; } 100% { top: 0; -webkit-animation-timing-function: ease-in; } } @-moz-keyframes jump { 0% { top: 0; -moz-animation-timing-function: ease-in; } 40% {} 50% { top: 140px; height: 120px; -moz-animtion-timing-function: ease-out; } 55% { top: 160px; height: 100px; border-radius: 70px/60px; -moz-animation-timing-function: ease-in; } 65% { top: 110px; height: 120px; border-radius: 50%; -moz-animation-timing-function: ease-out; } 95% { top: 0; -moz-animation-timing-function: ease-in; } 100% { top: 0; -moz-animation-timing-function: ease-in; } }
Step 4 – 影を表示しよう!
影を表示しましょう. 基本的にはボールを表示する際に指定したスタイルと同じです.
border-radius と transform の scaleY で楕円にしています.
.ball-shadow { position: absolute; left: 50%; bottom: 0; width: 50px; height: 65px; border-radius: 30px / 40px; margin-left: -25px; background: rgba(20, 20, 20, 0.1); box-shadow: 0px 0 20px 35px rgba(20,20,20,.1); -webkit-transform: scaleY(0.3); -moz-transform: scaleY(0.3); transform: scaleY(0.3); }
Step 5 – バウンドに合わせて影を縮ませよう!
ball のバウンドと同じく animation プロパティを使って影の伸縮を表現しています.
書いているコードは長めですが, やっていることはとてもシンプルです.
.ball-shadow { ... -webkit-animation: shrink 1s infinite; -moz-animation: shrink 1s infinite; animation: shrink 1s infinite; }
@-webkit-keyframes shrink { 0% { bottom: 0; margin-left: -30px; width: 60px; height: 75px; background: rgba(20, 20, 20, .1); box-shadow: 0px 0 20px 35px rgba(20,20,20,.1); border-radius: 30px / 40px; -webkit-animation-timing-function: ease-in; } 50% { bottom: 30px; margin-left: -10px; width: 20px; height: 5px; background: rgba(20, 20, 20, .3); box-shadow: 0px 0 20px 35px rgba(20,20,20,.3); border-radius: 20px / 20px; -webkit-animation-timing-function: ease-out; } 100% { bottom: 0; margin-left: -30px; width: 60px; height: 75px; background: rgba(20, 20, 20, .1); box-shadow: 0px 0 20px 35px rgba(20,20,20,.1); border-radius: 30px / 40px; -webkit-animation-timing-function: ease-in; } } @-moz-keyframes shrink { 0% { bottom: 0; margin-left: -30px; width: 60px; height: 75px; background: rgba(20, 20, 20, .1); box-shadow: 0px 0 20px 35px rgba(20,20,20,.1); border-radius: 30px / 40px; -moz-animation-timing-function: ease-in; } 50% { bottom: 30px; margin-left: -10px; width: 20px; height: 5px; background: rgba(20, 20, 20, .3); box-shadow: 0px 0 20px 35px rgba(20,20,20,.3); border-radius: 20px / 20px; -moz-animation-timing-function: ease-out; } 100% { bottom: 0; margin-left: -30px; width: 60px; height: 75px; background: rgba(20, 20, 20, .1); box-shadow: 0px 0 20px 35px rgba(20,20,20,.1); border-radius: 30px / 40px; -moz-animation-timing-function: ease-in; } }
Step 6 – テカリを表示しよう!
最後にテカリを入れます.
これは ball クラスに疑似要素 after を指定し, ボールの上の方にテカリを表示しています.
ちょっとした一手間だけでも結構見栄えしますね. これで完成です!!
.ball::after { content: ""; position: absolute; width: 60px; height: 30px; border-radius: 40px / 20px; left: 30px; top: 10px; background: -webkit-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%); background: -moz-linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%); background: linear-gradient(top, rgba(232,232,232,1) 0%,rgba(232,232,232,1) 1%,rgba(255,255,255,0) 100%); }
以上, 『CSS3 でボールをバウンド』でした~. 意外と簡単でしょ?
会社では C/C++, 家では JavaScript 三昧. なのでたまにこうやって息抜きに HTML5 や CSS3 で遊んでたりします.
なにかリクエストやアドバイスなどありましたらブログコメントもしくは Twitter の方にじゃんじゃん下さい♪
CSS3 で 3Dっぽいボールを作ってバウンドさせてみた http://t.co/sSv2Gjy4
CSS3 で 3Dっぽいボールを作ってバウンドさせてみた http://t.co/sSv2Gjy4
CSS3
CSS3 で 3Dっぽいボールを作ってバウンドさせてみた | TM Life CSS3 で 3Dっぽいボールを作ってバウンドさせてみました. 元ネタは『CREATING AN ANIMATED 3D BOUNCING BALL … http://t.co/Nq1RaEQo
ブログ更新.
『CSS3 で 3Dっぽいボールを作ってバウンドさせてみた』
http://t.co/2lHmpeJT
http://t.co/udLdVYs5
#web #css #css3
ブログ更新.
『CSS3 で 3Dっぽいボールを作ってバウンドさせてみた』
http://t.co/2lHmpeJT
http://t.co/udLdVYs5
#web #css #css3
CSS3 で 3Dっぽいボールを作ってバウンドさせてみた | TM Life http://t.co/kcQF7FTR
“CSS3 で 3Dっぽいボールを作ってバウンドさせてみた | TM Life” http://t.co/wqJ82tFi
CSS3 で 3Dっぽいボールを作ってバウンドさせてみた http://t.co/N5wB7iYa @phi_jpさんから
CSS3 で 3Dっぽいボールを作ってバウンドさせてみた http://t.co/N5wB7iYa @phi_jpさんから
CSS3 で 3Dっぽいボールを作ってバウンドさせてみた http://t.co/N5wB7iYa @phi_jpさんから