Awesome Open Source
Awesome Open Source

viewprt Build Status

A tiny, dependency-free, high performance viewport position & intersection observation tool. You can watch when elements enter & exit a viewport, or when a viewport itself reaches its bounds. Use this as a building block for lazy loaders, infinite scrolling, etc.

Demo / Examples 🕹

Install

npm i viewprt -S

Usage & API

import {
  ElementObserver,   // Use this to observe when an element enters and exits the viewport
  PositionObserver   // Use this to observe when a viewport reaches its bounds
  ObserverCollection // Advanced: Used for grouping custom viewport handling
} from 'viewprt'

// All options are optional. The defaults are shown below.

// ElementObserver(element, options)
const elementObserver = ElementObserver(document.getElementById('element'), {
  onEnter(element, viewport) {},               // callback when the element enters the viewport
  onExit(element, viewport) {},                // callback when the element exits the viewport
  offset: 0,                                   // offset from the edges of the viewport in pixels
  once: false,                                 // if true, observer is detroyed after first callback is triggered
  observerCollection: new ObserverCollection() // Advanced: Used for grouping custom viewport handling
})

// PositionObserver(options)
const positionObserver = PositionObserver({
  onBottom(container, viewport) {},            // callback when the viewport reaches the bottom
  onTop(container, viewport) {},               // callback when the viewport reaches the top
  onLeft(container, viewport) {},              // callback when the viewport reaches the left
  onRight(container, viewport) {},             // callback when the viewport reaches the right
  onFit(container, viewport) {},               // callback when the viewport contents fit within the container without having to scroll
  container: document.body,                    // the viewport element to observe the position of
  offset: 0,                                   // offset from the edges of the viewport in pixels
  once: false,                                 // if true, observer is detroyed after first callback is triggered
  observerCollection: new ObserverCollection() // Advanced: Used for grouping custom viewport handling
})

The viewport argument in callbacks is an object containing the current state of the viewport e.g.:

{
  width: 1024,
  height: 768,
  positionX: 0,
  positionY: 2000
  directionY: "down",
  directionX: "none"
}
// Stop observing:
positionObserver.destroy()
elementObserver.destroy() // This happens automatically if the element is removed from the DOM

// Start observing again:
positionObserver.activate()
elementObserver.activate()

Advanced: Using a custom observer collection

If you need to control scroll and resize events (e.g. for custom throttling/debouncing), you can create a new instance of ObserverCollection.

const debouncedObserverCollection = new ObserverCollection({ handleScrollResize: h => debounce(h, 300) })

const elementObserver = ElementObserver(document.getElementById('element1'), {
  observerCollection: debouncedObserverCollection
})

// The same instance of ObserverCollection should be reused to have only one scroll and resize event
const elementObserver = ElementObserver(document.getElementById('element2'), {
  observerCollection: debouncedObserverCollection
})

Browser support

Chrome, Firefox, Edge, IE 11+, Safari 8+
(requestAnimationFrame, MutationObserver, Map)


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
javascript (68,779) 
scrolling (90) 
lazy-loading (65) 
infinite-scroll (62) 
viewport (37) 

Find Open Source By Browsing 7,000 Topics Across 59 Categories

Advertising 📦 10
All Projects
Application Programming Interfaces 📦 124
Applications 📦 192
Artificial Intelligence 📦 78
Blockchain 📦 73
Build Tools 📦 113
Cloud Computing 📦 80
Code Quality 📦 28
Collaboration 📦 32
Command Line Interface 📦 49
Community 📦 83
Companies 📦 60
Compilers 📦 63
Computer Science 📦 80
Configuration Management 📦 42
Content Management 📦 175
Control Flow 📦 213
Data Formats 📦 78
Data Processing 📦 276
Data Storage 📦 135
Economics 📦 64
Frameworks 📦 215
Games 📦 129
Graphics 📦 110
Hardware 📦 152
Integrated Development Environments 📦 49
Learning Resources 📦 166
Legal 📦 29
Libraries 📦 129
Lists Of Projects 📦 22
Machine Learning 📦 347
Mapping 📦 64
Marketing 📦 15
Mathematics 📦 55
Media 📦 239
Messaging 📦 98
Networking 📦 315
Operating Systems 📦 89
Operations 📦 121
Package Managers 📦 55
Programming Languages 📦 245
Runtime Environments 📦 100
Science 📦 42
Security 📦 396
Social Media 📦 27
Software Architecture 📦 72
Software Development 📦 72
Software Performance 📦 58
Software Quality 📦 133
Text Editors 📦 49
Text Processing 📦 136
User Interface 📦 330
User Interface Components 📦 514
Version Control 📦 30
Virtualization 📦 71
Web Browsers 📦 42
Web Servers 📦 26
Web User Interface 📦 210