scroll-into-view-if-needed
Ponyfill for upcoming Element.scrollIntoView() APIs like scrollMode: if-needed, behavior: smooth and block: center
Last updated 9 years ago by stipsan .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install scroll-into-view-if-needed 
SYNC missed versions from official npm registry.

This is the work-in-progress, the current stable version is here

CircleCI Status npm stat npm version semantic-release scroll-into-view-if-needed

This used to be just a ponyfill for Element.scrollIntoViewIfNeeded but is currently being rewritten to cover Element.scrollIntoView(ScrollIntoViewOptions) including the new scrollMode: "if-needed" option. This readme will be updated when it's ready for stable release.

Demo

Install

yarn add scroll-into-view-if-needed@next

Usage

import scrollIntoView from 'scroll-into-view-if-needed'
const node = document.getElementById('hero')

// similar behavior as Element.scrollIntoView({block: "nearest", inline: "nearest"})
// only that it is a no-op if `node` is already visible
// see: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
// same behavior as Element.scrollIntoViewIfNeeded()
// see: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoViewIfNeeded
scrollIntoView(node, {
  scrollMode: 'if-needed',
  block: 'nearest',
  inline: 'nearest',
})

// same behavior as Element.scrollIntoViewIfNeeded(true) without the "IfNeeded" behavior
// see: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoViewIfNeeded
scrollIntoView(node, { block: 'center', inline: 'center' })
// scrollMode is "always" by default

// smooth scroll if the browser supports it and if the element isn't visible
scrollIntoView(node, { behavior: 'smooth', scrollMode: 'if-needed' })

Ponyfill smooth scrolling

If you're ok with a larger bundlesize and want the smooth scrolling behavior to be ponyfilled you can use the smooth-scroll-into-view-if-needed addon.

Custom scrolling transition

If the default smooth scrolling ponyfill isn't the duration or easing you want, you can provide your own scrolling logic by giving behavior a callback.

import scrollIntoView from 'scroll-into-view-if-needed'
const node = document.getElementById('hero')

scrollIntoView(node, {
  // Your scroll actions will always be an array, even if there is nothing to scroll
  behavior: scrollActions =>
    // list is sorted from innermost (closest parent to your target) to outermost (often the document.body or viewport)
    scrollActions.forEach(({ el, top, left }) => {
      // implement the scroll anyway you want
      el.scrollTop = top
      el.scrollLeft = left

      // If you need the relative scroll coordinates, for things like window.scrollBy style logic or whatever, just do the math
      const offsetTop = el.scrollTop - top
      const offsetLeft = el.scrollLeft - left
    }),
  // all the other options (scrollMode, block, inline) still work, so you don't need to reimplement them (unless you really really want to)
})

More documentation will be added (hang in there)

Current Tags

  • 3.1.0                                ...           latest (3 years ago)

83 Versions

  • 3.1.0                                ...           3 years ago
  • 3.0.10                                ...           4 years ago
  • 3.0.9                                ...           4 years ago
  • 3.0.8                                ...           4 years ago
  • 3.0.7                                ...           4 years ago
  • 3.0.6                                ...           4 years ago
  • 3.0.5                                ...           4 years ago
  • 3.0.4                                ...           4 years ago
  • 3.0.3                                ...           4 years ago
  • 3.0.2                                ...           4 years ago
  • 3.0.1                                ...           4 years ago
  • 3.0.0                                ...           4 years ago
  • 2.2.31                                ...           4 years ago
  • 2.2.30                                ...           4 years ago
  • 2.2.29                                ...           5 years ago
  • 2.2.28                                ...           6 years ago
  • 2.2.27                                ...           6 years ago
  • 2.2.26                                ...           6 years ago
  • 2.2.25                                ...           7 years ago
  • 2.2.24                                ...           7 years ago
  • 2.2.23                                ...           7 years ago
  • 2.2.22                                ...           7 years ago
  • 2.2.21                                ...           7 years ago
  • 2.2.20                                ...           8 years ago
  • 2.2.19                                ...           8 years ago
  • 2.2.18                                ...           8 years ago
  • 2.2.17                                ...           8 years ago
  • 2.2.16                                ...           8 years ago
  • 2.2.15                                ...           8 years ago
  • 2.2.14                                ...           8 years ago
  • 2.2.13                                ...           8 years ago
  • 2.2.12                                ...           8 years ago
  • 2.2.11                                ...           8 years ago
  • 2.2.10                                ...           8 years ago
  • 2.2.9                                ...           8 years ago
  • 2.2.8                                ...           8 years ago
  • 2.2.7                                ...           8 years ago
  • 2.2.6                                ...           8 years ago
  • 2.2.5                                ...           8 years ago
  • 2.2.4                                ...           9 years ago
  • 2.2.3                                ...           9 years ago
  • 2.2.2                                ...           9 years ago
  • 2.2.1                                ...           9 years ago
  • 2.2.0                                ...           9 years ago
  • 2.1.7                                ...           9 years ago
  • 2.1.6                                ...           9 years ago
  • 2.1.5                                ...           9 years ago
  • 2.1.4                                ...           9 years ago
  • 2.1.3                                ...           9 years ago
  • 2.1.2                                ...           9 years ago
  • 2.1.1                                ...           9 years ago
  • 2.1.0                                ...           9 years ago
  • 1.5.1 [deprecated]           ...           9 years ago
  • 2.0.1-alpha.1                                ...           9 years ago
  • 2.0.1-alpha.0                                ...           9 years ago
  • 2.0.0-alpha.4                                ...           9 years ago
  • 2.0.0                                ...           9 years ago
  • 2.0.0-alpha.d4e115e7                                ...           9 years ago
  • 2.0.0-alpha.f7841b3e                                ...           9 years ago
  • 2.0.0-alpha.3                                ...           9 years ago
  • 1.5.0                                ...           9 years ago
  • 1.4.1                                ...           9 years ago
  • 1.4.0                                ...           9 years ago
  • 1.3.0                                ...           9 years ago
  • 1.2.8                                ...           9 years ago
  • 1.2.7                                ...           9 years ago
  • 1.2.6                                ...           9 years ago
  • 1.2.5                                ...           9 years ago
  • 1.2.4                                ...           9 years ago
  • 1.2.3                                ...           9 years ago
  • 1.2.2                                ...           9 years ago
  • 1.2.1                                ...           9 years ago
  • 1.2.0                                ...           9 years ago
  • 1.1.1                                ...           9 years ago
  • 1.1.0                                ...           10 years ago
  • 1.0.7                                ...           10 years ago
  • 1.0.6                                ...           10 years ago
  • 1.0.5                                ...           10 years ago
  • 1.0.4                                ...           10 years ago
  • 1.0.3                                ...           10 years ago
  • 1.0.2                                ...           11 years ago
  • 1.0.1                                ...           11 years ago
  • 1.0.0                                ...           11 years ago
Maintainers (1)
Downloads
Total 355
Today 0
This Week 0
This Month 199
Last Day 0
Last Week 60
Last Month 30
Dependencies (0)
None

© 2010 - cnpmjs.org x YWFE | Home | YWFE