This is a ponyfill for smooth scrolling scroll-into-view-if-needed
.
Install
yarn add smooth-scroll-into-view-if-needed
Usage
import scrollIntoView from 'smooth-scroll-into-view-if-needed'
const node = document.getElementById('hero')
scrollIntoView(node, { behavior: 'smooth' })
scrollIntoView(node, {
behavior: 'smooth',
scrollMode: 'if-needed',
block: 'nearest',
inline: 'nearest',
})
const sequence = async () => {
const slide = document.getElementById('slide-3')
await scrollIntoView(node, { behavior: 'smooth' })
return scrollIntoView(slide, { behavior: 'smooth' })
}
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 'smooth-scroll-into-view-if-needed'
const node = document.getElementById('hero')
scrollIntoView(node, {
behavior: scrollActions =>
scrollActions.forEach(([el, scrollTop, scrollLeft]) => {
el.scrollTop = scrollTop
el.scrollLeft = scrollLeft
const offsetTop = el.scrollTop - scrollTop
const offsetLeft = el.scrollLeft - scrollLeft
}),
})
More documentation will be added (hang in there)