【CSS】css-doodle を使って雨を降らせてみる

デザオ
雨だー
デザコト
css-doodleで雨を降らしてみますねー

サンプル

コードを見る(HTML・CSS)

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/css-doodle/0.6.1/css-doodle.min.js"></script>

<div class="rain">
  <css-doodle>
    :doodle {
      @grid: 1x3 / 100vmax;
      position: absolute;
      top: 0; left: 0;
      z-index: 0;
    }

    @size: 100% 130%;
    position: absolute;
    background: @m(100, (linear-gradient(transparent, @p(#ffffff@repeat(2, @p([0-9a-f])), #ffffff@repeat(2, @p([0-9a-f]))))
    @r(0%, 100%) @r(0%, 100%) /
    @r(1px) @r(10vmin)
    no-repeat
    ));

    will-change: transform;
    animation: f 20s linear calc(-20s / @size() * @i()) infinite;
    @keyframes f {
      from { transform: translateY(-100%) }
      to { transform: translateY(100%) }
    }
  </css-doodle>

  <div class="rain_img"><img src="https://125naroom.com/wp/wp-content/themes/design125naroom/img/dezao.png" alt="デザオ" width="130" height="130"></div>
</div>

CSS

/* ---------- 01 雨 ---------- */
.rain {
  position: relative;
  height: 100vh;
  background: #2b2a27;
  overflow: hidden;
}
.rain_img {
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  z-index: 1;
}
.rain_img img {
  width: 130px;
  vertical-align: bottom;
}

/* 動きを減らす設定のとき(雨を止める) */
@media (prefers-reduced-motion: reduce) {
  .rain css-doodle { display: none; }
}

css-doodle とは?

css-doodle とは、CSSが使える進化版といった感じ。
まだ使えるブラウザが限られている。
現在はChromeとSafariだけ。
古いブラウザだとPolyfillの読み込みが必要。

対応していないブラウザの場合は、css-doodle を非表示にするとかですかね。

JSだったら

if (!window.customElements || !document.head.attachShadow) {
  document.body.classList.add('no-doodle-support');
}

CSSだったら

.no-doodle-support css-doodle {
  display: none;
}

さいごに

デザオ
css-doodleってすごいですねー
デザコト
休憩しましょうー

ふたりの小言

ほかの見本は、みほんちょうに。

デザコトauthor

あ、いいな、と思うWebデザインを紹介しています。デザインの参考に。やさしいデザインが多いです。Webデザインギャラリー『デザインのこと - Web design gallery』を運営しています。