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

@egstad/detect-scroll

v1.0.11

Published

A performant and lightweight ES6 module for detecting scroll activity (direction + location) for X and/or Y axis

Downloads

811

Readme

Detect Scroll 📜️ 🔍️ 👀️

A performant and lightweight (~1.6kb) ES6 module for detecting scroll activity (direction + location) for X and/or Y axis

Coverage:statements Coverage:functions Coverage:lines License: MIT

Introduction

The default scroll event listener is amazing and all, but isn't exactly the most usable tool out of the box. It's all, "Hey look, a user scrolled!". And I'm like "Great! But like, in what direction and where did they stop?". And then it's all like, "IDFK, how about you do some math and figure it out".

In short, this little library adds a handful of helpful CustomEvent's which make scroll detection a little more insightful.

View Example

Note: Open your console to preview debug mode


Installation

npm install @egstad/detect-scroll

Usage

Example One

  1. Install and import detectScroll.
  2. Select an individual element you'd like to monitor the scroll activity of. Can be a string selector (ie: '.scroll-container'), or any valid HTMLElement. In our case, we'll use the window.
  3. Register the Event listeners that you'd like to use. Any events that are not explicitly defined will not fire.
  4. If/when you want to destroy the instance, running destroy() will remove all event listeners.
import detectScroll from '@egstad/detect-scroll'

// setup instance & events
const instance = new detectScroll(window, {
  events: {
    scrollUp: foo(),
    scrollDown: bar(),
  },
})

// if/when you want to destroy the instance and events
instance.destroy()

Example Two

Another way to get up and running with this library is to handle the events yourself. This option registers and dispatches all Events, but you'll have to add/remove the event listeners yourself.

import detectScroll from '@egstad/detect-scroll'

// setup instance
const instance = new detectScroll(window)

// setup events
window.addEventListener('scrollUp', foo)
window.addEventListener('scrollDown', bar)

// if/when you want to destroy all events
window.removeEventListener('scrollUp', foo)
window.removeEventListener('scrollDown', bar)

Default Configuration

const instance = new detectScroll(window, {
  vertical: true,
  horizontal: true,
  passiveMode: true,
  debugMode: false,
  events: undefined,
})

Properties & Events worth knowing about

Optional Properties

| Optional Properties | Default Value | Description | | ------------------- | ------------- | --------------------------------------------- | | vertical | true | Registers & Dispatches y-axis activity | | horizontal | true | Registers & Dispatches x-axis activity | | passiveMode | true | Determines if scroll event is passive or not | | debugMode | false | Logs events in console as they dispatch | | events | undefined | Event overrides (see Events section for more) |

Events

Scroll events are dispatched using CustomEvent's. Here's a gorgeous list of all 10.

If you would like to a specific selection of them, check out this example.

| Custom Event Name | Description | | ----------------- | ------------------------------------------------------ | | scrollStart | Fired when scrolling begins | | scrollStop | Fired when scrolling ends | | scrollX | Fired every time x position updates | | scrollY | Fired every time y position updates | | scrollUp | Fired when scrolling up | | scrollDown | Fired when scrolling down | | scrollLeft | Fired when scrolling left | | scrollRight | Fired when scrolling right | | scrollMinX | Fired when the left-most part of el/window is reached | | scrollMaxX | Fired when the right-most part of el/window is reached | | scrollMinY | Fired when top of element/window is reached | | scrollMaxY | Fired when bottom of element/window is reached |

Event Template

const foo = (ev) => {
  console.log(ev.type)
}

const instance = new detectScroll(window, {
  events: {
    // exclude any of these and the event's won't be registered or fired
    scrollStart: foo,
    scrollStop: foo,
    scrollX: foo,
    scrollY: foo,
    scrollUp: foo,
    scrollDown: foo,
    scrollLeft: foo,
    scrollRight: foo,
    scrollMinX: foo,
    scrollMaxX: foo,
    scrollMinY: foo,
    scrollMaxY: foo,
  },
})