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

simple-scroll-hook

v0.2.1

Published

A jQuery-less, simple library for hooking CSS class changes to HTML elements as they scroll into view.

Downloads

10

Readme

simple-scroll-hook

A simple, jQuery-less library for hooking HTML elements' CSS class changes to the window scroll. Multiple events can be bound to the scrollHook object, which creates only one scroll listener for everything hooked in. Additionally, after all its hooked events are fired, it cleans up after itself and removes its listeners to prevent memory leaks.

What are some use cases?

As of now, simple-scroll-hook works ideally as the user scrolls down a site.

Example use cases include fading in/sliding up text as the user scrolls into a new section, or making elements sticky as they scroll down.

Both can be seen in the example.html in this repo. To view, you can simply run the following in the command line:

open example.html

Installation

simple-scroll-hook can be used as:

  1. A <script> tag, creating a window.scrollHook singleton object.
  2. A Node.js / CommonJS module
  3. An AMD module

Usage

Say we have these CSS classes:

.not-sticky {
  position: absolute;
  bottom: 0;
  right: 0;
}

.sticky {
  position: fixed;
  top: 0;
  right: 0;
}

And want to apply the .sticky class when the top of the viewport hits the top of <div class='not-sticky'>. We also want to remove .not-sticky.

Example as a module. var scrollHook = require('simple-scroll-hook'); can easily be replaced with var scrollHook = window.scrollHook; if used as a <script> global.

var scrollHook = require('simple-scroll-hook');


// Assume this exists in the DOM
var sticky = document.querySelector('.not-sticky');

// Transition states when top of viewport hits the top of this element
var transitionPos = sticky.getBoundingClientRect().top + window.innerHeight;

scrollHook.register(sticky, {
  initialStates: 'not-sticky',
  finalStates: 'sticky',
  position: transitionPos
});

// Register more transitions if desired

// Only call check for transitions every 250ms (Default 50ms).
scrollHook.setThrottleTime(250);

// This specific call will add the window listener
scrollHook.start();

That's it! Transitions should work as a user scrolls down, and simple-scroll-hook will automatically clean up after itself after all transitions are completed. This will also work if you have CSS3 animations in your classes.

Many of the functions are also chainable, so the above code could also be written as:

var scrollHook = require('simple-scroll-hook');


var sticky = document.querySelector('.not-sticky');
var transitionPos = sticky.getBoundingClientRect().top + window.innerHeight;

var exampleConfig = {
  initialStates: 'not-sticky',
  finalStates: 'sticky',
  position: transitionPos
};

scrollHook.register(sticky, exampleConfig)
          .register(anotherEl, anotherConfig);
          .register(yetAnotherEl, yetAnotherConfig)
          .setThrottleTime(250)
          .start();

API

scrollHook.register(element, config)

Register an element, and its configuration.

// Example configuration:
{
  initialStates: {string | array},  CSS class(es) of the element when out of view.
  finalStates: {string | array},    CSS class(es) of the element when scrolled into view.
  position: {int}                   (optional) Where to transition classes.
}

Note, simple-scroll-hook can act to just remove initialStates, making finalStates optional. It can also be done vice-versa.

position, if not specified, will default to the element.offsetTop.

scrollHook.setThrottleTime(ms)

Default 50ms. Set the interval between firing wheel event callbacks.

scrollHook.start()

Add the wheel event listener, and fire registered events.

Contributing

Issues and Pull requests are always welcome. Relevant test coverage is required, please.