1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
import type Swup from '../Swup.js';
import type { Visit } from './Visit.js';
/**
* Update the scroll position after page render.
* @returns Promise<boolean>
*/
export const scrollToContent = function (this: Swup, visit: Visit): boolean {
const options: ScrollIntoViewOptions = { behavior: 'auto' };
const { target, reset } = visit.scroll;
const scrollTarget = target ?? visit.to.hash;
let scrolled = false;
if (scrollTarget) {
scrolled = this.hooks.callSync(
'scroll:anchor',
visit,
{ hash: scrollTarget, options },
(visit, { hash, options }) => {
const anchor = this.getAnchorElement(hash);
if (anchor) {
anchor.scrollIntoView(options);
}
return !!anchor;
}
);
}
if (reset && !scrolled) {
scrolled = this.hooks.callSync('scroll:top', visit, { options }, (visit, { options }) => {
window.scrollTo({ top: 0, left: 0, ...options });
return true;
});
}
return scrolled;
};
|