Skip to content

hulkish/scroll-behavior

 
 

Repository files navigation

scroll-behavior Travis npm package

Scroll behaviors for use with history.

Coveralls Discord

Usage

Enhance your history object with one of the scroll behaviors in this library to get the desired scroll behavior after transitions.

import createHistory from 'history/lib/createBrowserHistory'
import useScroll from 'scroll-behavior/lib/useStandardScroll'

const history = useScroll(createHistory)()

Guide

Installation

$ npm install history scroll-behavior

You may also want to install React Router to obtain a complete routing solution for React that works with history.

Scroll behaviors

useScrollToTop

useScrollToTop scrolls to the top of the page after any transition.

This is not fully reliable for POP transitions.

useSimpleScroll

useSimpleScroll scrolls to the top of the page on PUSH and REPLACE transitions, while allowing the browser to manage scroll position for POP transitions.

This can give pretty good results with synchronous transitions on browsers like Chrome that don't update the scroll position until after they've notified history of the location change. It will not work as well when using asynchronous transitions or with browsers like Firefox that update the scroll position before emitting the location change.

useStandardScroll

useStandardScroll attempts to imitate native browser scroll behavior by recording updates to the window scroll position, then restoring the previous scroll position upon a POP transition.

Notes

  • Support for async transitions is currently very poor. Fixing this will require major breaking API changes in the future.

About

Scroll behaviors for use with history

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%