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

@swgrhck/scroll-monitor

v0.6.0

Published

This is a JavaScript plugin that monitors and responds to scroll event, and supports the usage via data attributes to separate html from script.

Downloads

2

Readme

scroll-monitor

BrowserStack Status Build Status Coverage Status

中文文档

This is a JavaScript plugin that monitors and responds to scroll event, and supports the usage via data attributes to separate html from script.

[TOC]

Introduction

The topology of scroll-monitor is illustrated as follow:

Scroll Monitor Typology

Every Monitor has a target, when target scrolls, Monitor will receive scrolling events dispatched by target. Monitor can register Resolvers, which can resolve scrolling events delivered by Monitor and convert scroll events to other events, such as scrolling up events, and then return converted events to Monitor. Monitor will dispatch converted events to Subscribers when receiving, so Subscribers can respond to certain scrolling events, such as executing some JavaScript when target scrolls up.

So, the processing procedure is as follow:

  1. target scrolls.
  2. Monitor receives scrolling events and delivers to Resolvers.
  3. Resolvers resolve scrolling events, convert to other events and return to Monitor.
  4. Monitor receives converted events and dispatchs to Subscribers.
  5. Subscribers can respond to converted scrolling events, such as scrolling up.

Installation

Include scroll-monitor.umd.min.js script, or standalone JS files, such as monitor.min.js, if you only want to use partial components:

<script src="scroll-monitor.umd.min.js"></script>

<!-- or if you only want to use partial components -->
<script src="monitor.min.js"></script>
<!-- other components -->

Monitor

The core component of this plugin is Monitor, which monitors scroll events of targets, resolves ScrollMetric of target, delivers ScrollMetrics and scrolling events to Resolvers, then dispatchs converted events to subscribers, so subscribers can listen to converted scrolling event, such as scrolling up. See follow charpter for more information about ScrollMetric.

By data attributes

The easiest and preferred way to subscribe to Monitor is by data attribute data-monitor="scroll":

<div data-monitor="scroll"></div>

The div above will monitor scrolling events of window, so when window scrolls, Monitor will dispatch events converted by Resolvers to this div.

You can also specify the targets of Monitor by data attribute data-monitor-target="<querySelector>":

<div class="monitored">
  <div data-monitor="scroll" data-monitor-target=".monitored"></div>
</div>

Now the div will monitor scroll events of all elements with class monitored.

By JavaScript

The other way to subscribe to Monitor is by JavaScript:

const Monitor = window.Monitor || window.scrollMonitor.Monitor
let monitor = Monitor.of(target)
monitor.subscribe(subscriber)

You can invoke Monitor.of(target) to get the monitor of target, and then subscribe subscriber by invoking monitor.subscribe(subscriber).

The target must be an instance of Window or Element.

The subscriber must be an instance of EventTarget.

Register Resolvers

Monitor can register Resolvers to resolve scrolling events, there are two ways to register Resolvers:

  1. register as a global Resolver to all Monitors.

    const resolver = someResolver()
    Monitor.registerResolver(resolver)
  2. register as certain Monitor's own Resolver.

    const resolver = someResolver()
    Monitor.of(target).registerResolver(resolver)

The only requirement of Resolver is it must have a resolve function returing an array of Events:

const customResolver = {
  resolve(lastMetric, crtMetric, event) {
    const events = []
    // resolve and add converted events
    return events
  }
}

Monitor.registerResolver(customResolver)
// or
Monitor.of(target).registerResolver(customResolver)

ScrollMetric

ScrollMetric contains some scrolling metric about target, including 6 read-only properties:

  1. scrollHeight: a measurement of the height of an element's content, including content not visible on the screen due to overflow.
  2. scrollWidth: a measurement of the width of an element's content, including content not visible on the screen due to overflow.
  3. viewHeight: zero for elements with no CSS or inline layout boxes, otherwise it's the inner height of an element in pixels, including padding but not the horizontal scrollbar height, border, or margin.
  4. viewWidth: zero for elements with no CSS or inline layout boxes, otherwise it's the inner width of an element in pixels. It includes padding but not the vertical scrollbar (if present, if rendered), border or margin.
  5. top: a measurement of the distance from the element's top to its topmost visible content.
  6. left: a measurement of the distance from the element's left to its leftmost visible content.

The following figure illustrates the properties of ScrollMetric:

Scroll Metric Illustration

Scroll Direction Resolver

ScrollDirectionResolver is a resolver registered to Monitor, which can resolve scrolling events to recognize the scrolling direction.

If you include scroll-monitor.umd.min.js or scroll-monitor-direction.min.js, it will register a global ScrollDirectionResolver to all Monitors when document is ready.

By data attributes

The easiest and preferred way to use ScrollDirectionResolver is by data attribute data-monitor="scroll-direction":

<div data-monitor="scroll scroll-direction"></div>

The div above will monitor scrolling events of window, and toggle the classes according to scroll direction, by default, none classes will be toggled, you should specify the classes by data attributes:

  • data-scroll-up-classes="<classes>"
  • data-scroll-down-classes="<classes>"
  • data-scroll-left-classes="<classes>"
  • data-scroll-right-classes="<classes>"

You can specify multiple classes to be toggled by separating classes with spaces. For example:

<div data-monitor="scroll scroll-direction" data-scroll-up-classes="up scroll"></div>

Now when window scrolls up, the div will toggle class up and scroll.

See demo.

By JavaScript

The other way to use ScrollDirectionResolver is by Javascript. When target scrolls, ScrollDirectionResolver will resolve and dispatch 4 kinds of events:

  • scroll.up.scroll-monitor when target scrolls up.

  • scroll.down.scroll-monitor when target scrolls down.

  • scroll.left.scroll-monitor when target scrolls left.

  • scroll.right.scroll-monitor when target scrolls right.

This means that you can add event listeners to subscribers to do anything you want:

Monitor.of(window).subscribe(window)

window.addEventListener('scroll.up.scroll-monitor', event => {
  // anything you want to do
})

See demo.

Processing interval

Since scroll events can fire at a high rate, so ScrollDirectionResolver have a time interval to recognize direction, which defaults to 50ms. You can get or set interval by resolver.interval, or set interval when creating resolver by new ScrollDirectionResolver(interval).

const resolver = new ScrollDirectionResolver(interval)
resolver.interval = interval

Extend ScrollMonitor

The ScrollMonitor is flexible and pluggable, this means you can extend ScrollMonitor easily if present components can't satisfy your needs, the only thing you need to do is to register your own Resolver to Monitor:

const customResolver = {
  resolve(lastMetric, crtMetric, event) {
    const events = []
    // resolve and add converted events
    return events
  }
}

Monitor.registerResolver(customResolver)
// or
Monitor.of(target).registerResolver(customResolver)

The resolve function receives 3 arguments:

  1. lastMetric: the ScrollMetric before target scrolls.
  2. crtMetric : the ScrollMetric after target scrolls.
  3. event: the origin scroll event emitted by target.

Once you register your own Register, you can add EventListener to subscribers so that they can respond to events conveted by yourself properly.

Browser support

BrowserStack Logo

BrowserStack is a cross browser and real device web-based testing platform. BrowserStack can be used for interactive as well as automated testing through frameworks like Selenium, Karma and others.

This plugin is tested on multiple platforms and browsers under supports of BrowserStack:

  • Windows
  • Mac OS
  • Android
  • Chrome
  • Firefox
  • Safari
  • Microsoft Edge

License

The plugin is available as open source under the terms of the MIT License.