diff --git a/docs/configuration.md b/docs/configuration.md index 61c3a9b3e..a1fff6ec7 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -62,6 +62,19 @@ window.$docsify = { }; ``` +## alwaysScrollHeader + +- Type: `Boolean` +- Default: `false` + +Always scrolls to ID/header when specified in route. By default, page will use browser's scroll auto-restoration. + +```js +window.$docsify = { + alwaysScrollHeader: true, +}; +``` + ## autoHeader - Type: `Boolean` diff --git a/src/core/config.js b/src/core/config.js index 9a8bdb2f8..cfc384b3d 100644 --- a/src/core/config.js +++ b/src/core/config.js @@ -7,6 +7,7 @@ export default function (vm) { const config = merge( { auto2top: false, + alwaysScrollHeader: false, autoHeader: false, basePath: '', catchPluginErrors: true, diff --git a/src/core/event/index.js b/src/core/event/index.js index 11c02f975..37eb4580e 100644 --- a/src/core/event/index.js +++ b/src/core/event/index.js @@ -12,12 +12,12 @@ import { scrollIntoView, scroll2Top } from './scroll'; export function Events(Base) { return class Events extends Base { $resetEvents(source) { - const { auto2top } = this.config; + const { auto2top, alwaysScrollHeader } = this.config; (() => { - // Rely on the browser's scroll auto-restoration when going back or forward + // If alwaysScrollHeader is false (default), rely on the browser's scroll auto-restoration when going back or forward if (source === 'history') { - return; + alwaysScrollHeader && scrollIntoView(this.route.path, this.route.query.id); } // Scroll to ID if specified if (this.route.query.id) {