scroll-location
v1.0.3
Published
remember your scroll position on routes
Downloads
4
Readme
scroll-location
remember your scroll position on routes
npm i scroll-location
usage
this example is using bel to create an element. when it’s added to the dom, it will scroll the window to the last known position and begin tracking. when it’s removed, it will stop tracking scroll position.
var html = require('bel')
var scrollLocation = require('scroll-location')
var watcher = scrollLocation(function (state) {
console.log(state)
})
var route = h`
<div onload=${handleLoad} onunload=${watcher.stop}>
please enjoy the time while it is passing by
</div>
`
function handleLoad () {
window.scrollTo(0, watcher.y())
watcher.start()
}
api
watcher = scrollLocation(render?)
create a new instance of scroll position with it’s own internal state. optionally pass a render
function. passes the latest version of the state when changed.
location
grab the most recent state, which looks like { x: 0, y: 0 }
x
returns your horizontal position
y
returns your vertical position