Socket
Socket
Sign inDemoInstall

scroll-event-handler

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    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.


Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Install size
74.1 kB
Created
Weekly downloads
 

Readme

Source

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 :

TypeDescription
stringDefault value : 'main'
Using a querySelector you will have to indicate the container to listen to with the appropriate string.

CancelOnDirectionChange

cancelOnDirectionChange description :

TypeDescription
booleanDefault value : true
Defined if the scroll counter should be reset if the direction of the scroll changes.

Callback

callback description :

TypeDescription
functionDefault value : () => {}
Allows you to define a global callback which will be called at each trigger before the other callbacks.

Scroll

scroll description :

TypeDescription
objectEither: An object encapsulating the various configuration parameters for all possible scrolls (X / Y - next / prev).
NameTypeDescription
valuenumberIndicates the number of scrolls before the callback is triggered
callbackfunctionDefines the function to be called when the number of scrolls is reached.
An object encapsulating the different scroll axes.
NameTypeDescription
xobjectEither: An object encapsulating the various configuration parameters for all horizontal scrolls.
NameTypeDescription
valuenumberIndicates the number of scrolls before the callback is triggered
callbackfunctionDefines the function to be called when the number of scrolls is reached.
An object encapsulating the different directions of the scroll.
NameTypeDescription
nextobjectAn object contain the various configuration parameters for horizontal next scrolls.
NameTypeDescription
valuenumberDefault value : 2
Indicates the number of scrolls before the callback is triggered
callbackfunctionDefault value : () => {}
Defines the function to be called when the number of scrolls is reached.
prevobjectAn object contain the various configuration parameters for horizontal previous scrolls.
NameTypeDescription
valuenumberDefault value : 2
Indicates the number of scrolls before the callback is triggered
callbackfunctionDefault value : () => {}
Defines the function to be called when the number of scrolls is reached.
yobjectEither: An object encapsulating the various configuration parameters for all vertical scrolls.
NameTypeDescription
valuenumberIndicates the number of scrolls before the callback is triggered
callbackfunctionDefines the function to be called when the number of scrolls is reached.
An object encapsulating the different directions of the scroll.
NameTypeDescription
nextobjectAn object contain the various configuration parameters for vertical next scrolls.
NameTypeDescription
valuenumberDefault value : 4
Indicates the number of scrolls before the callback is triggered
callbackfunctionDefault value : () => {}
Defines the function to be called when the number of scrolls is reached.
prevobjectAn object contain the various configuration parameters for vertical previous scrolls.
NameTypeDescription
valuenumberDefault value : 4
Indicates the number of scrolls before the callback is triggered
callbackfunctionDefault value : () => {}
Defines the function to be called when the number of scrolls is reached.

Touch

touch description :

TypeDescription
objectEither: An object encapsulating the various configuration parameters for all possible touch (X / Y - next / prev).
NameTypeDescription
valuenumberIndicates the number of distance covered when touched before the callback is triggered
callbackfunctionDefines the function to be called when the trigger is reached.
An object encapsulating the different touch axes.
NameTypeDescription
xobjectEither: An object encapsulating the various configuration parameters for all horizontal touches.
NameTypeDescription
valuenumberIndicates the number of distance covered when touched before the callback is triggered
callbackfunctionDefines the function to be called when the trigger is reached.
An object encapsulating the different directions of the touch.
NameTypeDescription
nextobjectAn object contain the various configuration parameters for horizontal next touches.
NameTypeDescription
valuenumberDefault value : 60
Indicates the number of distance covered when touched before the callback is triggered
callbackfunctionDefault value : () => {}
Defines the function to be called when the trigger is reached.
prevobjectAn object contain the various configuration parameters for horizontal previous touches.
NameTypeDescription
valuenumberDefault value : 60
Indicates the number of distance covered when touched before the callback is triggered
callbackfunctionDefault value : () => {}
Defines the function to be called when the trigger is reached.
yobjectEither: An object encapsulating the various configuration parameters for all vertical touches.
NameTypeDescription
valuenumberIndicates the number of distance covered when touched before the callback is triggered
callbackfunctionDefines the function to be called when the trigger is reached.
An object encapsulating the different directions of the touch.
NameTypeDescription
nextobjectAn object contain the various configuration parameters for vertical next touches.
NameTypeDescription
valuenumberDefault value : 150
Indicates the number of distance covered when touched before the callback is triggered
callbackfunctionDefault value : () => {}
Defines the function to be called when the trigger is reached.
prevobjectAn object contain the various configuration parameters for vertical previous touches.
NameTypeDescription
valuenumberDefault value : 150
Indicates the number of distance covered when touched before the callback is triggered
callbackfunctionDefault value : () => {}
Defines the function to be called when the trigger is reached.

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.

Keywords

FAQs

Last updated on 19 Jul 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc