Code Monkey home page Code Monkey logo

scrollprogress's Introduction

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.

scrollprogress's People

Contributors

gazpachu avatar jeremenichelli avatar jumpalottahigh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

scrollprogress's Issues

Multiple progress bar on the same page?

Would it be possible to use 2 or more progress bars in the same page.

For example on a page with 3 articles, each one should have its progress bar. So if the first article is finish, the progress start again with the second . Also if you scroll up, then the progress bar adapt accordingly?

scrollProgress gives negative values (up to -7)

I want to use scrollProgress for a simple Indicator. But if I load the page it starts at 0 and goes into negative when I'm scrolling down. If I resize the window the script seems to recalculate the page height and the value is corect ( 0 - 1 ) now.
Could you give me a hint how I could trigger this after pageload?

TypeError

Hi,

Thanks for making this script ๐Ÿ‘
Unfortunately I can't get it to run because of the following error:

Uncaught TypeError: Cannot read property 'appendChild' of null
at _createElements (scrollprogress.js:48)
at Object._set [as set] (scrollprogress.js:107)
at main.js:1

Would really appreciate it if you could look into it or let me know if I'm doing something wrong :) Installed via npm if that helps..

Cheers

React Component dynamic update

In React, when we have dynamic component with fetch API, the height (or width) or not the same as height in componentDidMount()

To correct the problem, you can recalculate the viewportheight and viewportwidth in getProgress.

Regards

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.