npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

scroll-event-handler

v1.1.0

Published

Scroll event handler is a NPM package est un package for reading scroll (desktop) or touch (mobile) events and their assigned a callback.

Downloads

24

Readme

scroll event handler

Scroll event handler is a NPM package est un package for reading scroll (desktop) or touch (mobile) events and their assigned a callback.

Badges

npm npm bundle size npm

GitHub last
commit GitHub top language HitCount

Codacy Badge time tracker Demo

NPM

Demo

The online demo gives you an overview of what can be done with this package.

You can also see the use of this package on my portfolio.

Linter

For this npm ESLint is used with the basic parameters to have a clean code.

Installation

It can be installed from npm.

$ npm install scroll-event-handler

Usage

The minimal configuration for scroll listener is this bellow :

import ScrollListener from 'scroll-event-handler';

let newScrollListener = new ScrollListener({callback: () => console.log('scroll')});

With this configuration scroll listener will fire the callback regardless of the direction using the defaults settings. for further use an example are available below.

Options

Container

container description :

CancelOnDirectionChange

cancelOnDirectionChange description :

Callback

callback description :

Scroll

scroll description :

Touch

touch description :

Methods

Several methods can be called after creating the listener. You will find them below.

  • removeScrollListener() : allows to suppress the listener.

  • switchCancelOnDirectionChange() : allows to switch the bolean value of cancelOnDirectionChange.

  • changeSettings(Object) : allows you to modify the parameters given when creating the listener. It takes the same options as when creating the listener.

You can find the way to call them in the examples section.

Examples

Listen scroll on element

let newScrollListener = new ScrollListener({
  container: '#myListenedElement',
});
// newScrollListener listen the scroll and touch event in your #myListenedElement.

Listen vertical scroll/touch

let newScrollListener = new ScrollListener({
  container: '#myListenedElement',
  scroll: {
    y: {
      value: 5,
      callback: () => console.log('Hello scroll world'),
    },
  },
  touch: {
    y: {
      value: 200,
      callback: () => console.log('Hello touch world'),
    },
  },
});
// now you console.log a string if user verticaly scroll/touch.

Listen horizontal scroll with direction

let newScrollListener = new ScrollListener({
  container: '#myListenedElement',
  scroll: {
    x: {
      prev: {
        value: 5,
        callback: () => console.log('prev scroll'),
      },
      next: {
        value: 2,
        callback: () => console.log('next scroll'),
      },
    },
  },
  touch: {
    x: {
      prev: {
        value: 200,
        callback: () => console.log('prev touch'),
      },
      next: {
        value: 150,
        callback: () => console.log('next touch'),
      },
    },
  },
});
// sent you a different console.log depending on whether the user scrolls forward or backward on the horizontal axis

Setup global callback

let newScrollListener = new ScrollListener({
  container: '#myListenedElement',
  callback: () => console.log('my global callback'),
  scroll: {
    x: {
      prev: {
        value: 5,
        callback: () => console.log('prev scroll'),
      },
      next: {
        value: 2,
        callback: () => console.log('next scroll'),
      },
    },
  },
  touch: {
    x: {
      prev: {
        value: 200,
        callback: () => console.log('prev touch'),
      },
      next: {
        value: 150,
        callback: () => console.log('next touch'),
      },
    },
  },
});
// during each scroll listened to, your global callback function will be called before the others.

Remove a listener

let newScrollListener = new ScrollListener({
  container: '#myListenedElement',
});

newScrollListener.removeScrollListener();
//the listener is now removed.

Switch

let newScrollListener = new ScrollListener({
  container: '#myListenedElement',
  cancelOnDirectionChange: true
});

newScrollListener.switchCancelOnDirectionChange();
// now the cancelOnDirectionChange is false.

Change vertical previous scroll/touch settings

let newScrollListener = new ScrollListener({
  container: '#myListenedElement',
  scroll: {
    x: {
      prev: {
        value: 5,
        callback: () => console.log('prev scroll'),
      },
      next: {
        value: 2,
        callback: () => console.log('next scroll'),
      },
    },
  },
  touch: {
    x: {
      prev: {
        value: 200,
        callback: () => console.log('prev touch'),
      },
      next: {
        value: 150,
        callback: () => console.log('next touch'),
      },
    },
  },
});

newScrollListener.changeSettings({
  scroll: {
    x: {
      prev: {
        value: 10,
      },
    },
  },
  touch: {
    x: {
      prev: {
        value: 300,
      },
    },
  },
});
// now scroll.x.prev value is 10 and the touch.x.prev.value is 300.

Compatible Browsers

Browserstack

The tests were carried out thanks to Browserstack which offers us its services since our npm is opensource.

The npm is now run on all devices and browsers but it's possible that problems may be encountered on browsers that may be common. If this is the case, do not hesitate to open an issue.

Bug or problem ?

If you encounter a problem or a bug due to the NPM package do not hesitate to open an issue with the corresponding label and a detailed description of your problem.

Contributors

  • Guyomar Alexis - ga-devfront : lead developer of project.
  • Daniels-Roth Stan - mrstandu33 : consultant and adviser on development.

See also the list of contributors who participated in this project.

Want to contribute to the project ? First read your document on how to contribute effectively.

License

This project is licensed under the MIT License - see the LICENSE file for details.