2.0.0 • Published 8 years ago
prevent-popstate-scroll v2.0.0
prevent-popstate-scroll
Deprecated: use history.scrollRestoration
and its polyfill instead, it has broader compatibility.
Prevent the scroll restoration caused by the
popstate
event or back/forward buttons. Demo
Warning: currently only working on Chrome and Safari. Firefox and IE will need a bit more work.
Usage
// Prevent the scroll, always
preventPopstateScroll.prevent();
// Stop preventing the scroll
preventPopstateScroll.allow();
// Prevent the scroll only once, for example before manually calling history.go(-1)
preventPopstateScroll.preventOnce();
With browserify
npm install --save prevent-popstate-scroll
var preventPopstateScroll = require('prevent-popstate-scroll');
Alternative usage in ES6
import {prevent, allow, preventOnce} from 'prevent-popstate-scroll';
prevent();
allow();
preventOnce();
or:
import preventPopstateScroll from 'prevent-popstate-scroll';
preventPopstateScroll(true);
preventPopstateScroll(false);
API
preventPopstateScroll.prevent()
Prevents the scroll restoration that normally happens after the popstate
History event.
preventPopstateScroll.allow()
Restores the normal browser behavior.
preventPopstateScroll.preventOnce()
Prevents the scroll restoration only once. Useful before manipulating the history once (e.g. history.go(-1)
), without changing the whole site's behavior.
Dependencies
Used on
- http://away.gorving.com/ — to avoid scroll jumping when closing history-enabled lightboxes (i.e. closable via back button)
License
MIT © Federico Brigante