Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

on-intersect

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

on-intersect

Call back when an element intersects with another

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9
increased by50%
Maintainers
1
Weekly downloads
 
Created
Source

on-intersect stability

npm version build status downloads js-standard-style

Call back when an element intersects with another.

Usage

var intersectExists = require('on-intersect/exists')
var onViewport = require('on-intersect')
var html = require('bel')

var el = html`
  <h1 style="color: white">
    BEHOLD THE GOBLIN!
  </h1>
`

var main = html`
  <main>
    <div style="height: 110vh"></div>
    ${el}
    <div style="height: 110vh"></div>
  </main>
`

document.body.appendChild(main)

// observers should only be added _after_ the element is rendered on the DOM,
// else it displeases the browser emperors and they _will_ warn you
if (intersectExists()) {
  var stopObserving = onIntersect(el, onEnter, onExit)
}

function onEnter (entry) {
  console.log(entry.time)
  console.log(entry.rootBounds)
  console.log(entry.intersectionRect)
  console.log(entry.intersectionRatio)
  console.log(entry.target)

  document.body.setAttribute('style', 'background-color: green')
}

function onExit (entry) {
  document.body.setAttribute('style', 'background-color: white')
  stopObserving()
}

API

stopObserving = onViewport(elementOrObject, [onEnter|null], [onExit])

Call a callback when an element intersects with another. Defaults to document.body. The first argument can either be an HTML Node or an object. If it's an object it must contain a target which is an HTML node. Options can be:

  • target: The element that is being observed
  • root: The element that is used as the viewport for checking visiblity of the target. Defaults to the browser viewport.
  • rootMargin: Margin around the root. Can have values similar to the css margin property: "10px 20px 30px 40px" (top, right, bottom, left). If the root element is specified % values can be used.
  • threshold: Number or array of numbers to indicate at what % of visiblity of the target the observer should trigger. eg: trigger for every 25 percent that comes into view: [0, 0.25, 0.5, 0.75, 1]. Will call onEnter every time the threshold is passed. threshold defaults to 1.

stopObserving()

Unregister the observer

bool = intersectExists()

Check if window.InterSectionObserver exists

Installation

$ npm install on-intersect

See Also

Further Reading

Authors

License

MIT

Keywords

FAQs

Package last updated on 08 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc