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

scroll-triggers

v4.4.0

Published

A tiny, dependency-less javascript library to add classes as elements scroll into viewport.

Downloads

1,913

Readme

scroll-triggers npm

A tiny, dependency-less javascript library to add classes as elements scroll into viewport.

Features

  • Add a class when an element comes into view (great for animations)
  • Set an image when an element comes into view (great for lazy loading)
  • Set the width of an element based on scroll % (great for scroll progress bars)
  • API for both HTML and Javascript

Install

npm install scroll-triggers

Setup

import 'scroll-triggers';

// alternative
import scrollTriggers from 'scroll-triggers';

Events

Custom events are fired/listened on the element:

| Event | Type | Description | |----------------------------|----------|-------------------------| | scrolltriggers:inView | Fired | Element is in view | | scrolltriggers:outOfView | Fired | Element is out of view | | scrolltriggers:pause | Listened | Pauses scroll-triggers | | scrolltriggers:resume | Listened | Resumes scroll-triggers |

Options

List of available options:

| Name | Type | Description | |---------------|---------------------------------------------------|--------------------------------------------------------------------------| | className | {string} | Class to be added/removed when element is in view | | start | {string|Element|NodeList} CSS Selector | Add class when the specified element is in view | | end | {string|Element|NodeList} CSS Selector | Removes class when the specified element is in view | | position | {string = 'bottom'} "top|middle|bottom" | Add class at when element hits the specified position of page | | positionEnd | {string = 'bottom'} "auto|top|middle|bottom" | Removes class when specified element hits the specified position of page | | offset | {number} | The offset controls the distance (negative or positive) between the top border of the element and the top border of the window. This is useful to fine tune when a class is added. | | image | {string} Path to image | Set an image when an element comes into view, if the element is an it will set it as the src, otherwise it will be set as background-image | | src | {string} Path to resource | Set the src property when an element comes into view | | srcset | {string} Path to resource | Set the srcset property when an element comes into view | | progress | {boolean = false} | Set the width of an element based on scroll % | | once | {boolean = true} | Whether scroll-triggers should be executed once or not | | fixed | {boolean = true} | Needed for fixed (position: fixed) elements | | inView | {function} | Callback executed when element is in view | | outOfView | {function} | Callback executed when element is out view |

Usage

See the example.

HTML

Add class when element is in view.

<div data-scroll data-scroll-class="class-to-add"></div>

Add class when another element is in view

<div data-scroll data-scroll-class="class-to-add" data-scroll-start=".some .selector"></div>

Add class when another element is in view and remove when it gets to another element

<div data-scroll data-scroll-class="class-to-add" data-scroll-start=".some .selector" data-scroll-end=".some .lower .selector"></div>

Add class at when element hits bottom of page

<div data-scroll data-scroll-class="class-to-add" data-scroll-position="bottom"></div>

Add class at when element hits middle of page

<div data-scroll data-scroll-class="class-to-add" data-scroll-position="middle"></div>

Set an image when an element comes into view as a background image

<div data-scroll data-scroll-image="/path/to/image.jpg"></div>

<!--
  This will generate the markup below:
  <div data-scroll data-scroll-image="/path/to/image.jpg" style="background-image: url('/path/to/image.jpg'); background-repeat: no-repeat;"></div>
-->

Set the width of an element based on scroll % (great for progress bars)

<div data-scroll data-scroll-progress></div>

Set the src property when an element comes into view (great for lazy load)

<iframe data-scroll data-scroll-src="https://wikipedia.org/wiki/Main_Page"/></iframe>

Set the srcset property when an element comes into view (great for lazy load)

<picture>
  <source media="(min-width: 650px)" data-scroll data-scroll-srcset="http://placehold.it/465x465?text=Min-650" />
</picture>

JavaScript

import scrollTriggers from 'scroll-triggers';

scrollTriggers([
  {
    el: '.some-selector',
    start: '.selector',
    end: '.selector',
    className: 'class-to-add',
    image: 'image/path.jpg',
    src: 'http://url-to-resource.com',
    srcSet: 'http://url-to-resource.com',
    position: 'top|middle|bottom',
    positionEnd: 'auto|top|middle|bottom',
    offset: -20,
    progress: true|false,
    once: true|false,
    fixed: true|false,
    inView: (el, options) => {},
    outOfView: (el, options) => {}
  }
]);