All Projects → stutrek → Scrollmonitor

stutrek / Scrollmonitor

Licence: mit
A simple and fast API to monitor elements as you scroll

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Scrollmonitor

Motus
Animation library that mimics CSS keyframes when scrolling.
Stars: ✭ 502 (-84.55%)
Mutual labels:  viewport, scroll, dom
onscroll-effect
A tiny JavaScript library to enable CSS animations when user scrolls.
Stars: ✭ 35 (-98.92%)
Mutual labels:  scroll, viewport
Emergence.js
Detect element visibility in the browser
Stars: ✭ 1,891 (-41.82%)
Mutual labels:  viewport, scroll
React Scroll Sync
Synced scroll position across multiple scrollable elements
Stars: ✭ 252 (-92.25%)
Mutual labels:  scroll, dom
render-props
㸚 Easy-to-use React state containers which utilize the render props (function as child) pattern
Stars: ✭ 33 (-98.98%)
Mutual labels:  dom, viewport
react-scroll-trigger
📜 React component that monitors scroll events to trigger callbacks when it enters, exits and progresses through the viewport. All callback include the progress and velocity of the scrolling, in the event you want to manipulate stuff based on those values.
Stars: ✭ 126 (-96.12%)
Mutual labels:  scroll, viewport
scrollparent.js
A function to get the scrolling parent of an html element.
Stars: ✭ 51 (-98.43%)
Mutual labels:  dom, scroll
Arachni
Web Application Security Scanner Framework
Stars: ✭ 2,942 (-9.48%)
Mutual labels:  dom
React Horizontal Scrolling Menu
Horizontal scrolling menu component for React.
Stars: ✭ 289 (-91.11%)
Mutual labels:  scroll
Inview notifier list
A Flutter package that builds a list view and notifies when the widgets are on screen.
Stars: ✭ 269 (-91.72%)
Mutual labels:  scroll
Hyperhtml
A Fast & Light Virtual DOM Alternative
Stars: ✭ 2,872 (-11.63%)
Mutual labels:  dom
Rye
A modern, lightweight browser library using ES5 natives
Stars: ✭ 271 (-91.66%)
Mutual labels:  dom
Uumarqueeview
[iOS]Customizable marquee view. #Marquee,MarqueeView,跑马灯,滚屏,上翻,左滑,多行,自定义
Stars: ✭ 295 (-90.92%)
Mutual labels:  scroll
Ngx Scroll To
Scroll to any element to enhance scroll-based features in you app. Works for Angular 4+, both AoT and SSR. No dependencies.
Stars: ✭ 269 (-91.72%)
Mutual labels:  scroll
Typescript Dom Lib Generator
Tool for generating dom related TypeScript and JavaScript library files
Stars: ✭ 300 (-90.77%)
Mutual labels:  dom
Wrnavigationbar
超简单!!! 一行代码设置状态栏、导航栏按钮、标题、颜色、透明度,移动等 WRNavigationBar which allows you to change NavigationBar's appearance dynamically
Stars: ✭ 2,923 (-10.06%)
Mutual labels:  scroll
Tags Input
🔖 <input type="tags"> like magic
Stars: ✭ 312 (-90.4%)
Mutual labels:  dom
Vue Happy Scroll
基于vue2.0实现的滚动条插件。scroll component for vue2.0
Stars: ✭ 299 (-90.8%)
Mutual labels:  scroll
Javascript For Everyone
A step by step guide to learn JavaScript and programming
Stars: ✭ 285 (-91.23%)
Mutual labels:  dom
Stickystack.js
A jQuery plugin that creates a stacking effect by sticking panels as they reach the top of the viewport.
Stars: ✭ 287 (-91.17%)
Mutual labels:  viewport

scrollMonitor

The scroll monitor allows you to receive events when elements enter or exit a viewport. It does this using watcher objects, which watch an element and trigger events. Watcher objects also contain information about the element they watch, including the element's visibility and location relative to the viewport. If your scroll container is an element other than the body you can create a container that creates watchers.

The scroll monitor was designed to be very fast. On each scroll event the DOM is only touched twice, once to find the document height and again to find the viewport top. No variables are declared, nor are any objects, arrays, or strings created. Watchers are very cheap. Create them liberally.

The code is vanilla javascript and has no external dependencies, however the script cannot be put in the head.

Also see the React hooks, React component and the parallax library.

Basic Usage

When the body scrolls

var scrollMonitor = require("scrollmonitor"); // if you're old school you can use the scrollMonitor global.
var myElement = document.getElementById("itemToWatch");

var elementWatcher = scrollMonitor.create( myElement );

elementWatcher.enterViewport(function() {
    console.log( 'I have entered the viewport' );
});
elementWatcher.exitViewport(function() {
    console.log( 'I have left the viewport' );
});

For a scroll container

var containerElement = document.getElementById("container");

var containerMonitor = scrollMonitor.createContainer(containerElement);
// this containerMonitor is an instance of the scroll monitor
// that listens to scroll events on your container.

var childElement = document.getElementById("child-of-container");
var elementWatcher = containerMonitor.create(childElement);

