Giter VIP home page Giter VIP logo

stroll.js's Introduction

stroll.js

A tiny library without dependencies for smooth scrolling in modern browsers. Think of Mr. Toast!

Installation

NPM

npm install stroll.js --save

Script

Just include the dist/scroll.min.js but refer to the polyfill section below. As the dist folder is not part of the repository you still need to install the package via npm.

Usage

import stroll from "stroll.js";

stroll(".target");

stroll returns native Promises. It resolves to an object once scrolling is complete. The object contains a single property named wasCancelled that indicates if scrolling was completed (in case stroll was called again before it finished it’s false).

stroll’s first argument can be a number (absolute position in viewport on primary scroll axis), a selector string, an object with an x and/or y key or something that has a getBoundingClientRect function. You can also pass null, if you want to scroll from the current position.

Whenever the term primary scroll axis is used it refers to the axis of an element that has the largest scroll width. On window this most likely always is the y axis. For elements it depends. A carousel will probably use the x axis.

stroll’s second argument can be an object, with one or more of the following properties:

duration (number|function, 500)

Time in milliseconds or a function that returns a duration for a given distance on x and y axis.

offset (number|object, 0)

Is added to the final scroll position. Accepts an object with x and y key, or a number that is used for the primary scroll axis.

focus (bool, true)

Should the element you scroll to be focused once scrolling is done? this keeps keyboard navigation intact.

easing (function, easeInOutQuad from Robert Penner)

Timing function that is used to animate the scroll position.

ignoreUserScroll (bool, false)

Whether or not programmatic scrolling should be aborted when a user has scrolled. This works by checking if the last set scroll position equals the current position.

allowInvalidPositions (bool, false)

Whether or not positions outside of the document are allowed during strolling or should trigger an early exit.

Examples

Relative Scrolling

Scroll 100px from the current position.

stroll(null, { offset: 100 })
// or
stroll.relative(100)

Changing defaults

Change default duration to 2 seconds.

stroll.DEFAULTS.duration = 2 * 1000

ALL the settings

Scroll to target link anchor - 70px with easeInOutElastic easing for a duration of 10 times the distances to the element, don’t focus and allow invalid positions during animation.

stroll(target.getAttribute("href"), {
    offset: -70,
    duration: (distance) => 10 * (distance.x + distance.y),
    focus: false,
    allowInvalidPositions: true,
    easing: (t, b, c, d) => {
        var s=1.70158;var p=0;var a=c;
        if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
        if (a < Math.abs(c)) { a=c; var s=p/4; }
        else var s = p/(2*Math.PI) * Math.asin (c/a);
        if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
        return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
    }
});

Promise API

Stroll to .target, wait 2 seconds, scroll +500px from current position, wait 1 second, scroll to beginning of the document.

function wait(time) {
    return new Promise((resolve, reject) => {
        setTimeout(resolve, time);    
    });
}

stroll(".target")
    .then(() => wait(2 * 1000))
    .then(() => stroll.relative(500))
    .then(() => wait(1 * 1000))
    .then(() => stroll(0));

Scroll elements instead of window

stroll.js is able to scroll the window as well as elements. If you want to stroll an element you first must create a stroll instance for this element.

const element = document.querySelector(".some-element");
const elementStroller = stroll.factory(element);
elementStroller.relative(500);

The elementStroller instance will have the same API as the default stroll instance (it misses the factory function though).

Browser Compatibility

stroll.js is build for modern Browsers. It uses the Object.assign, requestAnimationFrame and Promise APIs. You may want to polyfill these. requestAnimationFrame is widespread nowadays, but Object.assign and Promises are quite new additions and part of ES-2015. Polyfills that should work for you:

Without polyfills you should be fine in Edge (12+), Safari 9 and and all current versions of Firefox and Chromium/Chrome.

Shout-out

... goes to jump.js for inspiring me to create my own implementation. They’ve worked on v1.0.0, when I created this, and I wasn’t happy with 0.4.0 :).

stroll.js's People

Contributors

kmohrf avatar

Stargazers

Scott C. Krause avatar Karl Fleischmann avatar

Watchers

 avatar James Cloos avatar Karl Fleischmann avatar

stroll.js's Issues

Uncaught (in promise)

Hey @kmohrf . I like stroll.js a lot so far. 😄

I have the following issue, which is a little constructed and (maybe) mainly due to my lacking js-skills:
When having two consecutive calls to stroll, reject() is undefined.

Minimal working example:

<!DOCTYPE html>
<html>
  <body>
    <section>
      <h1>Stroll test</h1>

      <div style="height: 2000px"
           id="first">
        <p>Text.</p>
      </div>

      <div style="height: 2000px"
           id="second">
        <p>Text.</p>
      </div>
    </section>

    <script type="text/javascript" src="dist/stroll.js"></script>
    <script>
stroll('#second');
stroll('#first');

// alternatively a more sensible example
setTimeout(() => stroll('body', {duration: 5 * 1000}), 5 * 1000);
stroll('#second', {duration: 6 * 1000});
    </script>
  </body>
</html>

I know, that this is not the intended way of strolling to and fro. The strolling is cancelled, but I don't know what to make out of the error.

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.