LoginSignup
14
7

More than 5 years have passed since last update.

CSS animation で遊び倒す - 太陽 -

Last updated at Posted at 2019-01-31

CSS animation day11 となりました。

だいぶ、自然系ネタが尽きてきました。本日は、おしゃれな太陽を表現します。

1. 完成版

ダウンロード (23).gif

See the Pen shining sun by hiroya iizuka (@hiroyaiizuka) on CodePen.

2. 参考文献

Drop shadow be a Gradient - CSS Animated Gradient Shadow Effects - Quick HTML, CSS Tips & Tricks - YouTube

3. 分解してみる

❶.

では背景から作りましょう。

index.html
<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <link rel="stylesheet" href="css/styles.css" />
  </head>
  <body>
    <div class="shadow"></div>
  </body>
</html>
styles.css
body {
  margin: 0;
  padding: 0;
  background: #000;
}

.shadow {
  position: relative;
  margin: 200px auto 0;
  width: 250px;
  height: 250px;
  border-radius: 50%;
  background: linear-gradient(#000, #262626);
}

スクリーンショット 2019-01-31 9.07.49.png

❷.
では、次に虹色を表現します。

styles.css
.shadow:before {
  content: "";
  position: absolute;
  top: 0px;
  left: 0px;
  border-radius: 50%;
  background: linear-gradient(orange, yellow, green, cyan, blue, violet);
  width: 100%;
  height: 100%;
  background-size: 100%;

スクリーンショット 2019-01-31 9.24.30.png

この虹色を影にするために背面に持ってくるためには、z-index: -1 とすれば良いですが、完全に裏になると、虹色がグラディエントが前面の黒に重なり、見えなくなります。
top, left をいじってみましょう。

styles.css
.shadow:before {
  content: "";
  position: absolute;
  top: -2px;
  left: -2px;
  border-radius: 50%;
  background: linear-gradient(orange, yellow, green, cyan, blue, violet);
  width: 100%;
  height: 100%;
  background-size: 100%;
  z-index: -1;
}

スクリーンショット 2019-01-31 9.27.52.png

いい感じになりました。
全周に影をつけるために、width, height を変更しましょう。

styles.css
.shadow:before {
  content: "";
  position: absolute;
  top: -2px;
  left: -2px;
  border-radius: 50%;
  background: linear-gradient(orange, yellow, green, cyan, blue, violet);
  width: calc(100% + 4px);
  height: calc(100% + 4px);
  background-size: 100%;
  z-index: -1;
}

スクリーンショット 2019-01-31 9.30.04.png

ポイントは、一つ
calc()
これは、レスポンシブデザイン対応のため、異なる単位(%, px)で計算したいときに、役立つ便利なメソッドです。
詳細は こちら の記事をご参照ください。


❸.
アニメーションを設定する。

アニメーションで、X座標を動かして、輪郭に虹色の変化をだしたいとします。
ただ今のままでは、グラデーションは横に同じ色が重なっているため、X軸への平行移動では、色の変化は出ません。そこで、虹色のグラデーションの先頭に任意の角度 (ここでは45度)をつけて、色の変化を明瞭に出しましょう。

styles.css
.shadow:before {
  content: "";
  position: absolute;
  top: -2px;
  left: -2px;
  border-radius: 50%;
  background: linear-gradient(45deg, orange, yellow, green, cyan, blue, violet);
  width: calc(100% + 4px);
  height: calc(100% + 4px);
  background-size: 400%;
  animation: shadow 20s linear infinite;
}

@keyframes shadow {
  0% {
    background-position: 0 0;
  }
  50% {
    background-position: 400% 0;
  }
  100% {
    background-position: 0 0;
  }
}

ダウンロード (21).gif

z index: -1 にして

ダウンロード (22).gif

かなり出来上がってきました。
では、最後、さらに表現力をあげます。


❹.
blur をつけよう!

styles.css
body {
  margin: 0;
  padding: 0;
  background: #000;
}

.shadow {
  position: relative;
  margin: 200px auto 0;
  width: 250px;
  height: 250px;
  border-radius: 50%;
  background: linear-gradient(#000, #262626);
}

.shadow:before,
.shadow:after {
  content: "";
  position: absolute;
  top: -2px;
  left: -2px;
  border-radius: 50%;
  background: linear-gradient(45deg, orange, yellow, green, cyan, blue, violet);
  width: calc(100% + 4px);
  height: calc(100% + 4px);
  background-size: 400%;
  z-index: -1;
  animation: shadow 20s linear infinite;
}

.shadow:after {
  filter: blur(20px);
}

@keyframes shadow {
  0% {
    background-position: 0 0;
  }
  50% {
    background-position: 300% 0;
  }
  100% {
    background-position: 0 0;
  }
}

ダウンロード (23).gif

after擬似クラスで、blurをつけました。
filterプロパティ は超優秀で、blur以外にも、明るくしたり、コントラストをつけたりすることができます。

styles.css
.shadow:after {
  filter: blur(20px) drop-shadow(16px 16px 16px red) invert(200%);
}

ダウンロード (24).gif

こんな素敵な表現が、CSSだけでできるなんて、素敵ですね。
ではまた明日〜!

14
7
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
14
7