🌜
🌞
moveto

moveto

v1.8.2

A lightweight scroll animation javascript library without any dependency.

npm install moveto

README

MoveTo npm version Bower version CDNJS version Build Status

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

Demo

Installation

Using npm

$ npm install moveto --save

Using Yarn

$ yarn add moveto

Using Bower

$ bower install moveTo --save

Usage

const moveTo = new MoveTo();

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

moveTo.move(target);

// Or register a trigger

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

moveTo.registerTrigger(trigger);

Trigger HTML markup

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

<a href="#target" class="js-trigger" data-mt-duration="300">Trigger</a>

<!-- Or -->

<button type="button" class="js-trigger" data-target="#target" data-mt-duration="300">Trigger</button>

Options

The default options are as follows:

new MoveTo({
  tolerance: 0,
  duration: 800,
  easing: 'easeOutQuart',
  container: window
})
Option Default Desctiption
tolerance 0 The tolerance of the target to be scrolled, can be negative or positive
duration 800 Duration of scrolling, in milliseconds
easing easeOutQuart Ease function name
container window The container been computed and scrolled
callback noop The function to be run after scrolling complete. Target passes as the first argument

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 run after the scroll complete. 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 function.

Examples

Pass ease function(s) when creating instance
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
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
    }
  });
});
Unregister a trigger
document.addEventListener('DOMContentLoaded', function () {
  const moveTo = new MoveTo();

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

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

  // Unregister a trigger
  unregister();
});
Back to top
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]);
  }
});
<a href="#" class="js-back-to-top" data-mt-duration="300">Back to top!</a>

Development setup

# To install dev dependencies run:

$ yarn

# Or so if using npm:

$ npm install

# To start the development server run:

$ yarn start

# Or so if using npm:

$ npm run start

# To lint your code run:

$ yarn lint

# Or so if using npm:

$ npm run lint

# To make a full new build run:

$ yarn build

# Or so if using npm:

$ npm run build

# To run tests:

$ yarn test

# Or so if using npm:

$ npm test

Browser support

It should work in the current stable releases of Chrome, Firefox, Safari as well as IE10 and up. To add support for older browsers, consider including polyfills/shims for the requestAnimationFrame.

License

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

Release Notes

1.8.2
By Hasan Aydoğdu • Published on June 28, 2019
  • Npm packages upgraded
1.8.1
By Hasan Aydoğdu • Published on May 7, 2019
  • Upgraded dependencies
  • Added npm scripts
  • Fixed the problem that linting the files not working correctly
  • Documentation updates
1.8.0
By Hasan Aydoğdu • Published on January 14, 2019
  • Added ability to scroll inside of custom element, instead of only window (#23)

Thanks to @pea3nut

1.7.4
By Hasan Aydoğdu • Published on September 28, 2018
  • Updated npm packages
1.7.3
By Hasan Aydoğdu • Published on May 3, 2018
  • Updated npm packages
1.7.1
By Hasan Aydoğdu • Published on October 21, 2017
  • [#15] Bug fix
  • Added Back to top example in Readme
1.7.0
By Hasan Aydoğdu • Published on October 1, 2017

Code improve [36fb964]

Thank you @danbovey for pull request

1.6.1
By Hasan Aydoğdu • Published on April 12, 2017

Fixed problem with request animation frame loop (#8)

1.6.0
By Hasan Aydoğdu • Published on March 27, 2017
  • Added unregister trigger support (9f136e4)
1.5.3
By Hasan Aydoğdu • Published on March 24, 2017
  • Fixed npm import problem #6
  • A few minor code improvements done (086362b, 14f2c1f, 57c06b5)

Thanks to @clintonyeb for the pull request

General

License
MIT
Typescript Types
Tree-shakeable
No

Popularity

GitHub Stargazers
2,784
Community Interest
2,889
Number of Forks
100

Maintenance

Commits
10/219/22015
Last Commit
Open Issues
3
Closed Issues
15
Open Pull Requests
0
Closed Pull Requests
4

Versions

Versions Released
10/219/2201
Latest Version Released
Jun 28, 2019
Current Tags
latest1.8.2

Contributors

hsnaydd
hsnaydd
Commits: 125
lex111
lex111
Commits: 2
danbovey
danbovey
Commits: 1
pataar
pataar
Commits: 1
yildirayunlu
yildirayunlu
Commits: 1
extend1994
extend1994
Commits: 1
374632897
374632897
Commits: 1
pea3nut
pea3nut
Commits: 1
johnbacon
johnbacon
Commits: 1