MoveTo

A lightweight scroll animation javascript library without any dependency

README

MoveTo Version CDNJS version CI Status


A lightweight (only 1kb gzipped) scroll animation javascript library without any dependency.


Installation


Using npm


  1. ```sh
  2. $ npm install moveto --save
  3. ```

Using Yarn


  1. ```sh
  2. $ yarn add moveto
  3. ```

Usage


  1. ```js
  2. const moveTo = new MoveTo();

  3. const target = document.getElementById('target');

  4. moveTo.move(target);

  5. // Or register a trigger

  6. const trigger = document.getElementsByClassName('js-trigger')[0];

  7. moveTo.registerTrigger(trigger);

  8. ```

Trigger HTML markup

You can pass all options as data attributes with the mt prefix. Option name should be written in kebab case format, for example:

  1. ```html
  2. <a href="#target" class="js-trigger" data-mt-duration="300">Trigger</a>
  3. !-- Or -->
  4. <button type="button" class="js-trigger" data-target="#target" data-mt-duration="300">Trigger</button>
  5. ```

Options


The following options are available:

  1. ```js
  2. new MoveTo({
  3.   tolerance: 0,
  4.   duration: 800,
  5.   easing: 'easeOutQuart',
  6.   container: window
  7. })
  8. ```

OptionDefaultDescription
|-----------|--------------|--------------------------------------------------------------------------------------|
tolerance0The
duration800Duration
easingeaseOutQuartEase
containerwindowThe
callbacknoopThe

API


move(target, options)


Start scroll animation from current position to the anchor point.

target

Type: HTMLElement|Number

Target element/position to be scrolled. Target position is the scrolling distance. It must be negative if the upward movement is desired.

options

Type: Object

Pass custom options.

registerTrigger(trigger, callback)


trigger

Type: HTMLElement

This is the trigger element for starting to scroll when on click.

callback


This is the callback function to be ran after the scroll completes. This will overwrite the callback option.

addEaseFunction(name, fn)


Adds custom ease function.

name

Type: String

Ease function name.

fn

Type: Function

Ease function. See Easing Equations for more ease functions.

Examples


Pass ease function(s) when creating an instance

  js
  document.addEventListener('DOMContentLoaded', function () {
    const easeFunctions = {
      easeInQuad: function (t, b, c, d) {
        t /= d;
        return c t t + b;
      },
      easeOutQuad: function (t, b, c, d) {
        t /= d;
        return -c t (t - 2) + b;
      }
    }

    const moveTo = new MoveTo({
      duration: 1000,
      easing: 'easeInQuad'
    }, easeFunctions);

    const trigger = document.getElementsByClassName('js-trigger')[0];

    moveTo.registerTrigger(trigger);
  });
  

Working with callback function

  js
  document.addEventListener('DOMContentLoaded', function () {
    const moveTo = new MoveTo({
      duration: 1000,
      callback: function (target) {
        // This will run if there is no overwrite
      }
    });

    const trigger = document.getElementsByClassName('js-trigger')[0];

    moveTo.registerTrigger(trigger, function (target) {
      // Overwrites global callback
    });

    // Or

    moveTo.move(1200, {
      duration: 500,
      callback: function () {
        // Overwrites global callback
      }
    });
  });
  1. ```
  2. </details>

  3. <details>
  4.   <summary>Unregister a trigger</summary>

  5.   ```js
  6.   document.addEventListener('DOMContentLoaded', function () {
  7.     const moveTo = new MoveTo();

  8.     const trigger = document.getElementsByClassName('js-trigger')[0];

  9.     // Register a trigger
  10.     const unregister = moveTo.registerTrigger(trigger, { duration: 500 });

  11.     // Unregister a trigger
  12.     unregister();
  13.   });
  14. ```

Back to top

  js
  document.addEventListener('DOMContentLoaded', function () {
    const moveTo = new MoveTo();
    const triggers = document.getElementsByClassName('js-back-to-top');

    for (var i = 0; triggers.length < i; i++) {
      moveTo.registerTrigger(triggers[i]);
    }
  });
  

  html
Back to top!
  

Development setup


  1. ```sh
  2. # To install dev dependencies run:

  3. $ yarn

  4. # Or so if using npm:

  5. $ npm install

  6. # To start the development server run:

  7. $ yarn start

  8. # Or so if using npm:

  9. $ npm run start

  10. # To lint your code run:

  11. $ yarn lint

  12. # Or so if using npm:

  13. $ npm run lint

  14. # To make a full new build run:

  15. $ yarn build

  16. # Or so if using npm:

  17. $ npm run build

  18. # To run tests:

  19. $ yarn test

  20. # Or so if using npm:

  21. $ npm test
  22. ```

Browser support


It should work in the current stable releases of Chrome, Firefox, Safari and Edge. To add support for older browsers, consider including polyfills/shims for the requestAnimationFrame and Element.scroll.

License


Copyright (c) 2017 Hasan Aydoğdu. See the LICENSE file for license rights and limitations (MIT).