public class ScrollPositionHandler extends Object
history.scrollRestoration = "auto" since it won't work with
content generated by JavaScript.
Uses History.state to store history indexes and a
token. This is done because need to know which index in the scroll position
arrays does each history entry map to. The token used to identify the correct
scroll positions array.
Uses window.sessionStorage to store the
actual scroll position arrays. This is used so that the scroll positions can
be always restored, even when navigating from outside back into a middle of
the history chain. The arrays are stored using the token so that they can be
linked to a specific history index.
| Modifier | Constructor and Description |
|---|---|
protected |
ScrollPositionHandler()
Default constructor to use in subclasses to override all functionality
from this class.
|
|
ScrollPositionHandler(Registry registry)
Creates a new instance connected to the given registry.
|
| Modifier and Type | Method and Description |
|---|---|
void |
afterServerNavigation(elemental.json.JsonObject state)
Store scroll positions when there has been navigation triggered by a
click on a link element and a server round-trip was needed.
|
void |
beforeClientNavigation(String newHref)
Store scroll positions when there has been navigation triggered by a
click on a link element and no server round-trip is needed.
|
static double[] |
getScrollPosition()
Gets the scroll position of the page as an array.
|
void |
onPopStateEvent(elemental.events.PopStateEvent event,
boolean triggersServerSideRoundtrip)
Store scroll positions and restore scroll positions depending on the
given pop state event.
|
void |
setIgnoreScrollRestorationOnNextPopStateEvent(boolean ignoreScrollRestorationOnNextPopStateEvent)
Tells this scroll handler that the next call to
onPopStateEvent(PopStateEvent, boolean) should not try to
restore scroll position. |
public ScrollPositionHandler(Registry registry)
registry - the global registryprotected ScrollPositionHandler()
public void onPopStateEvent(elemental.events.PopStateEvent event,
boolean triggersServerSideRoundtrip)
This method behaves differently if there has been a
beforeClientNavigation(String) before this, and if the pop state
event is caused by a fragment change that doesn't require a server side
round-trip.
event - the pop state eventtriggersServerSideRoundtrip - true if the pop state event triggers a server
side request, false if notpublic void setIgnoreScrollRestorationOnNextPopStateEvent(boolean ignoreScrollRestorationOnNextPopStateEvent)
onPopStateEvent(PopStateEvent, boolean) should not try to
restore scroll position.
There are differences between browsers on which order the pop state and fragment change events are fired. This is just to recognize the case where a fragment change event fired by framework (to get scroll to fragment) causes a pop state event that should be ignored here.
ignoreScrollRestorationOnNextPopStateEvent - true to NOT restore scroll on pop state event,
false to restorepublic void beforeClientNavigation(String newHref)
If href for the page navigated into contains a hash (even just #), then the browser will fire a pop state event afterwards.
newHref - the href of the clicked linkpublic void afterServerNavigation(elemental.json.JsonObject state)
state - includes scroll position of the previous page and the complete
href of the router link that was clicked and caused this
navigationpublic static double[] getScrollPosition()
Copyright © 2000–2025 Vaadin Ltd. All rights reserved.