Light weight library to observe the viewport scroll position
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist Update version on banner Sep 23, 2017
src Fix NaN when scroll trajectory is zero Sep 23, 2017
test Improve global objects mock up on test Aug 23, 2017
.babelrc Move to observer approach. Add horizontal scroll tracking Aug 14, 2017
.eslintrc.json Move to observer approach. Add horizontal scroll tracking Aug 14, 2017
.gitignore npm and bower files added May 11, 2015
.npmignore
.travis.yml Move to observer approach. Add horizontal scroll tracking Aug 14, 2017
LICENSE Initial commit May 11, 2014
README.md Fix typo May 26, 2018
bower.json v3.0.1 Aug 14, 2017
index.html Fix NaN when scroll trajectory is zero Sep 23, 2017
package.json 3.0.2 Sep 23, 2017
rollup.config.js Move to observer approach. Add horizontal scroll tracking Aug 14, 2017

README.md

scrollProgress Build Status

Light weight library to observe the viewport scroll position.

In previous versions this package injected a scroll bar showing the scrolling progress. Why did I change it? Because it wasn't flexible to be adapted to others uses or UI libraries.

This means you still can create a progress bar as before and even with less code, keep scrolling to the Recipes section to see how.

Add it to your project

Include the dist file in a script tag or run npm install scrollprogress --save.

Use

In this last version, you have to create a new instance to create an observer and pass a callback to the contructor. That observer can be destroy at any time.

import ScrollProgress from 'scrollprogress';

const progressObserver = new ScrollProgress((x, y) => {
  console.log(x, y);
});

The callback will get two arguments, the first one being a decimal number for the horizontal scrolling progress and the second one for the vertical scrolling progress.

The method you pass will also get called on resize since the viewport and body metrics might change.

destroy

Whenever you want the observer to stop working just call progressObserver.destroy().

Recipes

Vanilla scroll progress bar

To accomplish the old functionality you will need to add the DOM element and the styles, something that the old version did for you, and then create an observer to update the bar width.

HTML

<div class="progress-bar"></div>

CSS

.progress-bar {
  background-color: rebeccapurple;
  height: 5px;
  position: fixed;
  top: 0;
  bottom: 0;
}

JS

const progressElement = document.querySelector('.progress-bar');

const progressObserver = new ScrollProgress((x, y) => {
  progressElement.style.width = y * 100 + '%';
});

And that's it! Super simple.

Remember the only thing that the script will control will be the width of the progress bar as you scroll, the rest of the styling is all on you.

As a React component

One of the main reasons this library was moved to this new approach is because you can easily couple an observer with any component library used nowadays. For example, create a React scroll bar component.

import { Component } from 'react';
import ScrollProgress from 'scrollprogress';

export default class ScrollProgress extends Component {
  constructor() {
    this.state = {
      progress: 0
    };
  }

  componentDidMount() {
    this.progressObserver = new ScrollProgress((x, y) => {
      this.setState({ progress: y * 100 });
    });
  }

  componentWillUnmount() {
    this.progressObserver.destroy();
  }

  render() {
    const style = {
      backgroundColor: 'rebeccapurple',
      height: '5px',
      position: 'fixed',
      top: 0,
      bottom: 0,
      width: `${this.state.progress}%`
    };

    return (
      <div
        className="progress-bar"
        style={ style }
      />
    );
  }
}

It's easy to imagine how to create the same component for other frameworks. If you want to add a recipe or any other use case to the documentation clone this repo and make a pull request.

License

The MIT License (MIT)

Copyright (c) 2017 Jeremias Menichelli

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.