Giter Club home page Giter Club logo

moveto's Introduction

MoveTo npm version Bower version Build Status

A lightweight (only 1kb gzipped), smooth scrolling 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.

  <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'
})
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
callback noop The function to be run after scrolling complete. Target passes as the first argument

API

move(target, options)

Scrolls to target

target

Type: HTMLElement|Number

Target element/position to be scrolled. Target position is the distance to the top of the page

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 http://gizma.com/easing/ 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
    }
  });
});

Development setup

# To install dev dependencies run:

yarn

# or

npm install

# To start the development server run:

gulp serve

# To lint your code run:

gulp scripts:lint

# To make a full new build run:

gulp build

# To run tests

yarn test

# or

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).

moveto's People

Contributors

hsnaydd avatar yildirayunlu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.