You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hi there,
I am a mobile dev mainly and also now doing blazor and I have a blazor server 9/10 (moving to 10) and I would like to scroll to the top when navigating to a new page, I have tried many solutions out there but nothing..
Blazor apparently by design seems to remember the position where you were in the previous page... so If I was at the bottom of the page and navigate away to another one it will show at the bottom.. that is annoying.. how can I change behavior
tried tbelow : and none works for me . I would like that when navigating to a new page it goes to the top
<script>
function scrollToTop() {
document.getElementsByClassName('content')[0].scrollTop = 0;
}
</script>
<script>
// Scroll to top after each enhanced navigation completes
Blazor.addEventListener('enhancedload', () => {
window.scrollTo({ top: 0, left: 0, behavior: 'instant' });
});
</script>`
public sealed class ScrollToTopService : IDisposable
{
private readonly NavigationManager _nav;
private readonly IJSRuntime _js;
public ScrollToTopService(NavigationManager nav, IJSRuntime js)
{
_nav = nav;
_js = js;
_nav.LocationChanged += OnLocationChanged;
}
private async void OnLocationChanged(object? _, LocationChangedEventArgs __)
=> await _js.InvokeVoidAsync("window.scrollTo", 0, 0);
public void Dispose() => _nav.LocationChanged -= OnLocationChanged;
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
hi there,
I am a mobile dev mainly and also now doing blazor and I have a blazor server 9/10 (moving to 10) and I would like to scroll to the top when navigating to a new page, I have tried many solutions out there but nothing..
Blazor apparently by design seems to remember the position where you were in the previous page... so If I was at the bottom of the page and navigate away to another one it will show at the bottom.. that is annoying.. how can I change behavior
tried tbelow : and none works for me . I would like that when navigating to a new page it goes to the top
Any suggestions?
thanks
Beta Was this translation helpful? Give feedback.
All reactions