WEBデザインの勉強

初心者が1からWEBデザインを勉強するブログ。

CSS3アニメーションで桜が舞う

f:id:mmmmofu:20160810110832p:plain

デモはこちら
http://efu.sakura.ne.jp/web/sakura/

<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>桜が舞う</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="container">
    <div class="inner">
        <div class="sakura1"></div>
        <div class="sakura2"></div>
        <div class="sakura3"></div>
        <div class="sakura4"></div>
        <div class="sakura5"></div>
        <div class="sakura6"></div>
        <div class="sakura7"></div>
        <div class="sakura8"></div>
        <div class="sakura9"></div>
        <div class="sakura10"></div>
        <div class="sakura11"></div>
        <div class="sakura12"></div>
        <div class="sakura13"></div>
        <div class="sakura14"></div>
        <div class="sakura15"></div>
        <div class="sakura16"></div>
        <div class="sakura17"></div>
        <div class="sakura18"></div>
        <div class="sakura19"></div>
        <div class="sakura20"></div>
    </div>
</div>
</body>
</html>

CSS

@charset "utf-8";
html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}
#container {
  width: 100%;
  height: 100%;
  background: #000;
}
.inner {
  position: relative;
  width: 300px;
  margin: 0 auto;
}
.inner div {
    margin-top: 50%;
    opacity: 0;
    -webkit-animation-name: sakura;
    animation-name: sakura;
    -webkit-animation-iteration-count: infinite;
    animation-iteration-count: infinite;
    -webkit-animation-timing-function: linear;
    animation-timing-function: linear;
    position: absolute;
    display: block;
    width: 10px;
    height: 10px;
    background: url("img/sakura01.png") no-repeat;
    background-size: 10px 10px;
}
.sakura2, .sakura4, .sakura6, .sakura8, .sakura11, .sakura14, .sakura17 {
    width: 13px !important;
    height: 13px !important;
    background: url("img/sakura02.png") no-repeat !important;
    background-size: 13px !important;
}
.sakura1 {
    left: 9px;
    -webkit-animation-duration: 2.5s;
    animation-duration: 2.5s;
}
.sakura2 {
    left: 34px;
    -webkit-animation-duration: 2.7s;
    animation-duration: 2.7s;
    -webkit-animation-delay: 0.2s;
    animation-delay: 0.2s;
}
.sakura3 {
    left: 74px;
    -webkit-animation-duration: 2.9s;
    animation-duration: 2.9s;
    -webkit-animation-delay: 0.4s;
    animation-delay: 0.4s;
}
.sakura4 {
    left: 113px;
    -webkit-animation-duration: 2.4s;
    animation-duration: 2.4s;
    -webkit-animation-delay: 0s;
    animation-delay: 0s;
}
.sakura5 {
    left: 158px;
    -webkit-animation-duration: 2.4s;
    animation-duration: 2.4s;
    -webkit-animation-delay: 1s;
    animation-delay: 1s;
}
.sakura6 {
    left: 180px;
    -webkit-animation-duration: 2.2s;
    animation-duration: 2.2s;
    -webkit-animation-delay: 1.2s;
    animation-delay: 1.2s;
}
.sakura7 {
    left: 210px;
    -webkit-animation-duration: 2.7s;
    animation-duration: 2.7s;
    -webkit-animation-delay: 1.2s;
    animation-delay: 1.2s;
}
.sakura8 {
    left: 230px;
    -webkit-animation-duration: 3s;
    animation-duration: 3s;
    -webkit-animation-delay: 1.4s;
    animation-delay: 1.4s;
}
.sakura9 {
    left: 258px;
    animation-delay: 5s;
    animation-duration: 4s;
}
.sakura10 {
    left: 270px;
    animation-delay: 2s;
    animation-duration: 3s;
}
.sakura11 {
    left: 3px;
    animation-delay: 8s;
    animation-duration: 4s;
}
.sakura12 {
    left: 45px;
    animation-delay: 10s;
    animation-duration: 2s;
}
.sakura13 {
    left: 78px;
    animation-delay: 4s;
    animation-duration: 3s;
}
.sakura14 {
    left: 119px;
    animation-delay: 8s;
    animation-duration: 6s;
}
.sakura15 {
    left: 143px;
    animation-delay: 14s;
    animation-duration: 4s;
}
.sakura16 {
    left: 189px;
    animation-delay: 25s;
    animation-duration: 4s;
}
.sakura17 {
    left: 202px;
    animation-delay: 15s;
    animation-duration: 2s;
}
.sakura18 {
    left: 221px;
    animation-delay: 6s;
    animation-duration: 4s;
}
.sakura19 {
    left: 245px;
    animation-delay: 2s;
    animation-duration: 2s;
}
.sakura20 {
    left: 276px;
}
/*アニメーションの設定*/
@keyframes sakura {
  0% {
    -webkit-transform: translate(0, 0);
    transform: translate(0, 0);
    opacity: 0;
  }
  50% {
    -webkit-transform: translate(0px, 140px);
    transform: translate(0px, 140px);
    opacity: 1;
  }
  100% {
    -webkit-transform: translate(0, 280px);
    opacity: 0.2;
  }
}

animation

アニメーションについてまとめて指定する

animation-name

キーフレームアニメーションの際に使用する名前を指定

animation-iteration-count

アニメーションの繰り返し回数を指定
「infinite」で無限に繰り返し

animation-timing-function

アニメーションのタイミング・進行割合を指定する

ease(初期値)/linear/ease-in/ease-out/ease-in-out/cubic-bezier()


参考
yachin29.hatenablog.com

liginc.co.jp