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

@superstructure.net/scrollprogressobserver

v0.0.2

Published

ScrollProgressObserver

Downloads

5

Readme

ScrollProgressObserver

Track the visibility and scroll progress of elements in the viewport. When do they enter and leave the viewport while scrolling? How far have they advanced through the viewport?

Demo

https://codesandbox.io/s/scrollprogressobserver-js-demo-xnm2k7?file=/src/Demo.js

Install

npm install '@superstructure.net/scrollprogressobserver'

Usage

import { ScrollProgressObserver } from "@superstructure.net/scrollprogressobserver";

const targetElement = document.querySelector(".target");
const options = {
  // This configuration starts tracking the target element when its
  // top edge passes a point 10vh from the viewport's bottom edge and
  // stops tracking when the target element's bottom edge passes a
  // point 20vh from the viewport's top edge.
  //
  // No worries, the default settings or fine, too.
  startTargetEdge: "start",
  endTargetEdge: "end",
  startViewportEdge: 0.9,
  endViewportEdge: 0.2,

  // Callbacks
  onEnter: () => {
    console.log("target element tracking started.");
  },
  onLeave: () => {
    console.log("target element tracking stopped.");
  },
  onProgress: (progress) => {
    console.log(
      `target element has progressed ${progress * 100}% through the viewport.`
    );
  },
};

const observer = new ScrollProgressObserver(targetElement, options);

Options

scrollDirection

y or x

Default: y

observeResize

Update the instance when the target element or the viewport is resized. Uses ResizeObserver internally.

Default: true

Manually call the resize() method when setting observeResize: false.

animationLoop

Default: true

Run an internal animation loop based on requestAnimationFrame. Manually call the onFrame method when using an external animation loop.

Tracking edges

startTargetEdge

Defines the target element's edge that starts tracking when scrolling in positive direction.

start, center, end or float from 0...1 (start...end).

Default: start

Example: When setting startTargetEdge: "start" with scrollDirection: "y", tracking starts when the target element's top edge passes the startViewportEdge (defaults to the bottom edge of the screen).

endTargetEdge

Defines the target element's edge that ends tracking when scrolling in positive direction.

start, center, end or float from 0...1 (start...end).

Default: end

Example: When setting endTargetEdge: "end" with scrollDirection: "y", tracking ends when the target element's bottom edge passes the endViewportEdge (defaults to the top edge of the screen).

startViewportEdge

Defines the viewport's edge that starts tracking when scrolling in positive direction.

start, center, end or float from 0...1 (start...end).

Default: end

Example: When setting startViewportEdge: "end" with scrollDirection: "y", tracking starts when the target element's startTargetEdge (defaults to the top edge of the target element) passes the bottom edge of the viewport .

endViewportEdge

Defines the viewport's edge that ends tracking when scrolling in positive direction.

start, center, end or float from 0...1 (start...end).

Default: start

Example: When setting endViewportEdge: "start" with scrollDirection: "y", tracking ends when the target element's endTargetEdge (defaults to the bottom edge of the target element) passes the top edge of the viewport .

Callbacks

onEnter and onLeave

Called when tracking of the target element starts/ends. Can be controlled via startTargetEdge, endTargetEdge, startViewportEdge and endViewportEdge. Receives the target element as first argument.

onEnterViewport and onLeaveViewport

Called when the target element enters/leaves the viewport. Ignores startTargetEdge, endTargetEdge, startViewportEdge and endViewportEdge. Receives the target element as first argument.

onProgress

Called every frame whil tracking of the target element is active. Receives progress in the range 0...1 as first argument and the target element as the second argument.