elementWatcher.enterViewport(function() {
    console.log( 'I have entered the viewport' );
});
elementWatcher.exitViewport(function() {
    console.log( 'I have left the viewport' );
});

Note: an element is said to be in the viewport if it is scrolled into its parent, it does not matter if the parent is in the viewport.

Demos

Watcher Objects

Create watcher objects with scrollMonitor.create( watchItem ). An optional second argument lets you receive events before or after this element enters the viewport. See "Offsets".

watchItem can be one of the following:

  • DOM Element - the watcher will watch the area contained by the DOM element.
  • Object - obj.top and obj.bottom will be used for watcher.top and watcher.bottom.
  • Number - the watcher will watch a 1px area this many pixels from the top. Negative numbers will watch from the bottom.
  • jQuery object - it will use the first DOM element.
  • NodeList or Array - it will use the first DOM element.
  • string - it will use the string as a CSS selector and watch the first match.

Watchers are automatically recalculated on the first scroll event after the height of the document changes.

Events

Element watchers trigger six events:

  • visibilityChange - when the element enters or exits the viewport.
  • stateChange - similar to visibilityChange but is also called if the element goes from below the viewport to above it in one scroll event or when the element goes from partially to fully visible or vice versa.
  • enterViewport - when the element enters the viewport.
  • fullyEnterViewport - when the element is completely in the viewport [1].
  • exitViewport - when the element completely leaves the viewport.
  • partiallyExitViewport - when the element goes from being fully in the viewport to only partially [2].
  1. If the element is larger than the viewport fullyEnterViewport will be triggered when the element spans the entire viewport.
  2. If the element is larger than the viewport partiallyExitViewport will be triggered when the element no longer spans the entire viewport.

Properties

  • elementWatcher.isInViewport - true if any part of the element is visible, false if not.
  • elementWatcher.isFullyInViewport - true if the entire element is visible [1].
  • elementWatcher.isAboveViewport - true if any part of the element is above the viewport.
  • elementWatcher.isBelowViewport - true if any part of the element is below the viewport.
  • elementWatcher.top - distance from the top of the document to the top of this watcher.
  • elementWatcher.bottom - distance from the top of the document to the bottom of this watcher.
  • elementWatcher.height - top - bottom.
  • elementWatcher.watchItem - the element, number, or object that this watcher is watching.
  • elementWatcher.offsets - an object that determines the offsets of this watcher. See "Offsets".
  1. If the element is larger than the viewport isFullyInViewport is true when the element spans the entire viewport.

Methods

  • elementWatcher.on/off/one - the standard event functions.
  • elementWatcher.recalculateLocation - recalculates the location of the element in relation to the document.
  • elementWatcher.destroy - removes this watcher and clears out its event listeners.
  • elementWatcher.lock - locks this watcher at its current location. See "Locking".
  • elementWatcher.unlock - unlocks this watcher.

These methods are automatically called by the scrollMonitor, you should never need them:

  • elementWatcher.update - updates the boolean properties in relation to the viewport. Does not trigger events.
  • elementWatcher.triggerCallbacks - triggers any callbacks that need to be called.

Locking

Sometimes you want to change the element you're watching, but want to continue watching the original area. One common use case is setting position: fixed on an element when it exits the viewport, then removing positioning when it when it reenters.

var watcher = scrollMonitor.create( $element );
watcher.lock(); // ensure that we're always watching the place the element originally was

watcher.exitViewport(function() {
    $element.addClass('fixed');
});
watcher.enterViewport(function() {
    $element.removeClass('fixed');
});

Because the watcher was locked on the second line, the scroll monitor will never recalculate its location.

Offsets

If you want to trigger an event when the edge of an element is near the edge of the viewport, you can use offsets. The offset is the second argument to scrollMonitor.create.

This will trigger events when an element gets within 200px of the viewport:

scrollMonitor.create( element, 200 )

This will trigger when the element is 200px inside the viewport:

scrollMonitor.create( element, -200 )

If you only want it to affect the top and bottom differently you can send an object in.

scrollMonitor.create( element, {top: 200, bottom: 50})

If you only want it to affect the top and not the bottom you can use only one property in.

scrollMonitor.create( element, {top: 200})

scrollMonitor Module

Methods

  • scrollMonitor.createContainer( containerEl ) - returns a new ScrollMonitorContainer that can be used just like the scrollMonitor module.
  • scrollMonitor.create( watchItem, offsets ) - Returns a new watcher. watchItem is a DOM element, jQuery object, NodeList, CSS selector, object with .top and .bottom, or a number.
  • scrollMonitor.update() - update and trigger all watchers.
  • scrollMonitor.recalculateLocations() - recalculate the location of all unlocked watchers and trigger if needed.

Properties

  • scrollMonitor.viewportTop - distance from the top of the document to the top of the viewport.
  • scrollMonitor.viewportBottom - distance from the top of the document to the bottom of the viewport.
  • scrollMonitor.viewportHeight - height of the viewport.
  • scrollMonitor.documentHeight - height of the document.

Contributing

There is a set of unit tests written with Mocha + Chai that run in testem. Getting them running is simple:

npm install
npm test

then open http://localhost:7357

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].