Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <head>
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
  <!--==============レイアウトを制御する独自のCSSを読み込み===============-->
  <link href="https://assets.codepen.io/6329135/reset04.css" rel="stylesheet">
</head>

<body>
  <div class="scrolldown"><span>Scroll</span></div>
  
  <div class="wrapper">
    <p>JavaScriptと組み合わせて動かすきっかけを設定する方法</p>
    <p>スクロールすると画像がアニメーションします</p>

    <div class="space"></div>

    <div class="stepsTrigger"></div>
  </div>
  <!--/wrapper-->

  <script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>

</body>
              
            
!

CSS

              
                @charset "utf-8";

/*========= スクロールダウンのためのCSS ===============*/

/*スクロールダウン全体の場所*/
.scrolldown {
  /*描画位置※位置は適宜調整してください*/
  position: absolute;
  bottom: 10px;
  right: 50%;
  /*マウスの動き1.6秒かけて動く永遠にループ*/
  animation: mousemove 1.6s ease-in-out infinite;
}

/*下からの距離が変化して上から下に動く*/
@keyframes mousemove {
  0% {
    bottom: 10px;
  }
  50% {
    bottom: 5px;
  }
  100% {
    bottom: 10px;
  }
}

/*Scrollテキストの描写*/
.scrolldown span {
  z-index: 10;
  /*描画位置*/
  position: absolute;
  left: -15px;
  bottom: 45px;
  /*テキストの形状*/
  color: #333;
  font-size: 0.7rem;
  letter-spacing: 0.05em;
  white-space: nowrap;
}

/*マウスの中の線描写 */
.scrolldown span::after {
  z-index: 10;
  content: "";
  /*描画位置*/
  position: absolute;
  top: 10px;
  left: 17px;
  /*線の形状*/
  width: 1px;
  height: 15px;
  background: #333;
  /*線の動き1.4秒かけて動く。永遠にループ*/
  animation: mousepathmove 1.4s linear infinite;
  opacity: 0;
}

/*上からの距離・不透明度・高さが変化して上から下に流れる*/
@keyframes mousepathmove {
  0% {
    height: 0;
    top: 10px;
    opacity: 0;
  }
  50% {
    height: 15px;
    opacity: 1;
  }
  100% {
    height: 0;
    top: 30px;
    opacity: 0;
  }
}

/*マウスの描写 */
.scrolldown:before {
  z-index: 10;
  content: "";
  /*描画位置*/
  position: absolute;
  bottom: 0;
  left: -10px;
  /*マウスの形状*/
  width: 25px;
  height: 37px;
  border-radius: 10px;
  border: 1px solid #333;
}

/*マウスの中の丸の描写*/
.scrolldown:after {
  z-index: 10;
  content: "";
  /*描画位置*/
  position: absolute;
  bottom: 26px;
  left: 0;
  /*丸の形状*/
  width: 5px;
  height: 5px;
  border-radius: 50%;
  border: 1px solid #333;
}

/*========= スクロールのためのCSS ===============*/
.space {
  padding: 100vh 0;
}

/*========= レイアウトのためのCSS ===============*/

.wrapper {
  text-align: center;
  padding: 0 0 100px 0;
}

p {
  text-align: center;
  padding: 20px 0;
  font-size: clamp(1rem, 2vw, 1.5rem);
  color: #333;
  white-space: nowrap;
  font-weight: bold;
}

.steps {
  margin: 0 auto;
}

/*=============== スクロールするとpng画像が動く =================*/

.stepsTrigger {
  opacity: 0;
}

.steps {
  width: 100px; /*1フレーム分の横幅*/
  height: 100px; /*1フレーム分の縦幅*/
  background: url("https://assets.codepen.io/6329135/steps.png") no-repeat; /*背景画像の読み込み*/
  animation: stepsline 1s steps(3) forwards;
  /*stepsline→アニメーション名
    1s→アニメーションをする時間
    step→()の中には、アニメーション制作コマ数-1の値を入れる(例:4コマ-1コマ=3コマ)
    forwards→最後の形を維持*/
}

.steps:hover {
  animation: stepsline 1s steps(3) forwards;
  /*stepsline→アニメーション名
    1s→アニメーションをする時間
    step→()の中には、アニメーション制作コマ数-1の値を入れる(例:4コマ-1コマ=3コマ)
    forwards→最後の形を維持*/
}

@keyframes stepsline {
  0% {
    opacity: 0;
    background-position: 0 0;
  }
  100% {
    opacity: 1;
    background-position: -300px 0; /*アニメーション制作コマ数から1コマ分引いた横幅を指定。
(例:今回は1フレーム横幅100pxで制作しているので全体の400px-100px=300px⇒-300px)を指定。*/
  }
}

              
            
!

JS

              
                // 動きのきっかけの起点となるアニメーションの名前を定義
function stepsAnime() {
  $(".stepsTrigger").each(function () {
    //stepsTriggerというクラス名が
    var elemPos = $(this).offset().top - 50; //要素より、50px上の
    var scroll = $(window).scrollTop();
    var windowHeight = $(window).height();
    if (scroll >= elemPos - windowHeight) {
      $(this).addClass("steps"); // 画面内に入ったらstepsというクラス名を追記
    } else {
      $(this).removeClass("steps"); // 画面外に出たらstepsというクラス名を外す
    }
  });
}

// 画面をスクロールをしたら動かしたい場合の記述
$(window).scroll(function () {
  stepsAnime(); /* アニメーション用の関数を呼ぶ*/
}); // ここまで画面をスクロールをしたら動かしたい場合の記述

// 画面が読み込まれたらすぐに動かしたい場合の記述
$(window).on("load", function () {
  stepsAnime(); /* アニメーション用の関数を呼ぶ*/
}); // ここまで画面が読み込まれたらすぐに動かしたい場合の記述

              
            
!
999px

Console