scroll-polyfill
Scroll options polyfill:
- Add
ScrollToOptions
polyfill for Element.protype.{scroll|scrollTo|scrollBy}
, window.{scroll|scrollTo|scrollBy}
- Add
ScrollIntoViewOptions
polyfill for Element.prototype.scrollIntoView
- Smooth spring-based scrolling
Install
npm install scroll-polyfill
Usage
Polyfill
import 'scroll-polyfill/auto'
import scrollPolyfill from 'scroll-polyfill'
scrollPolyfill()
scrollPolyfill({force: true})
window.scroll({behavior: 'smooth', left: 100, top: 100})
scroller.scrollBy({behavior: 'smooth', top: 100})
scrollerChild.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
inline: 'start',
})
document.body.scrollIntoView(false)
These methods have smooth spring-based scrolling and are recommended even if polyfill is not installed:
import {scrollTo, scrollBy, scrollIntoView} from 'scroll-polyfill'
scrollTo(window, {behavior: 'smooth', top: 100})
scrollBy(document.scrollingElement, {behavior: 'smooth', top: 100})
scrollIntoView(scrollerChild, {
behavior: 'smooth',
block: 'nearest',
inline: 'start',
})