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

smooth-scroll-into-view-if-needed

v2.0.2

Published

Ponyfill for smooth scrolling elements into view (if needed!)

Downloads

365,758

Readme

CircleCI Status npm stat npm version gzip size size module formats: umd, cjs, and es semantic-release smooth-scroll-into-view-if-needed

This is an addon to scroll-into-view-if-needed that ponyfills smooth scrolling. And while scroll-into-view-if-needed use the same default options as browsers and the spec does, this library is a bit more opinionated and include bonus features that help you build great UIs.

Demo

Install

yarn add smooth-scroll-into-view-if-needed

The UMD build is also available on unpkg:

<script src="https://unpkg.com/smooth-scroll-into-view-if-needed/umd/smooth-scroll-into-view-if-needed.min.js"></script>

You can find the library on window.scrollIntoView.

Usage

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

// `options.behavior` is set to `smooth` by default so you don't have to pass options like in `scroll-into-view-if-needed`
scrollIntoView(node)

// combine it with any of the other options from 'scroll-into-view-if-needed'
scrollIntoView(node, {
  scrollMode: 'if-needed',
  block: 'nearest',
  inline: 'nearest',
})

// a promise is always returned to help reduce boilerplate
const sequence = async () => {
  const slide = document.getElementById('slide-3')
  // First smooth scroll to hero
  await scrollIntoView(node, { behavior: 'smooth' })
  // Then we scroll to a slide in a slideshow
  return scrollIntoView(slide, { behavior: 'smooth' })
}

Polyfills

This library rely on Promise and requestAnimationFrame. This library does not ship with polyfills for these to keep bundlesizes as low as possible.

API

Check the full API in scroll-into-view-if-needed.

scrollIntoView(target, [options]) => Promise

scroll-into-view-if-needed does not return anything, while this library will return a Promise that is resolved when all of the scrolling boxes are finished scrolling.

The ability to cancel animations will be added in a future version.

options

Type: Object

behavior

Type: 'auto' | 'smooth' | Function Default: 'smooth'

This option deviates from scroll-into-view-if-needed in two ways.

  • The default value is smooth instead of auto
  • Using smooth adds it to browsers that miss it, and overrides the native smooth scrolling in the browsers that have it to ensure the scrolling is consistent in any browser.

The options auto or Function behaves exactly like in scroll-into-view-if-needed.

duration

Type: number Default: 300

Introduced in v1.1.0

This setting is not a hard limit. The duration of a scroll differs depending on how many elements is scrolled, and the capabilities of the browser. On mobile the browser might pause or throttle the animation if the user switches to another tab. And there might be nothing to scroll. No matter the scenario a Promise is returned so you can await on it.

ease

Type: Function

Introduced in v1.1.0

The default easing is easeOutQuint based on these algorithms: https://gist.github.com/gre/1650294#file-easing-js

Linear example:

scrollIntoView(node, {
  ease: (t) => t,
})

Acceleration until halfway, then deceleration:

scrollIntoView(node, {
  ease: (t) =>
    t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1,
})

Sine easing in and out:

scrollIntoView(node, {
  ease: (t) => (1 + Math.sin(Math.PI * t - Math.PI / 2)) / 2,
})

Credits

  • smoothscroll for the reference implementation of smooth scrolling.

More documentation will be added