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

fold-notifier

v0.2.4

Published

Deal with the dom elements who enter the viewport

Downloads

9

Readme

fold-notifier

Deal with the dom elements who enter the viewport

Install

npm i fold-notifier

Package on npm

API

constructor(cb, [opts])

| Argument | Action | | :------ | :------- | | cb | the callback | | opts.attribute | optional targeted attribute, default to fold | | opts.offset | optional offset, default to 0 | | opts.target | optional targeted container, default to document.body |

The callback cb receive 2 arguments

| Argument | Action | | :------ | :------- | | el | the dom element entering the viewport | | data | a Plain Object with serialized attribute values |

You must call add or collect to start

add(arg, [arg], [...])

Add new elements manually

arg can be:

  • a Html Element
  • a NodeList
  • a flat Array or an Array of nested arg

Make sure that the array contains valid node elements with the correct opts.attribute

The elements are directly added in the previous internal data storage, faster than the collect method

const Fold = require('fold-notifier')

var fold = new Fold(/* ... */)

var ela = document.querySelector('p.a')
var elb = document.querySelector('p.b')
var elc = document.querySelector('p.c')
var lst = document.querySelectorAll('div')

fold.add(ela, [elb, elc], lst)

collect()

Collect new elements where opts.attribute is found

Use it when interesting new nodes are appended to the dom

Elements already "done" are ignored

It reset the internal data storage. Every element not "done" are tested / imported again

Safer than the add method with the opts.attribute test, but slower

Return the count of watched elements

Return the count of watched elements

<!-- .one and .two are ignored -->
<div class='rect one' fold fold-done></div>
<div class='rect two' fold='delay(1.3)' fold-done></div>

<!-- .three is collected -->
<div class='rect three' fold='delay(1.3)'></div>

kill()

Stop and delete all internal stuffs. Nothing can be done after that

Example 1

Two div will be animated when they enter the viewport

.rect {
  transform: translateY(80px) translateZ(0px);
  transition: all 3s cubic-bezier(.19, 1, .22, 1);
}

/* animation starts when the attribute `fold-done` is setted */
.rect[fold-done] {
  transform: translateY(0px);
}
<div class='rect one' fold></div>
<div class='rect two' fold='delay(1.3)'></div>
const Fold = require('fold-notifier')

// when update is invoked, the attribute `fold-done` was just setted to `el`
function update(el, data) {
  // `el` with class `.two` receive serialized data {offset:-25, delay:-1.3}
  if (data.delay != null) {
    el.style.transitionDelay = data.delay + 's'
  }
}

var fold = new Fold(update, {offset: -25})
fold.collect()

Example 2

Images are lazyloaded when they are about to enter the viewport

<!-- b.gif is the famous 1x1 transparent pixel image -->
<img class='one' src='b.gif' lazy='src(image1.jpg)'/>
<img class='two' src='b.gif' lazy='src(image2.jpg)'/>
const Fold = require('fold-notifier')

// when update is invoked, the attribute `lazy-done` was just setted to `el`
function update(el, data) {
  // `el` with class `.one` receive serialized data {offset:250, src:'image1.jpg'}
  // `el` with class `.two` receive {offset:250, src:'image2.jpg'}
  if (data.src != null) {
    el.src = data.src
  }
}

var fold = new Fold(update, {offset: 250, attribute:'lazy'})
fold.collect()

License

MIT