When updating a fragment you may control how Unpoly scrolls the page by passing
a { scroll }
option or [up-scroll]
attribute.
When navigating Unpoly will default to
{ scroll: 'auto' }
.
To reveal the new fragment, pass { scroll: 'target' }
.
The viewport will be scrolled so the new fragment is visible.
See tuning the scroll behavior for additional options.
To reveal a matching element, pass a CSS selector string as { scroll }
option.
From JavaScript you may also pass the Element
object that should be revealed.
Pass { scroll: 'layer' }
to reveal the container element of the updated layer.
Pass { scroll: 'main' }
to reveal the updated layer's main element.
Pass { scroll: false }
to keep all scroll positions.
Pass { scroll: 'reset' }
to reset the scroll positions of all
viewports that are ancestors or descendants of the updated fragment.
Pass { scroll: 'restore' }
to restore the last known scroll positions for the updated layer's URL.
Unpoly will automatically save scroll positions before a fragment update.
You may disable this behavior with { saveScroll: false }
.
#hash
targetPass { scroll: 'hash' }
to focus the element matching the #hash
in the URL.
To only scroll when a main target is updated,
you may append -if-main
to any of the string options in this list.
E.g. { scroll: 'reset-if-main' }
will reset scroll positions, but only if a main target is updated.
To implement other conditions, pass a function instead.
Pass an array of { scroll }
options and Unpoly will use the first applicable value.
E.g. { scroll: ['hash', 'reset'] }
will first try to an element mathing the #hash
in the URL.
If the URL has no #hash
, scroll positions will be reset.
In an [up-scroll]
attribute you may separate scroll options with an or
:
<a href="/path#section" up-follow up-scroll="hash or reset">Link label</a>
Pass { scroll: 'auto' }
to try a sequence of scroll strategies that works for most cases.
This is the default when navigating.
#hash
, scroll to the hash.You may configure this logic in up.fragment.config.autoScroll
.
To implement your custom scrolling logic, pass a function as { scroll }
option.
The function will be called with the updated fragment and an options object. The function is expected to either:
See tuning the scroll behavior for additional scroll-related options.