owned mediaウェブ制作に役立つコンテンツを発信中!

jQueryでスクロールに連動するアニメーションをプラグインなしで実装する

スクロールに連動した動きにパララックスがありますが、最近は要素に動きのアニメーションをつけたりと、より複雑でバリエーションが増えてきているようになりました。今回はjQueryを使って、要素をスクロールに合わせて動かすアニメーションを実装していきたいと思います。   このような機能のプラグインはたくさんありますが、ここではプラグインを使わない方法での実装になります。シンプルな動きであればプラグインなしでも充分対応できます。   今回の要件は、スクロールを進めていき画面内に要素が表示されるタイミングでアニメーションが始まるという仕様になります。では早速コードを見ていきます。 【HTML】※一部省略
<body>
  <section>
    <div class="scroll-animation-obj scroll-animation-hop">下から浮かび上がるアニメーション
    </div>
  </section>
  <section>
    <div class="scroll-animation-obj scroll-animation-left">左からスライドインで表示されるアニメーション
    </div>
  </section>
  <section>
    <div class="scroll-animation-obj scroll-animation-right">右からスライドインで表示されるアニメーション
    </div>
  </section>
</body>
  【CSS】
.scroll-animation-obj {
  opacity: 0;
  transition: all 0.5s ease 0.5s;
}
.scroll-animation-hop {
  transform: translate(0,60px);
}
.scroll-animation-left {
  transform: translate(-120px,0);
}
.scroll-animation-right {
  transform: translate(120px,0);
}
  HTMLの要素にそれぞれclassを当てておきます。対象となる要素には全て、opacityをゼロにして、transitionが設定されたclassをつけておきます。また、アニメーションの初期化として、それぞれCSSで設定しておきます。この値を調整することでいろんな動きが表現できます。続いてjQueryで動きをつけていきます。   【javascript】
const obj = $(".scroll-animation-obj");
const hopIn = $(".scroll-animation-hop");
const leftIn = $(".scroll-animation-left");
const rightIn = $(".scroll-animation-right");
$(window).on('scroll',function(){
  obj.each(function(){
    const objPos = $(this).offset().top;
    const scroll = $(window).scrollTop();
    const windowH = $(window).height();
    if(scroll > objPos - windowH){
      $(this).css({
        'opacity': '1'
      });
    } else {
      $(this).css({
        'opacity': '0'
      });
    }
  });
  hopIn.each(function(){
    const objPos = $(this).offset().top;
    const scroll = $(window).scrollTop();
    const windowH = $(window).height();
    if(scroll > objPos - windowH){
      $(this).css({
        'transform': 'translate(0,0)'
      });
    } else {
      $(this).css({
        'transform': 'translate(0,60px)'
      });
    }
  });
  leftIn.each(function(){
    const objPos = $(this).offset().top;
    const scroll = $(window).scrollTop();
    const windowH = $(window).height();
    if(scroll > objPos - windowH){
      $(this).css({
        'transform': 'translate(0,0)'
      });
    } else {
      $(this).css({
        'transform': 'translate(-120px,0)'
      });
    }
  });
  rightIn.each(function(){
    const objPos = $(this).offset().top;
    const scroll = $(window).scrollTop();
    const windowH = $(window).height();
    if(scroll > objPos - windowH){
      $(this).css({
        'transform': 'translate(0,0)'
      });
    } else {
      $(this).css({
        'transform': 'translate(120px,0)'
      });
    }
  });
});
  javascript側ではスクロールのイベントでアニメーションの関数を作成していきます。基本的にはどのアニメーションも同じ形になります。現在のページップからのスクロール量と対象となる要素の座標を取得して、要素がウィンドウ内まで表示されるようになった時にCSSで動かしていくという形になります。   いかがでしょうか、シンプルですがこれだけで意外と本格的なスクロールアニメーションが実装できます。今回のサンプルはこちらにあげていますので参考にどうぞ。  

See the Pen simple animation with scroll by designsupply (@designsupply) on CodePen.

 
  より複雑な動きやカスタマイズ性を求めるならばプラグインを導入するのも選択肢に入ってきますが、限定的な箇所で実装する場面であれば、じゅうぶん役に立つのではないでしょうか。
  • はてなブックマーク
  • Pocket
  • Linkedin
  • Feedly

この記事を書いた人

Twitter

sponserd

    keyword search

    recent posts

    • Twitter
    • Github
    contact usscroll to top
      • Facebook
      • Twitter
      • Github
      • Instagram