All Projects → stipsan → smooth-scroll-into-view-if-needed

stipsan / smooth-scroll-into-view-if-needed

Licence: MIT license
Smoothly scroll elements into view, cross browser!

Programming Languages

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

Projects that are alternatives of or similar to smooth-scroll-into-view-if-needed

Mos
一个用于在 macOS 上平滑你的鼠标滚动效果或单独设置滚动方向的小工具, 让你的滚轮爽如触控板 | A lightweight tool used to smooth scrolling and set scroll direction independently for your mouse on macOS
Stars: ✭ 7,772 (+5334.97%)
Mutual labels:  smooth-scrolling
Smooth Scrollbar
Customizable, Pluginable, and High-Performance JavaScript-Based Scrollbar Solution.
Stars: ✭ 2,695 (+1784.62%)
Mutual labels:  smooth-scrolling
really-smooth-scroll
A script that smoothen scrolling in the browser
Stars: ✭ 21 (-85.31%)
Mutual labels:  smooth-scrolling
Good Scroll.el
Attempt at good pixel-based smooth scrolling in Emacs
Stars: ✭ 52 (-63.64%)
Mutual labels:  smooth-scrolling
React Smooth Scroll Hook
A React Hook for using smooth scroll in React Component
Stars: ✭ 114 (-20.28%)
Mutual labels:  smooth-scrolling
Longinus
A pure Swift high-performance asynchronous image loading framework. SwiftUI supported.
Stars: ✭ 250 (+74.83%)
Mutual labels:  smooth-scrolling
Comfortable Motion.vim
Brings physics-based smooth scrolling to the Vim world!
Stars: ✭ 543 (+279.72%)
Mutual labels:  smooth-scrolling
portfolio
V2.0 of my personal portfolio! Feel free to fork it, star it, etc!
Stars: ✭ 31 (-78.32%)
Mutual labels:  smooth-scrolling
Scrollissimo
Javascript plugin for smooth scroll-controlled animations
Stars: ✭ 160 (+11.89%)
Mutual labels:  smooth-scrolling
smooth-parallax
Smooth Parallax makes it a lot easier to move objects when the page scroll with ease.
Stars: ✭ 66 (-53.85%)
Mutual labels:  smooth-scrolling
Scroll Behavior Polyfill
A polyfill for the 'scroll-behavior' CSS-property
Stars: ✭ 76 (-46.85%)
Mutual labels:  smooth-scrolling
Rolly
Custom scroll with inertia, smooth parallax and scenes manager
Stars: ✭ 109 (-23.78%)
Mutual labels:  smooth-scrolling
Moveto
A lightweight scroll animation javascript library without any dependency
Stars: ✭ 2,746 (+1820.28%)
Mutual labels:  smooth-scrolling
Scroll Into View If Needed
Element.scrollIntoView ponyfills for things like "if-needed" and "smooth"
Stars: ✭ 811 (+467.13%)
Mutual labels:  smooth-scrolling
react-cool-virtual
😎 ♻️ A tiny React hook for rendering large datasets like a breeze.
Stars: ✭ 1,031 (+620.98%)
Mutual labels:  smooth-scrolling
Vim Smoothie
Smooth scrolling for Vim done right🥤
Stars: ✭ 579 (+304.9%)
Mutual labels:  smooth-scrolling
Smoothscrollanimations
Demo of a tutorial on how to add smooth page scrolling with an inner image animation
Stars: ✭ 238 (+66.43%)
Mutual labels:  smooth-scrolling
magic-scroll
Apple Magic Mouse scrolling effect for normal mouses
Stars: ✭ 43 (-69.93%)
Mutual labels:  smooth-scrolling
smooth-vuebar
Vue directive wrapper for smooth-scrollbar
Stars: ✭ 29 (-79.72%)
Mutual labels:  smooth-scrolling
Vitrin
A simple app that shows categories, subcategories and items.
Stars: ✭ 70 (-51.05%)
Mutual labels:  smooth-scrolling

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

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].