Scroll-driven Interactions In JavaScript – Scrollmotion

Category: Animation , Javascript | June 10, 2022
Author:Neoflow
Views Total:288 views
Official Page:Go to website
Last Update:June 10, 2022
License:MIT

Preview:

Scroll-driven Interactions In JavaScript – Scrollmotion

Description:

Scrollmotion is a JavaScript library for scroll-driven interactions that use Animate.css library (or any other animation libraries) to animate elements as they scroll into view.

Written in TypeScript. Works perfectly on modern browsers that support both IntersectionObserver and MutationObserver APIs.

How to use it:

1. Install & download the package.

# NPM
$ npm i @neoflow/scrollmotion --save

2. Load the Scrollmotion library from the dist folder.

<script src="dist/scrollmotion.js"></script>

3. Load the latest Animate.css library from a CDN.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" integrity="sha512-c42qTSw/wPZ3/5LBzD+Bw5f7bSF2oxou6wEb+I/lqeaKV5FDIfMvvRp772y4jcJLKuGUOpbJMdg/BTl50fJYAw==" crossorigin="anonymous" />

4. Determine the animation type in the data-sm-animate-class attribute. Refer to the official website for all animation classes.

<div class="example" data-sm-animate-class="animate__bounceInDown">
  Content To Animate On Scroll
</div>

5. Initialize the Scrollmotion library on the element.

const sm = Scrollmotion('.sm-item');

6. Start tracking the scroll events and animating the element when it is scrolled into view.

sm.start();

7. Config the scroll-triggered animation using the following options.

const sm = Scrollmotion('.sm-item',{
      // root element
      root: null,
      // margin property of root element
      rootMargin: '0px 0px 0px 0px',
      // threshold to identify visibility changes
      threshold: [0, 0.25, 0.5, 0.75, 1],
      // how much an observed item has actually been visible, before the animation performs
      // you can also pass this option via data-sm-ratio attribute
      ratio: 0,
      // enable MutationObserver API
      observeMutation: false,
      // callbacks
      initialized: null,
      started: null,
      itemAnimated: null,
      stopped: null
});

8. Use the following functions to create your own animations, rather than using the Animate.css library.

const sm = Scrollmotion('.sm-item',{
      prepareItem: function (item) {
        item.style.visibility = "hidden";
      },
      
      animateItem: function (item) {
        item.style.visibility = "visible";
        item.classList.add('animate__animated');
        if (item.dataset.smAnimateClass) {
            item.classList.add(item.dataset.smAnimateClass);
        }
        else {
            item.classList.add(defaultConfig.animateClass);
        }
      },
});

Changelog:

v0.4.0 (06/10/2022)

  • Updated dependencies

v0.3.5 (03/02/2021)

  • Updated dependencies

v0.3.4 (12/03/2020)

  • Reorganized ESM

v0.3.3 (12/03/2020)

  • Updated deps

v0.3.2 (11/24/2020)

  • Updated deps and types handling

v0.3.1 (11/21/2020)

  • Fixed ESM handling

v0.2.0 (11/05/2020)

  • Added container and ìtems args to the initialized-callback
  • Added first tests
  • Updated dependencies

v0.1.0 (10/31/2020)

  • Updated dependencies

v0.0.2 (10/22/2020)

  • Updated dependencies

You Might Be Interested In:


One thought on “Scroll-driven Interactions In JavaScript – Scrollmotion

Leave a Reply