• Stars
    star
    136
  • Rank 267,670 (Top 6 %)
  • Language
    TypeScript
  • Created over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Smoothly scroll elements into view, cross browser!

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

More Repositories

1

scroll-into-view-if-needed

Element.scrollIntoView ponyfills for things like "if-needed" and "smooth"
JavaScript
1,065
star
2

react-spring-bottom-sheet

Accessible ♿️, Delightful ✨, & Fast 🚀
TypeScript
939
star
3

ioredis-mock

Emulates ioredis by performing all operations in-memory.
JavaScript
337
star
4

compute-scroll-into-view

Utility for calculating what should be scrolled, how it's scrolled is up to you
TypeScript
199
star
5

uikit-react

UIkit components built with React
JavaScript
111
star
6

bulma-loader

A Webpack loader for Bulma, a modern CSS framework based on Flexbox
JavaScript
34
star
7

redux-saga-sc

Provides sagas to easily dispatch redux actions over SocketCluster websockets
JavaScript
34
star
8

String.Slugify.js

Extends the String native object to have a slugify method, useful for url slugs.
JavaScript
27
star
9

epic

React example project, that takes you from fun development to high quality production
TypeScript
21
star
10

groqz

(experimental) Transforms GROQ strings to zod schemas in your TypeScript codebase.
TypeScript
14
star
11

nextjs-cv-cms-sanity-v3

My over-engineered CV
TypeScript
11
star
12

String.Inflector.js

Extends the String native with inflector methods, like pluralize and singularize.
9
star
13

uikit-loader

A Webpack CSS loader for UIkit, a lightweight and modular front-end framework for developing fast and powerful web interfaces
JavaScript
9
star
14

Element.Style.Transform.js

Provides a cross browser way of letting you use the CSS3 transform property. Inspired by http://github.com/zachstronaut/jquery-css-transform
JavaScript
9
star
15

public-talks

Slides and other stuff is all in this repo. Each talk is on its own branch
7
star
16

svgdiff

See the visual difference between two SVGs
TypeScript
7
star
17

redux-form-uikit

A set of wrapper components to facilitate using UIkit React with Redux Form
JavaScript
5
star
18

cocody.dev

My own website, wanna do some small blogging and share my fav resources.
JavaScript
3
star
19

react-transform-count-renders

React Transform that lets you console.count how many times your React components render
JavaScript
3
star
20

sanity-meetup-08-22

"v3 does what v2 don't"
TypeScript
3
star
21

system-font-stack

Give your web app a native look by using the font family of the users OS
TypeScript
3
star
22

trains

choo choo 🚂
JavaScript
2
star
23

postcss-import-svg

JavaScript
2
star
24

example-v3-studio

TypeScript
2
star
25

postcss-custom-properties-fallback

Adds fallbacks to your CSS var() functions
JavaScript
2
star
26

next-sanity-preview

JavaScript
2
star
27

express-pretty-error

Express compatible middleware for pretty errors in html, json, raw text, css and terminal contexts with stack traces included!
JavaScript
2
star
28

template-marketing-webapp-nextjs

TypeScript
1
star
29

serve-dynamic-favicon

middleware that serve dynamically generated favicons
JavaScript
1
star
30

example-npm

An example GitHub Action using npm
JavaScript
1
star
31

stipsan.me

My own personal website, don't really need one but it's a good excuse to try new tools and experiment 🚀
JavaScript
1
star
32

cancelpineapple.pizza

HTML
1
star
33

hyperfokus

Hyperfocus your todos until they're done
TypeScript
1
star
34

themer

Create Sanity Studio v3 themes!
TypeScript
1
star
35

graphql-field-resolver-to-typescript

Export ts definitions from your server .graphql files to strictly type your field resolvers
TypeScript
1
star
36

stipsan

1
star
37

scroll-into-view.dev

TypeScript
1
star
38

links

Because browser bookmarks sucks
1
star
39

renovate-presets

JavaScript
1
star
40

potatoes.fyi

HTML
1
star
41

top-github-code-reviewers

Showcase your most active code reviewers
JavaScript
1
star
42

nextjs-blog-cms-sanity-v3-netlify-test

TypeScript
1
star
43

redux-saga-sc-demo

A demo chat app showing redux-saga-sc in action
JavaScript
1
star
44

vc-app

TypeScript
1
star
45

gulp-purge-sourcemaps

Cleans up after gulp-sourcemaps have done a sourcemaps.write() allowing you to combine streams that generate both dev assets with sourcemaps and minified production assets.
JavaScript
1
star
46

sanity-template-gatsby-lps

JavaScript
1
star
47

example-v3-studio-next-runtime

A fork of stipsan/example-v3-studio to get around Vercel's "3 linked projects per repo" limit
TypeScript
1
star